implemented first /sample methods
This commit is contained in:
@ -11,7 +11,16 @@ export default class IdValidate {
|
||||
return this.id.validate(id).error === undefined;
|
||||
}
|
||||
|
||||
static parameter() { // :id url parameter
|
||||
static parameter () { // :id url parameter
|
||||
return ':id([0-9a-f]{24})';
|
||||
}
|
||||
|
||||
static stringify (data) {
|
||||
Object.keys(data).forEach(key => {
|
||||
if (data[key] !== null && data[key].hasOwnProperty('_bsontype') && data[key]._bsontype === 'ObjectID') {
|
||||
data[key] = data[key].toString();
|
||||
}
|
||||
});
|
||||
return data;
|
||||
}
|
||||
}
|
@ -66,7 +66,7 @@ export default class MaterialValidate { // validate input for material
|
||||
}
|
||||
|
||||
static output (data) { // validate output from database for needed properties, strip everything else
|
||||
data._id = data._id.toString();
|
||||
data = IdValidate.stringify(data);
|
||||
const {value, error} = joi.object({
|
||||
_id: IdValidate.get(),
|
||||
name: this.material.name,
|
||||
|
18
src/routes/validate/note_field.ts
Normal file
18
src/routes/validate/note_field.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import joi from '@hapi/joi';
|
||||
|
||||
export default class NoteFieldValidate {
|
||||
private static note_field = {
|
||||
name: joi.string()
|
||||
.max(128),
|
||||
|
||||
qty: joi.number()
|
||||
};
|
||||
|
||||
static output (data) {
|
||||
const {value, error} = joi.object({
|
||||
name: this.note_field.name,
|
||||
qty: this.note_field.qty
|
||||
}).validate(data, {stripUnknown: true});
|
||||
return error !== undefined? null : value;
|
||||
}
|
||||
}
|
77
src/routes/validate/sample.ts
Normal file
77
src/routes/validate/sample.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import joi from '@hapi/joi';
|
||||
|
||||
import IdValidate from './id';
|
||||
|
||||
export default class SampleValidate {
|
||||
private static sample = {
|
||||
number: joi.string()
|
||||
.max(128),
|
||||
|
||||
color: joi.string()
|
||||
.max(128),
|
||||
|
||||
type: joi.string()
|
||||
.max(128),
|
||||
|
||||
batch: joi.string()
|
||||
.max(128)
|
||||
.allow(''),
|
||||
|
||||
notes: joi.object({
|
||||
comment: joi.string()
|
||||
.max(512),
|
||||
|
||||
sample_references: joi.array()
|
||||
.items(joi.object({
|
||||
id: IdValidate.get(),
|
||||
|
||||
relation: joi.string()
|
||||
.max(128)
|
||||
})),
|
||||
|
||||
custom_fields: joi.object()
|
||||
.pattern(/.*/, joi.alternatives()
|
||||
.try(
|
||||
joi.string().max(128),
|
||||
joi.number(),
|
||||
joi.boolean(),
|
||||
joi.date()
|
||||
)
|
||||
)
|
||||
})
|
||||
};
|
||||
|
||||
static input (data, param) { // validate data, param: new(everything required)/change(available attributes are validated)
|
||||
if (param === 'new') {
|
||||
return joi.object({
|
||||
number: this.sample.number.required(),
|
||||
color: this.sample.color.required(),
|
||||
type: this.sample.type.required(),
|
||||
batch: this.sample.batch.required(),
|
||||
material_id: IdValidate.get().required(),
|
||||
notes: this.sample.notes.required()
|
||||
}).validate(data);
|
||||
}
|
||||
else if (param === 'change') {
|
||||
return{error: 'Not implemented!', value: {}};
|
||||
}
|
||||
else {
|
||||
return{error: 'No parameter specified!', value: {}};
|
||||
}
|
||||
}
|
||||
|
||||
static output (data) {
|
||||
data = IdValidate.stringify(data);
|
||||
const {value, error} = joi.object({
|
||||
_id: IdValidate.get(),
|
||||
number: this.sample.number,
|
||||
color: this.sample.color,
|
||||
type: this.sample.type,
|
||||
batch: this.sample.batch,
|
||||
material_id: IdValidate.get(),
|
||||
note_id: IdValidate.get().allow(null),
|
||||
user_id: IdValidate.get()
|
||||
}).validate(data, {stripUnknown: true});
|
||||
return error !== undefined? null : value;
|
||||
}
|
||||
}
|
@ -48,7 +48,7 @@ export default class TemplateValidate {
|
||||
}
|
||||
|
||||
static output (data) { // validate output from database for needed properties, strip everything else
|
||||
data._id = data._id.toString();
|
||||
data = IdValidate.stringify(data);
|
||||
const {value, error} = joi.object({
|
||||
_id: IdValidate.get(),
|
||||
name: this.template.name,
|
||||
|
@ -69,7 +69,7 @@ export default class UserValidate { // validate input for user
|
||||
}
|
||||
|
||||
static output (data) { // validate output from database for needed properties, strip everything else
|
||||
data._id = data._id.toString();
|
||||
data = IdValidate.stringify(data);
|
||||
const {value, error} = joi.object({
|
||||
_id: IdValidate.get(),
|
||||
name: this.user.name,
|
||||
|
Reference in New Issue
Block a user