72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import Joi from 'joi';
|
|
import IdValidate from './id';
|
|
|
|
export default class TemplateValidate {
|
|
private static template = {
|
|
name: Joi.string()
|
|
.max(128),
|
|
|
|
version: Joi.number()
|
|
.min(1),
|
|
|
|
parameters: Joi.array()
|
|
.items(
|
|
Joi.object({
|
|
name: Joi.string()
|
|
.max(128)
|
|
.invalid('condition_template', 'material_template')
|
|
.pattern(/^[^.]+$/)
|
|
.required()
|
|
.messages({'string.pattern.base': 'name must not contain a dot'}),
|
|
|
|
range: Joi.object({
|
|
values: Joi.array()
|
|
.min(1),
|
|
|
|
min: Joi.number(),
|
|
|
|
max: Joi.number(),
|
|
|
|
type: Joi.string()
|
|
.valid('array')
|
|
})
|
|
.oxor('values', 'min')
|
|
.oxor('values', 'max')
|
|
.oxor('type', 'values')
|
|
.oxor('type', 'min')
|
|
.oxor('type', 'max')
|
|
.required()
|
|
})
|
|
)
|
|
};
|
|
|
|
static input (data, param) { // validate input, set param to 'new' to make all attributes required
|
|
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 and strip unwanted properties, returns null if not valid
|
|
data = IdValidate.stringify(data);
|
|
const {value, error} = Joi.object({
|
|
_id: IdValidate.get(),
|
|
name: this.template.name,
|
|
version: this.template.version,
|
|
first_id: IdValidate.get(),
|
|
parameters: this.template.parameters
|
|
}).validate(data, {stripUnknown: true});
|
|
return error !== undefined? null : value;
|
|
}
|
|
} |