Updates as per review comments.
This commit is contained in:
parent
4b54edf02b
commit
0e4748003e
|
@ -1,19 +1,20 @@
|
|||
function validate(schema, property) {
|
||||
// Return a Koa middleware function
|
||||
return (ctx, next) => {
|
||||
if (schema) {
|
||||
let params =
|
||||
ctx[property] != null
|
||||
? ctx[property]
|
||||
: ctx.request[property] != null
|
||||
? ctx.request[property]
|
||||
: null
|
||||
if (!schema) {
|
||||
return next()
|
||||
}
|
||||
let params = null
|
||||
if (ctx[property] != null) {
|
||||
params = ctx[property]
|
||||
} else if (ctx.request[property] != null) {
|
||||
params = ctx.request[property]
|
||||
}
|
||||
const { error } = schema.validate(params)
|
||||
if (error) {
|
||||
ctx.throw(400, `Invalid ${property} - ${error.message}`)
|
||||
return
|
||||
}
|
||||
}
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ function cleanMustache(string) {
|
|||
"]": "",
|
||||
}
|
||||
let regex = new RegExp(/{{[^}}]*}}/g)
|
||||
let match
|
||||
while ((match = regex.exec(string)) !== null) {
|
||||
let matches = [...string.matchAll(regex)]
|
||||
for (let match of matches) {
|
||||
let baseIdx = string.indexOf(match)
|
||||
for (let key of Object.keys(charToReplace)) {
|
||||
let idxChar = match[0].indexOf(key)
|
||||
|
|
Loading…
Reference in New Issue