Updates as per review comments.

This commit is contained in:
mike12345567 2020-09-21 23:19:45 +01:00
parent 4b54edf02b
commit 0e4748003e
2 changed files with 15 additions and 14 deletions

View File

@ -1,19 +1,20 @@
function validate(schema, property) { function validate(schema, property) {
// Return a Koa middleware function // Return a Koa middleware function
return (ctx, next) => { return (ctx, next) => {
if (schema) { if (!schema) {
let params = return next()
ctx[property] != null }
? ctx[property] let params = null
: ctx.request[property] != null if (ctx[property] != null) {
? ctx.request[property] params = ctx[property]
: null } else if (ctx.request[property] != null) {
params = ctx.request[property]
}
const { error } = schema.validate(params) const { error } = schema.validate(params)
if (error) { if (error) {
ctx.throw(400, `Invalid ${property} - ${error.message}`) ctx.throw(400, `Invalid ${property} - ${error.message}`)
return return
} }
}
return next() return next()
} }
} }

View File

@ -10,8 +10,8 @@ function cleanMustache(string) {
"]": "", "]": "",
} }
let regex = new RegExp(/{{[^}}]*}}/g) let regex = new RegExp(/{{[^}}]*}}/g)
let match let matches = [...string.matchAll(regex)]
while ((match = regex.exec(string)) !== null) { for (let match of matches) {
let baseIdx = string.indexOf(match) let baseIdx = string.indexOf(match)
for (let key of Object.keys(charToReplace)) { for (let key of Object.keys(charToReplace)) {
let idxChar = match[0].indexOf(key) let idxChar = match[0].indexOf(key)