@domain-schema/validation v0.0.36
Domain Schema Validation
Installation
yarn add @domain-schema/validation
or
npm install @domain-schema/validation
Validation
Domain Validation intended to check values whether they comply with structure of domain schema fields.
Usage
Firstly import DomanValidator
from domain-schema validation package
import DomainValidator from '@domain-schema/validation'
Then call DomanValidator
static method validate
DomainValidator.validate(schema, values)
With such params:
schema
-DomainSchema
instancevalues
- Formatted object from form or another source with values whichDomainValidator
will check on conformity ofDomainSchema
key - Field name correspond to schema field value - Entity which must pass validation
You can register domain-schema field validation using following approaches:```js { name : "Jack" } ```
- Type defining
isAdmin: { type: Boolean }
- Build-in validators
- Custom validators
For more accurate validation you could use several approaches simultaneously
Built-in validators
optional
- By default, all keys are required. Setoptional: true
to change that.name = { ... optional: true, ... }
matches
- Checks if the value matches some specific field
password = { ... }; passwordConfirmation = { ... matches: 'password', ... }
max
- Checks if the value or value length does not exceed a specified number (works with both numbers and strings)
// Checks length if type is String... name = { ... type: String, max: 6, ... } // ...and value when type is Number age = { ... type: Number, max: 16, ... }
min
- Checks if the value or value length not less then a specified number (works with both numbers and strings)
// Checks length if type is String... name = { ... type: String, min: 6, ... } // ...and value when type is Number age = { ... type: Number, min: 16, ... }
email
- Checks if the value corresponds to an email
name = { ... email: true, ... }
alphaNumeric
- Checks if the value consists of alphanumeric characters
text = { ... alphaNumeric: true, ... }
phoneNumber
- Checks if the value corresponds to a phone number
phone = { ... phoneNumber: true, ... }
equals
- Checks if the value equals to a specific value
role = { ... equals: 'admin', ... }
Nested Schema Validation
Be careful when establishing relation via schema field on your domain schema
class Product extends Schema {
__ = { name: 'Product', tablePrefix: '' };
id = DomainSchema.Int;
name = {
type: String,
searchText: true
};
category = {
type: Category
};
}
class Category extends Schema {
__ = { name: 'Category', tablePrefix: '' };
id = DomainSchema.Int;
name = {
type: String,
searchText: true,
};
products = {
type: [Product]
};
}
In this case domainValidator
also will check all fields of nested Category
schema and then Product again.
So object must have the following structure in order to pass validation
{
id : 1,
name : 'Iphone X'
category : {
id : 1,
name : 'Phones',
products : ...
}
}
To prevent check of nested schema use blackbox
attribute
products = {
type: [Product],
blackbox: true
};
Now the following object will pass validation
{
id : 1,
name : 'Iphone X'
category : {
id : 1,
name : 'Phones'
}
}
Customizing Validation Messages
Validation error messages can be overridden by defining messages in one place as a single
object and passing it to the setValidationMessages
function.
In that object the keys
are validator names and values can be either strings or functions, as follows:
- Values are strings:
import DomainValidation from '@domain-schema/validation';
DomainValidation.setValidationMessages({
required: 'This field is required',
phoneNumber: 'Error! Not a phone number!'
});
- Values are callback functions:
import DomainValidation from '@domain-schema/validation';
DomainValidation.setValidationMessages({
required: ({fieldName}) => {
return `Field '${fieldName}' is required`
},
phoneNumber: ({values, fieldName}) => {
return `Error! '${values[fieldName]}' is not a phone number`
}
});
Callback functions, in turn, get object with the following properties:
values
- the validation objectfieldName
- the current field nameschema
- the domain schema definition object
We can also define a custom validation error message for a specific field right in the schema:
...
name = {
type: String,
...
required: {
value: true,
msg: 'Required Name'
}
}
...
Custom validators
A user is able to add any number of custom validators whenever it need by passing functions to the validators
property of a schema field:
password = {
type: String,
...
validators: [(value, values) => {
return value.length > 3 ? undefined : 'Must Be more than 3 characters';
}]
};
Validation callback function gets the following params:
value
- the value for being validatedvalues
- the form object
NOTE: validation function must return a string with an error message or undefined
.
License
Copyright © 2018 SysGears INC. This source code is licensed under the MIT license.