From ec03e0699c0ae62baf8b22579cb53f206d254be8 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 15 May 2020 15:14:18 +0200 Subject: [PATCH] number prefixes are now not allowed to contain numbers --- .idea/libraries/dist.xml | 13 ++++++++++++ src/routes/template.spec.ts | 7 +++---- src/routes/validate/template.ts | 35 +++++++++++++++++---------------- 3 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 .idea/libraries/dist.xml diff --git a/.idea/libraries/dist.xml b/.idea/libraries/dist.xml new file mode 100644 index 0000000..3d92275 --- /dev/null +++ b/.idea/libraries/dist.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/routes/template.spec.ts b/src/routes/template.spec.ts index 68b99e9..c2e6fcd 100644 --- a/src/routes/template.spec.ts +++ b/src/routes/template.spec.ts @@ -4,7 +4,6 @@ import TemplateTreatmentModel from '../models/treatment_template'; import TemplateMeasurementModel from '../models/measurement_template'; import TestHelper from "../test/helper"; -// TODO: check number prefix to have no numbers describe('/template', () => { let server; @@ -371,14 +370,14 @@ describe('/template', () => { res: {status: 'Invalid body format', details: '"parameters[0].name" is required'} }); }); - it('rejects a missing number prefix', done => { + it('rejects a number prefix containing numbers', done => { TestHelper.request(server, done, { method: 'post', url: '/template/treatment/new', auth: {basic: 'admin'}, httpStatus: 400, - req: {name: 'heat aging', parameters: [{name: 'time', range: {min: 1}}]}, - res: {status: 'Invalid body format', details: '"number_prefix" is required'} + req: {name: 'heat aging', number_prefix: 'AB5', parameters: [{name: 'time', range: {min: 1}}]}, + res: {status: 'Invalid body format', details: '"number_prefix" with value "AB5" fails to match the required pattern: /^[a-zA-Z]+$/'} }); }); it('rejects a missing parameter range', done => { diff --git a/src/routes/validate/template.ts b/src/routes/validate/template.ts index 7cb461d..269e1a2 100644 --- a/src/routes/validate/template.ts +++ b/src/routes/validate/template.ts @@ -1,35 +1,36 @@ -import joi from '@hapi/joi'; +import Joi from '@hapi/joi'; import IdValidate from './id'; export default class TemplateValidate { private static template = { - name: joi.string() + name: Joi.string() .max(128), - version: joi.number() + version: Joi.number() .min(1), - number_prefix: joi.string() + number_prefix: Joi.string() + .pattern(/^[a-zA-Z]+$/) .min(1) .max(16), - parameters: joi.array() + parameters: Joi.array() .min(1) .items( - joi.object({ - name: joi.string() + Joi.object({ + name: Joi.string() .max(128) .required(), - range: joi.object({ - values: joi.array() + range: Joi.object({ + values: Joi.array() .min(1), - min: joi.number(), + min: Joi.number(), - max: joi.number(), + max: Joi.number(), - type: joi.string() + type: Joi.string() .valid('array') }) .oxor('values', 'min') @@ -45,14 +46,14 @@ export default class TemplateValidate { static input (data, param, template) { // validate data, param: new(everything required)/change(available attributes are validated) if (param === 'new') { if (template === 'treatment') { - return joi.object({ + return Joi.object({ name: this.template.name.required(), number_prefix: this.template.number_prefix.required(), parameters: this.template.parameters.required() }).validate(data); } else { - return joi.object({ + return Joi.object({ name: this.template.name.required(), parameters: this.template.parameters.required() }).validate(data); @@ -60,14 +61,14 @@ export default class TemplateValidate { } else if (param === 'change') { if (template === 'treatment') { - return joi.object({ + return Joi.object({ name: this.template.name, number_prefix: this.template.number_prefix, parameters: this.template.parameters }).validate(data); } else { - return joi.object({ + return Joi.object({ name: this.template.name, parameters: this.template.parameters }).validate(data); @@ -98,7 +99,7 @@ export default class TemplateValidate { parameters: this.template.parameters }; } - const {value, error} = joi.object(joiObject).validate(data, {stripUnknown: true}); + const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true}); return error !== undefined? null : value; } } \ No newline at end of file