diff --git a/lib/expose.js b/lib/expose.js index 01f2b5f4..8768efe9 100644 --- a/lib/expose.js +++ b/lib/expose.js @@ -117,25 +117,26 @@ function expose(definitions, methods, path, directory) { }); } - // add validation let validation = ''; + let validators = []; if (secure.length > 0) { - validation = ''; secure.forEach((property) => { let origin = property.type; - if (origin === 'path') {origin = 'req.params' }else {origin = 'this.data'} - validation = ` - async validate() { - try { - ${origin}['${property.property}'] = await global.FieldValidator.validate('${property.value}','${property.parameter_type}',${origin}['${property.property}'], this.req, this.res) - } catch (error) { - console.log('validation error', error); - throw new Error(error.message); - } - } - `; + if (origin === 'path') { origin = 'req.params' } else { origin = 'this.data' }; + validators.push(`${origin}['${property.property}'] = await global.FieldValidator.validate('${property.value}','${property.parameter_type}',${origin}['${property.property}'], this.req, this.res);`); }); + + validation = ` + async validate() { + try { + ${validators.join('\n')} + } catch (error) { + console.log('validation error', error); + throw new Error(error.message); + } + } + `; } // compile the file