addType #
Adds a new data type, to be used for document validation.
Arguments #
addType (validationType: BaseValidationType): void;| Arguments | Type | Description |
|---|---|---|
validationType | BaseValidationType | An instance of a class extending the abstract BaseValidationType class. Its typeName property names the new type |
Errors #
| Error | Reason |
|---|---|
| validation.types.missing_type_name | The instance has no typeName |
| validation.types.missing_function | validate or validateFieldSpecification is not a function, or, when allowChildren is true, getStrictness is not a function |
| validation.types.already_exists | A 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());