Archived
2

adapted existing /sample methods to condition, removed /condition route

This commit is contained in:
VLE2FE
2020-05-27 14:31:17 +02:00
parent 2829752d0c
commit 0acb9dd6fc
34 changed files with 1753 additions and 1374 deletions

View File

@ -1,50 +0,0 @@
import Joi from '@hapi/joi';
import IdValidate from './id';
export default class ConditionValidate {
private static condition = {
number: Joi.string()
.max(128),
parameters: Joi.object()
.pattern(/.*/, Joi.alternatives()
.try(
Joi.string().max(128),
Joi.number(),
Joi.boolean(),
Joi.array()
)
)
}
static input (data, param) { // validate input, set param to 'new' to make all attributes required
if (param === 'new') {
return Joi.object({
sample_id: IdValidate.get().required(),
parameters: this.condition.parameters.required(),
treatment_template: IdValidate.get().required()
}).validate(data);
}
else if (param === 'change') {
return Joi.object({
parameters: this.condition.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(),
sample_id: IdValidate.get(),
number: this.condition.number,
parameters: this.condition.parameters,
treatment_template: IdValidate.get()
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}

View File

@ -1,39 +1,39 @@
import joi from '@hapi/joi';
import Joi from '@hapi/joi';
import IdValidate from './id';
export default class MaterialValidate { // validate input for material
private static material = {
name: joi.string()
name: Joi.string()
.max(128),
supplier: joi.string()
supplier: Joi.string()
.max(128),
group: joi.string()
group: Joi.string()
.max(128),
mineral: joi.number()
mineral: Joi.number()
.integer()
.min(0)
.max(100),
glass_fiber: joi.number()
glass_fiber: Joi.number()
.integer()
.min(0)
.max(100),
carbon_fiber: joi.number()
carbon_fiber: Joi.number()
.integer()
.min(0)
.max(100),
numbers: joi.array()
.items(joi.object({
color: joi.string()
numbers: Joi.array()
.items(Joi.object({
color: Joi.string()
.max(128)
.required(),
number: joi.string()
number: Joi.string()
.max(128)
.allow('')
.required()
@ -42,7 +42,7 @@ export default class MaterialValidate { // validate input for material
static input (data, param) { // validate input, set param to 'new' to make all attributes required
if (param === 'new') {
return joi.object({
return Joi.object({
name: this.material.name.required(),
supplier: this.material.supplier.required(),
group: this.material.group.required(),
@ -53,7 +53,7 @@ export default class MaterialValidate { // validate input for material
}).validate(data);
}
else if (param === 'change') {
return joi.object({
return Joi.object({
name: this.material.name,
supplier: this.material.supplier,
group: this.material.group,
@ -70,7 +70,7 @@ export default class MaterialValidate { // validate input for material
static output (data) { // validate output and strip unwanted properties, returns null if not valid
data = IdValidate.stringify(data);
const {value, error} = joi.object({
const {value, error} = Joi.object({
_id: IdValidate.get(),
name: this.material.name,
supplier: this.material.supplier,
@ -82,4 +82,17 @@ export default class MaterialValidate { // validate input for material
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
static outputV() { // return output validator
return Joi.object({
_id: IdValidate.get(),
name: this.material.name,
supplier: this.material.supplier,
group: this.material.group,
mineral: this.material.mineral,
glass_fiber: this.material.glass_fiber,
carbon_fiber: this.material.carbon_fiber,
numbers: this.material.numbers
});
}
}

View File

@ -1,6 +1,8 @@
import Joi from '@hapi/joi';
import IdValidate from './id';
import UserValidate from './user';
import MaterialValidate from './material';
export default class SampleValidate {
private static sample = {
@ -17,13 +19,16 @@ export default class SampleValidate {
.max(128)
.allow(''),
condition: Joi.object(),
notes: Joi.object({
comment: Joi.string()
.max(512),
.max(512)
.allow(''),
sample_references: Joi.array()
.items(Joi.object({
id: IdValidate.get(),
sample_id: IdValidate.get(),
relation: Joi.string()
.max(128)
@ -47,6 +52,7 @@ export default class SampleValidate {
color: this.sample.color.required(),
type: this.sample.type.required(),
batch: this.sample.batch.required(),
condition: this.sample.condition.required(),
material_id: IdValidate.get().required(),
notes: this.sample.notes.required()
}).validate(data);
@ -56,6 +62,7 @@ export default class SampleValidate {
color: this.sample.color,
type: this.sample.type,
batch: this.sample.batch,
condition: this.sample.condition,
material_id: IdValidate.get(),
notes: this.sample.notes,
}).validate(data);
@ -65,18 +72,39 @@ export default class SampleValidate {
}
}
static output (data) { // validate output and strip unwanted properties, returns null if not valid
static output (data, param = 'refs') { // validate output and strip unwanted properties, returns null if not valid
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});
let joiObject;
if (param === 'refs') {
joiObject = {
_id: IdValidate.get(),
number: this.sample.number,
color: this.sample.color,
type: this.sample.type,
batch: this.sample.batch,
condition: this.sample.condition,
material_id: IdValidate.get(),
note_id: IdValidate.get().allow(null),
user_id: IdValidate.get()
};
}
else if(param === 'details') {
joiObject = {
_id: IdValidate.get(),
number: this.sample.number,
color: this.sample.color,
type: this.sample.type,
batch: this.sample.batch,
condition: this.sample.condition,
material: MaterialValidate.outputV(),
notes: this.sample.notes,
user: UserValidate.username()
}
}
else {
return null;
}
const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}

View File

@ -9,11 +9,6 @@ export default class TemplateValidate {
version: Joi.number()
.min(1),
number_prefix: Joi.string()
.pattern(/^[a-zA-Z]+$/)
.min(1)
.max(16),
parameters: Joi.array()
.min(1)
.items(
@ -43,63 +38,32 @@ export default class TemplateValidate {
)
};
static input (data, param, template) { // validate input, set param to 'new' to make all attributes required
static input (data, param) { // validate input, set param to 'new' to make all attributes required
if (param === 'new') {
if (template === 'treatment') {
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({
name: this.template.name.required(),
parameters: this.template.parameters.required()
}).validate(data);
}
return Joi.object({
name: this.template.name.required(),
parameters: this.template.parameters.required()
}).validate(data);
}
else if (param === 'change') {
if (template === 'treatment') {
return Joi.object({
name: this.template.name,
number_prefix: this.template.number_prefix,
parameters: this.template.parameters
}).validate(data);
}
else {
return Joi.object({
name: this.template.name,
parameters: this.template.parameters
}).validate(data);
}
return Joi.object({
name: this.template.name,
parameters: this.template.parameters
}).validate(data);
}
else {
return{error: 'No parameter specified!', value: {}};
}
}
static output (data, template) { // validate output and strip unwanted properties, returns null if not valid
static output (data) { // validate output and strip unwanted properties, returns null if not valid
data = IdValidate.stringify(data);
let joiObject;
if (template === 'treatment') { // differentiate between measurement and treatment (has number_prefix) template
joiObject = {
_id: IdValidate.get(),
name: this.template.name,
version: this.template.version,
number_prefix: this.template.number_prefix,
parameters: this.template.parameters
};
}
else {
joiObject = {
_id: IdValidate.get(),
name: this.template.name,
version: this.template.version,
parameters: this.template.parameters
};
}
const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true});
const {value, error} = Joi.object({
_id: IdValidate.get(),
name: this.template.name,
version: this.template.version,
parameters: this.template.parameters
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}

View File

@ -84,4 +84,8 @@ export default class UserValidate { // validate input for user
static isSpecialName (name) { // true if name belongs to special names
return this.specialUsernames.indexOf(name) > -1;
}
static username() {
return this.user.name;
}
}