finished /template methods
This commit is contained in:
59
src/routes/validate/template.ts
Normal file
59
src/routes/validate/template.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import joi from '@hapi/joi';
|
||||
import IdValidate from './id';
|
||||
|
||||
export default class TemplateValidate {
|
||||
private static template = {
|
||||
name: joi.string()
|
||||
.max(128),
|
||||
|
||||
parameters: joi.array()
|
||||
.min(1)
|
||||
.items(
|
||||
joi.object({
|
||||
name: joi.string()
|
||||
.max(128)
|
||||
.required(),
|
||||
|
||||
range: joi.object({
|
||||
values: joi.array()
|
||||
.min(1),
|
||||
|
||||
min: joi.number(),
|
||||
|
||||
max: joi.number()
|
||||
})
|
||||
.oxor('values', 'min')
|
||||
.oxor('values', 'max')
|
||||
.required()
|
||||
})
|
||||
)
|
||||
};
|
||||
|
||||
static input (data, param) { // validate data, param: new(everything required)/change(available attributes are validated)
|
||||
if (param === 'new') {
|
||||
return joi.object({
|
||||
name: this.template.name.required(),
|
||||
parameters: this.template.parameters.required()
|
||||
}).validate(data);
|
||||
}
|
||||
else if (param === 'change') {
|
||||
return joi.object({
|
||||
name: this.template.name,
|
||||
parameters: this.template.parameters
|
||||
}).validate(data);
|
||||
}
|
||||
else {
|
||||
return{error: 'No parameter specified!', value: {}};
|
||||
}
|
||||
}
|
||||
|
||||
static output (data) { // validate output from database for needed properties, strip everything else
|
||||
data._id = data._id.toString();
|
||||
const {value, error} = joi.object({
|
||||
_id: IdValidate.get(),
|
||||
name: this.template.name,
|
||||
parameters: this.template.parameters
|
||||
}).validate(data, {stripUnknown: true});
|
||||
return error !== undefined? null : value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user