addType #

Adds a new data type, to be used for document validation.

Arguments #

addType (validationType: BaseValidationType): void;

ArgumentsTypeDescription
validationType
BaseValidationType
An instance of a class extending the abstract BaseValidationType class. Its typeName property names the new type

Errors #

ErrorReason
validation.types.missing_type_nameThe instance has no typeName
validation.types.missing_functionvalidate or validateFieldSpecification is not a function, or, when allowChildren is true, getStrictness is not a function
validation.types.already_existsA type with the same typeName is already registered

Usage #

class ZipCodeType extends context.constructors.BaseValidationType {
  constructor () {
    super();
    this.typeName = 'zipCode';
    this.allowChildren = false;
  }

  validate (opts, field, errors) {
    return /^[0-9]{5}$/.test(field);
  }
}

context.accessors.validation.addType(new ZipCodeType());