implemented more /sample methods
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
import joi from '@hapi/joi';
 | 
			
		||||
import Joi from '@hapi/joi';
 | 
			
		||||
 | 
			
		||||
export default class IdValidate {
 | 
			
		||||
  private static id = joi.string().pattern(new RegExp('[0-9a-f]{24}')).length(24);
 | 
			
		||||
  private static id = Joi.string().pattern(new RegExp('[0-9a-f]{24}')).length(24);
 | 
			
		||||
 | 
			
		||||
  static get () {
 | 
			
		||||
    return this.id;
 | 
			
		||||
 
 | 
			
		||||
@@ -31,9 +31,11 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
    numbers: joi.array()
 | 
			
		||||
      .items(joi.object({
 | 
			
		||||
        color: joi.string()
 | 
			
		||||
          .max(128),
 | 
			
		||||
          .max(128)
 | 
			
		||||
          .required(),
 | 
			
		||||
        number: joi.number()
 | 
			
		||||
          .min(0)
 | 
			
		||||
          .required()
 | 
			
		||||
      }))
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
@@ -46,7 +48,7 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
        mineral: this.material.mineral.required(),
 | 
			
		||||
        glass_fiber: this.material.glass_fiber.required(),
 | 
			
		||||
        carbon_fiber: this.material.carbon_fiber.required(),
 | 
			
		||||
        numbers: this.material.numbers
 | 
			
		||||
        numbers: this.material.numbers.required()
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
    else if (param === 'change') {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,15 @@
 | 
			
		||||
import joi from '@hapi/joi';
 | 
			
		||||
import Joi from '@hapi/joi';
 | 
			
		||||
 | 
			
		||||
export default class NoteFieldValidate {
 | 
			
		||||
  private static note_field = {
 | 
			
		||||
    name: joi.string()
 | 
			
		||||
    name: Joi.string()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    qty: joi.number()
 | 
			
		||||
    qty: Joi.number()
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  static output (data) {
 | 
			
		||||
    const {value, error} = joi.object({
 | 
			
		||||
    const {value, error} = Joi.object({
 | 
			
		||||
      name: this.note_field.name,
 | 
			
		||||
      qty: this.note_field.qty
 | 
			
		||||
    }).validate(data, {stripUnknown: true});
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								src/routes/validate/res400.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/routes/validate/res400.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
export default function res400 (error, res) {
 | 
			
		||||
  res.status(400).json({status: 'Invalid body format', details: error.details[0].message});
 | 
			
		||||
}
 | 
			
		||||
@@ -1,41 +1,41 @@
 | 
			
		||||
import joi from '@hapi/joi';
 | 
			
		||||
import Joi from '@hapi/joi';
 | 
			
		||||
 | 
			
		||||
import IdValidate from './id';
 | 
			
		||||
 | 
			
		||||
export default class SampleValidate {
 | 
			
		||||
  private static sample = {
 | 
			
		||||
    number: joi.string()
 | 
			
		||||
    number: Joi.string()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    color: joi.string()
 | 
			
		||||
    color: Joi.string()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    type: joi.string()
 | 
			
		||||
    type: Joi.string()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    batch: joi.string()
 | 
			
		||||
    batch: Joi.string()
 | 
			
		||||
      .max(128)
 | 
			
		||||
      .allow(''),
 | 
			
		||||
 | 
			
		||||
    notes: joi.object({
 | 
			
		||||
      comment: joi.string()
 | 
			
		||||
    notes: Joi.object({
 | 
			
		||||
      comment: Joi.string()
 | 
			
		||||
        .max(512),
 | 
			
		||||
 | 
			
		||||
      sample_references: joi.array()
 | 
			
		||||
        .items(joi.object({
 | 
			
		||||
      sample_references: Joi.array()
 | 
			
		||||
        .items(Joi.object({
 | 
			
		||||
          id: IdValidate.get(),
 | 
			
		||||
 | 
			
		||||
          relation: joi.string()
 | 
			
		||||
          relation: Joi.string()
 | 
			
		||||
            .max(128)
 | 
			
		||||
        })),
 | 
			
		||||
 | 
			
		||||
      custom_fields: joi.object()
 | 
			
		||||
        .pattern(/.*/, joi.alternatives()
 | 
			
		||||
      custom_fields: Joi.object()
 | 
			
		||||
        .pattern(/.*/, Joi.alternatives()
 | 
			
		||||
          .try(
 | 
			
		||||
            joi.string().max(128),
 | 
			
		||||
            joi.number(),
 | 
			
		||||
            joi.boolean(),
 | 
			
		||||
            joi.date()
 | 
			
		||||
            Joi.string().max(128),
 | 
			
		||||
            Joi.number(),
 | 
			
		||||
            Joi.boolean(),
 | 
			
		||||
            Joi.date()
 | 
			
		||||
          )
 | 
			
		||||
        )
 | 
			
		||||
    })
 | 
			
		||||
@@ -43,7 +43,7 @@ export default class SampleValidate {
 | 
			
		||||
 | 
			
		||||
  static input (data, param) {  // validate data, param: new(everything required)/change(available attributes are validated)
 | 
			
		||||
    if (param === 'new') {
 | 
			
		||||
      return joi.object({
 | 
			
		||||
      return Joi.object({
 | 
			
		||||
        number: this.sample.number.required(),
 | 
			
		||||
        color: this.sample.color.required(),
 | 
			
		||||
        type: this.sample.type.required(),
 | 
			
		||||
@@ -53,7 +53,14 @@ export default class SampleValidate {
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
    else if (param === 'change') {
 | 
			
		||||
      return{error: 'Not implemented!', value: {}};
 | 
			
		||||
      return Joi.object({
 | 
			
		||||
        number: this.sample.number,
 | 
			
		||||
        color: this.sample.color,
 | 
			
		||||
        type: this.sample.type,
 | 
			
		||||
        batch: this.sample.batch,
 | 
			
		||||
        material_id: IdValidate.get(),
 | 
			
		||||
        notes: this.sample.notes,
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
      return{error: 'No parameter specified!', value: {}};
 | 
			
		||||
@@ -62,7 +69,7 @@ export default class SampleValidate {
 | 
			
		||||
 | 
			
		||||
  static output (data) {
 | 
			
		||||
    data = IdValidate.stringify(data);
 | 
			
		||||
    const {value, error} = joi.object({
 | 
			
		||||
    const {value, error} = Joi.object({
 | 
			
		||||
      _id: IdValidate.get(),
 | 
			
		||||
      number: this.sample.number,
 | 
			
		||||
      color: this.sample.color,
 | 
			
		||||
 
 | 
			
		||||
@@ -1,32 +1,32 @@
 | 
			
		||||
import joi from '@hapi/joi';
 | 
			
		||||
import Joi from '@hapi/joi';
 | 
			
		||||
import globals from '../../globals';
 | 
			
		||||
 | 
			
		||||
import IdValidate from './id';
 | 
			
		||||
 | 
			
		||||
export default class UserValidate {  // validate input for user
 | 
			
		||||
  private static user = {
 | 
			
		||||
    name: joi.string()
 | 
			
		||||
    name: Joi.string()
 | 
			
		||||
      .alphanum()
 | 
			
		||||
      .lowercase()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    email: joi.string()
 | 
			
		||||
    email: Joi.string()
 | 
			
		||||
      .email({minDomainSegments: 2})
 | 
			
		||||
      .lowercase()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    pass: joi.string()
 | 
			
		||||
    pass: Joi.string()
 | 
			
		||||
      .pattern(new RegExp('^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#%&\'()*+,-.\\/:;<=>?@[\\]^_`{|}~])(?=\\S+$).{8,}$'))
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    level: joi.string()
 | 
			
		||||
    level: Joi.string()
 | 
			
		||||
      .valid(...globals.levels),
 | 
			
		||||
 | 
			
		||||
    location: joi.string()
 | 
			
		||||
    location: Joi.string()
 | 
			
		||||
      .alphanum()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    device_name: joi.string()
 | 
			
		||||
    device_name: Joi.string()
 | 
			
		||||
      .allow('')
 | 
			
		||||
      .max(128),
 | 
			
		||||
  };
 | 
			
		||||
@@ -35,7 +35,7 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
 | 
			
		||||
  static input (data, param) {
 | 
			
		||||
    if (param === 'new') {
 | 
			
		||||
      return joi.object({
 | 
			
		||||
      return Joi.object({
 | 
			
		||||
        name: this.user.name.required(),
 | 
			
		||||
        email: this.user.email.required(),
 | 
			
		||||
        pass: this.user.pass.required(),
 | 
			
		||||
@@ -45,7 +45,7 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
    else if (param === 'change') {
 | 
			
		||||
      return joi.object({
 | 
			
		||||
      return Joi.object({
 | 
			
		||||
        name: this.user.name,
 | 
			
		||||
        email: this.user.email,
 | 
			
		||||
        pass: this.user.pass,
 | 
			
		||||
@@ -54,7 +54,7 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
    else if (param === 'changeadmin') {
 | 
			
		||||
      return joi.object({
 | 
			
		||||
      return Joi.object({
 | 
			
		||||
        name: this.user.name,
 | 
			
		||||
        email: this.user.email,
 | 
			
		||||
        pass: this.user.pass,
 | 
			
		||||
@@ -70,7 +70,7 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
 | 
			
		||||
  static output (data) {  // validate output from database for needed properties, strip everything else
 | 
			
		||||
    data = IdValidate.stringify(data);
 | 
			
		||||
    const {value, error} = joi.object({
 | 
			
		||||
    const {value, error} = Joi.object({
 | 
			
		||||
      _id: IdValidate.get(),
 | 
			
		||||
      name: this.user.name,
 | 
			
		||||
      email: this.user.email,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user