restructured material
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
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).messages({'string.pattern.base': 'Invalid object id'});
 | 
			
		||||
 | 
			
		||||
  static get () {  // return joi validation
 | 
			
		||||
    return this.id;
 | 
			
		||||
 
 | 
			
		||||
@@ -13,31 +13,13 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
    group: Joi.string()
 | 
			
		||||
      .max(128),
 | 
			
		||||
 | 
			
		||||
    mineral: Joi.number()
 | 
			
		||||
      .integer()
 | 
			
		||||
      .min(0)
 | 
			
		||||
      .max(100),
 | 
			
		||||
 | 
			
		||||
    glass_fiber: Joi.number()
 | 
			
		||||
      .integer()
 | 
			
		||||
      .min(0)
 | 
			
		||||
      .max(100),
 | 
			
		||||
 | 
			
		||||
    carbon_fiber: Joi.number()
 | 
			
		||||
      .integer()
 | 
			
		||||
      .min(0)
 | 
			
		||||
      .max(100),
 | 
			
		||||
    properties: Joi.object(),
 | 
			
		||||
 | 
			
		||||
    numbers: Joi.array()
 | 
			
		||||
      .items(Joi.object({
 | 
			
		||||
        color: Joi.string()
 | 
			
		||||
          .max(128)
 | 
			
		||||
          .required(),
 | 
			
		||||
        number: Joi.string()
 | 
			
		||||
          .max(128)
 | 
			
		||||
          .allow('')
 | 
			
		||||
          .required()
 | 
			
		||||
      }))
 | 
			
		||||
      .items(
 | 
			
		||||
        Joi.string()
 | 
			
		||||
          .length(10)
 | 
			
		||||
      )
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  static input (data, param) {  // validate input, set param to 'new' to make all attributes required
 | 
			
		||||
@@ -46,9 +28,7 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
        name: this.material.name.required(),
 | 
			
		||||
        supplier: this.material.supplier.required(),
 | 
			
		||||
        group: this.material.group.required(),
 | 
			
		||||
        mineral: this.material.mineral.required(),
 | 
			
		||||
        glass_fiber: this.material.glass_fiber.required(),
 | 
			
		||||
        carbon_fiber: this.material.carbon_fiber.required(),
 | 
			
		||||
        properties: this.material.properties.required(),
 | 
			
		||||
        numbers: this.material.numbers.required()
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
@@ -57,9 +37,7 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
        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,
 | 
			
		||||
        properties: this.material.properties,
 | 
			
		||||
        numbers: this.material.numbers
 | 
			
		||||
      }).validate(data);
 | 
			
		||||
    }
 | 
			
		||||
@@ -77,9 +55,7 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
      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,
 | 
			
		||||
      properties: this.material.properties,
 | 
			
		||||
      numbers: this.material.numbers
 | 
			
		||||
    }).validate(data, {stripUnknown: true});
 | 
			
		||||
    return error !== undefined? null : value;
 | 
			
		||||
@@ -101,9 +77,7 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
      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,
 | 
			
		||||
      properties: this.material.properties,
 | 
			
		||||
      numbers: this.material.numbers
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -195,7 +195,6 @@ export default class SampleValidate {
 | 
			
		||||
              validator = Joi.object(this.sample);
 | 
			
		||||
            }
 | 
			
		||||
            const {value, error} = validator.validate({[field]: e});
 | 
			
		||||
            console.log(value);
 | 
			
		||||
            if (error) throw error;  // reject invalid values  // TODO: return exact error description, handle in frontend filters
 | 
			
		||||
            return value[field];
 | 
			
		||||
          });
 | 
			
		||||
@@ -212,10 +211,10 @@ export default class SampleValidate {
 | 
			
		||||
      'page-size': Joi.number().integer().min(1),
 | 
			
		||||
      sort: Joi.string().pattern(new RegExp('^(' + this.sortKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')-(asc|desc)$', 'm')).default('_id-asc'),
 | 
			
		||||
      csv: Joi.boolean().default(false),
 | 
			
		||||
      fields: Joi.array().items(Joi.string().pattern(new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm'))).default(['_id','number','type','batch','material_id','color','condition','note_id','user_id','added']),
 | 
			
		||||
      fields: Joi.array().items(Joi.string().pattern(new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm'))).default(['_id','number','type','batch','material_id','color','condition','note_id','user_id','added']).messages({'string.pattern.base': 'Invalid field name'}),
 | 
			
		||||
      filters: Joi.array().items(Joi.object({
 | 
			
		||||
        mode: Joi.string().valid('eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'in', 'nin'),
 | 
			
		||||
        field: Joi.string().pattern(new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm')),
 | 
			
		||||
        field: Joi.string().pattern(new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm')).messages({'string.pattern.base': 'Invalid filter field name'}),
 | 
			
		||||
        values: Joi.array().items(Joi.alternatives().try(Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso())).min(1)
 | 
			
		||||
      })).default([])
 | 
			
		||||
    }).with('to-page', 'page-size').validate(data);
 | 
			
		||||
 
 | 
			
		||||
@@ -15,8 +15,10 @@ export default class TemplateValidate {
 | 
			
		||||
        Joi.object({
 | 
			
		||||
          name: Joi.string()
 | 
			
		||||
            .max(128)
 | 
			
		||||
            .invalid('condition_template')
 | 
			
		||||
            .required(),
 | 
			
		||||
            .invalid('condition_template', 'material_template')
 | 
			
		||||
            .pattern(/^[^.]+$/)
 | 
			
		||||
            .required()
 | 
			
		||||
            .messages({'string.pattern.base': 'name must not contain a dot'}),
 | 
			
		||||
 | 
			
		||||
          range: Joi.object({
 | 
			
		||||
            values: Joi.array()
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,8 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
    name: Joi.string()
 | 
			
		||||
      .lowercase()
 | 
			
		||||
      .pattern(new RegExp('^[a-z0-9-_.]+$'))
 | 
			
		||||
      .max(128),
 | 
			
		||||
      .max(128)
 | 
			
		||||
      .messages({'string.pattern.base': 'name must only contain a-z0-9_.'}),
 | 
			
		||||
 | 
			
		||||
    email: Joi.string()
 | 
			
		||||
      .email({minDomainSegments: 2})
 | 
			
		||||
@@ -17,7 +18,8 @@ export default class UserValidate {  // validate input for user
 | 
			
		||||
 | 
			
		||||
    pass: Joi.string()
 | 
			
		||||
      .pattern(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#%&'()*+,-.\/:;<=>?@[\]^_`{|}~])(?=\S+$)[a-zA-Z0-9!"#%&'()*+,\-.\/:;<=>?@[\]^_`{|}~]{8,}$/)
 | 
			
		||||
      .max(128),
 | 
			
		||||
      .max(128)
 | 
			
		||||
      .messages({'string.pattern.base': 'password must have at least 8 characters, one uppercase and one lowercase character, one number and at least one of the following characters:  !"\\#%&\'()*+,-.\\/:;<=>?@[]^_`\\{|}~'}),
 | 
			
		||||
 | 
			
		||||
    level: Joi.string()
 | 
			
		||||
      .valid(...globals.levels),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user