mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
improve Schematized
util
This commit is contained in:
parent
075112dc04
commit
8f903245a8
@ -1,53 +1,61 @@
|
|||||||
export default (schema = {}, fn) => {
|
export default (schema = {}, fn) => {
|
||||||
return async (req, res, next) => {
|
return async (req, res, next) => {
|
||||||
// not necessary since linebridge lib will do this for you
|
if (typeof req.body === "undefined") {
|
||||||
// if (typeof req.body === "undefined") {
|
req.body = {}
|
||||||
// req.body = {}
|
}
|
||||||
// }
|
if (typeof req.query === "undefined") {
|
||||||
// if (typeof req.query === "undefined") {
|
req.query = {}
|
||||||
// req.query = {}
|
}
|
||||||
// }
|
|
||||||
|
if (typeof req.selection !== "object") {
|
||||||
|
req.selection = {}
|
||||||
|
}
|
||||||
|
|
||||||
if (schema.required) {
|
if (schema.required) {
|
||||||
if (Array.isArray(schema.required)) {
|
if (!Array.isArray(schema.required)) {
|
||||||
const missingKeys = []
|
|
||||||
const requiredKeys = Array.isArray(schema.required) ? schema.required : []
|
|
||||||
|
|
||||||
for await (let key of requiredKeys) {
|
|
||||||
if (typeof req.body[key] === "undefined" && typeof req.query[key] === "undefined") {
|
|
||||||
req.selection[key] = req.body[key]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (missingKeys.length > 0) {
|
|
||||||
return res.status(400).json({ error: `Missing ${missingKeys}` })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.warn("[INVALID SCHEMA] schema.required is defined but is not an array")
|
console.warn("[INVALID SCHEMA] schema.required is defined but is not an array")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const missingKeys = []
|
||||||
|
const requiredKeys = Array.isArray(schema.required) ? schema.required : []
|
||||||
|
|
||||||
|
for (let key of requiredKeys) {
|
||||||
|
const value = req.body[key] || req.query[key]
|
||||||
|
|
||||||
|
if (!value || typeof value === "undefined") {
|
||||||
|
missingKeys.push(key)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
req.selection[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (missingKeys.length > 0) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: `Missing required keys > ${missingKeys}`,
|
||||||
|
missingKeys: missingKeys
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (schema.select) {
|
if (schema.select) {
|
||||||
if (Array.isArray(schema.select)) {
|
if (!Array.isArray(schema.select)) {
|
||||||
if (typeof req.selection !== "object") {
|
|
||||||
req.selection = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign objects along request body and query.
|
|
||||||
for await (let key of schema.select) {
|
|
||||||
if (req.body && typeof req.body[key] !== "undefined") {
|
|
||||||
req.selection[key] = req.body[key]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.query && typeof req.query[key] !== "undefined") {
|
|
||||||
req.selection[key] = req.query[key]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.warn("[INVALID SCHEMA] schema.select is defined but is not an array")
|
console.warn("[INVALID SCHEMA] schema.select is defined but is not an array")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign objects along request body and query.
|
||||||
|
for await (let key of schema.select) {
|
||||||
|
if (req.body && typeof req.body[key] !== "undefined") {
|
||||||
|
req.selection[key] = req.body[key]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.query && typeof req.query[key] !== "undefined") {
|
||||||
|
req.selection[key] = req.query[key]
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user