- JSON-DATA-VALIDATOR Rules
- Composite Rule
- ExactValue Rule
- MaxLength Rule
- Required Rule
- RequiredBy Rule
- Contains Rule
- isAfter Rule
- isAlpha Rule
- isAlphaNumeric Rule
- isAscii Rule
- isBase32 Rule
- isBase64 Rule
- isBefore Rule
- isBIC Rule
- isBoolean Rule
- isCreditCard Rule
- isDataURI Rule
- isDecimal Rule
- isDivisibleBy Rule
- isEmail Rule
- isFQDN Rule
- isHASH Rule
- isHexadecimal Rule
- isHexColor Rule
- isIn Rule
- isInt Rule
- isIP Rule
- isIPRange Rule
- isISBN Rule
- isISIN Rule
- isISO31661Alpha2 Rule
- isISO31661Alpha3 Rule
- isISO8601 Rule
- isISSN Rule
- isISRC Rule
- isRFC3339 Rule
- isJSON Rule
- isJWT Rule
- isLatLong Rule
- isMACAddress Rule
- isMagnetURI Rule
- isMD5 Rule
- isMimeType Rule
- isMongoId Rule
- isNumeric Rule
- isOctal Rule
- isPort Rule
- isUUID Rule
- isValidUrl Rule
- Length Rule
- matches Rule
There are 2 properties that are common to all the rules configurations:
errorMessage
: use this key to provide a custom error message. If it is not provided, a default one is returned.path
: to enforce a fixed key to be evaluated by this rule. It can be useful when composing complex validation logics (see this for an example)
A composite rule is a rule whose result depends on the executions of a list of subrules. Each subrule can be a composite rule too, giving the ability to compose complex trees.
{
"type": "COMPOSITE",
"algorithm": "all|any",
"subrules": [
...
]
}
- type: Type must be COMPOSITE
- algorithm: this can be
all
orany
with the behaviour below:- all: this rule will return
valid
if all of its sub-rules returnsvalid
- any: this rule will return
valid
if any of its sub-rules returnsvalid
- all: this rule will return
- subrules: the list of subrules composing this composite rule. A subrule can be a composite rule itself.
import {RuleBuilder} from "./RuleBuilder";
// Builds a composite rule that must match all of its subrules.
// withSubRule can be called many times to add many subrules
const ruleAll = RuleBuilder.composite.all().withSubRule(...).build();
// Builds a composite rule that must match any of its subrules
// withSubRule can be called many times to add many subrules
const ruleAny = RuleBuilder.composite.any().withSubRule(...).build();
This rule returns valid only if the field to be evaluated has exactly the specified value.
{
"type": "EXACT_VALUE",
"value": "value to be checked"
}
- type: Type must be EXACT_VALUE
- value: the value that we want to check
import {RuleBuilder} from "./RuleBuilder";
// The field value must be *value*
RuleBuilder.exactValue.withValue('value').build();
// Validator configuration
const validatorConfig = {
ruleSets: [{
fields: {
myfield: [{
type: 'EXACT_VALUE',
value: '11'
}]
}
}]
}
const data = {
myfield: '56'
}
const validationResult = new Validator(validatorConfig).validate(data);
console.log('Validation result:', validationResult);
import {validatorBuilder} from "./ValidatorBuilder";
import {RuleBuilder} from "./RuleBuilder";
const validator = validatorBuilder()
.newRule()
.withField('myField')
.validate(RuleBuilder.exactValue.withValue('11').build()).build();
const data = {
myfield: '56'
}
const validationResult = new Validator(validatorConfig).validate(data);
console.log('Validation result:', validationResult);
Validates the length of the configured field
{
"type": "MAXLENGTH",
"maxlength": "12"
}
- type: Type must be MAXLENGTH
- value: if the field value is longer than this value, this check will return
false
The above configuration checks that the field has a maximum length of '12'
Checks that a particular field has a value
{
"type": "REQUIRED",
}
// Validator configuration
const validatorConfig = {
ruleSets: [{
fields: {
myfield2: [{
type: 'REQUIRED',
}]
}
}]
}
const data = {
myfield1: '56',
myfield2: 'test'
}
const validationResult = new Validator(validatorConfig).validate(data);
console.log('Validation result:', validationResult);
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.required.build();
import {validatorBuilder} from "./ValidatorBuilder";
import {RuleBuilder} from "./RuleBuilder";
const data = {
myfield1: '56',
myfield2: 'test'
}
const validationResult = validatorBuilder
.newRule()
.withField('myfield2').validate(RuleBuilder.required().build()).build().validate(data);
console.log('Validation result:', validationResult);
Checks that the field has a value if the field identified by the specified path has a value
{
"type": "REQUIREDBY",
"parent": "pathtoparent"
}
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.isRequiredBy.withParent('pathtoparent').build();
// Validator configuration
import {Validator} from "./Validator";
// parent.parentfield must be non-empty if myfield2 is non-empty
const validatorConfig = {
ruleSets: [{
fields: {
myfield2: [{
type: 'REQUIREDBY',
parent: 'parent.parentfield'
}]
}
}]
}
const data = {
parent: {
parentfield: 'testparent'
},
myfield2: 'test'
}
const validationResult = new Validator(validatorConfig).validate(data);
console.log('Validation result:', validationResult);
// Validator configuration
import {validatorBuilder} from "./ValidatorBuilder";
import {RuleBuilder} from "./RuleBuilder";
const data = {
parent: {
parentfield: 'testparent'
},
myfield2: 'test'
}
// parent.parentfield must be non-empty if myfield2 is non-empty
const validationResult = validatorBuilder()
.newRule()
.withField('myfield2')
.validate(RuleBuilder.isRequiredBy.withParent('parent.parentfield').build())
.build()
.validate(data)
console.log('Validation result:', validationResult);
Checks that a field value contains the configured value.
{
"type": "contains",
"seed": "value that must be contained"
}
- type: Type must be contains
- seed: the value that must be contained by the field value
Returns valid if the field value is a date that comes after the configured date.
{
"type": "isAfter",
"value": "12/12/2020",
"format": "DD/MM/YYYY"
}
- type: Type must be isAfter
- value: the reference date. The date format must comply with the value of the configured
format
- format: the pattern to be used to parse the date. If not specified, defaults to 'DD/MM/YYY'
Returns a valid
result if the field value is an alphabetic string.
{
"type": "isAlpha",
"locale": "the locale"
}
- type: Type must be isAlpha
- locale: the locale. Defaults to
en-US
. Check here for the allowed values
Returns a valid
result if the field value is an alphanumeric string.
{
"type": "isAlphaNumeric",
"locale": "the locale"
}
- type: Type must be isAlphaNumeric
- locale: the locale. Defaults to
en-US
. Check here for the allowed values
Returns a valid
result if the field value is an ASCII string.
{
"type": "isAscii",
}
- type: Type must be isAscii
Returns a valid
result if the field value is an Base32 string.
{
"type": "isBase32",
}
- type: Type must be isBase32
Returns a valid
result if the field value is an Base64 string.
{
"type": "isBase64",
}
- type: Type must be isBase64
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.isBase64.build();
Returns valid if the field value is a date that comes before the configured one.
{
"type": "isBefore",
"value": "12/12/2020",
"format": "DD/MM/YYYY"
}
- type: Type must be isBefore
- value: the reference date. The date format must comply with the value of the configured
format
- format: the pattern to be used to parse the date. If not specified, defaults to 'DD/MM/YYY'
Returns a valid
result if the field value is a valid BIC string.
{
"type": "isBIC",
}
- type: Type must be isBIC
Returns a valid
result if the field value is a valid boolean string (true/false).
{
"type": "isBoolean",
}
- type: Type must be isBoolean
Returns a valid
result if the field value is a valid credit card string.
{
"type": "isCreditCard",
}
- type: Type must be isCreditCard
Returns a valid
result if the field value is a valid data URI string (example: data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
).
{
"type": "isDataURI",
}
- type: Type must be isDataURI
Returns a valid
result if the field value is a valid decimal value.
{
"type": "isDecimal",
}
- type: Type must be isDecimal
Returns a valid
result if the field value is a number divisible by the specified number.
{
"type": "isDivisibleBy",
"number": "3"
}
- type: Type must be isDivisibleBy
- number: the field value must be divisible by this number
Returns a valid
result if the field value is a valid email address.
{
"type": "isEmail",
}
- type: Type must be isEmail
Returns a valid
result if the field value is a valid FQDN string.
{
"type": "isFQDN",
}
- type: Type must be isFQDN
Returns a valid
result if the field value is a valid HASH string. Supported algorithms are sha1
, sha256
, sha512
, MD5
.
{
"type": "isHASH",
"algorithm": "sha1"
}
- type: Type must be isHASH
- algorithm: The hashing algorithm. Defaults to
sha1
if not specified
Returns a valid
result if the field value is a valid HEX string.
{
"type": "isHexadecimal",
}
- type: Type must be isHexadecimal
Returns a valid
result if the field value is a valid HEX color string.
{
"type": "isHexColor",
}
- type: Type must be isHexColor
Returns a valid
result if the field value one of the specified values.
{
"type": "isIn",
"values": "value1,value2,value3"
}
- type: Type must be isIn
- values: a string containing all the values (comma separated)
import {RuleBuilder} from "./RuleBuilder";
// The following 3 syntax will result in the same rule
const rule = RuleBuilder.isIn.withValues('value1', 'value2', 'value3');
const rule2 = RuleBuilder.isIn.withValue('value1').withValue('value2').withValue('value3');
const rule3 = RuleBuilder.isIn.withValue('value1').withValues('value2', 'value3');
Returns a valid
result if the field value is an Int.
{
"type": "isInt",
}
- type: Type must be isInt
Returns a valid
result if the field value is an IP address (v4 or V6).
{
"type": "isIP",
}
- type: Type must be isIP
Returns a valid
result if the field value is an IP Range address (v4).
{
"type": "isIPRange",
}
- type: Type must be isIPRange
Returns a valid
result if the field value is a valid ISBN.
{
"type": "isISBN",
}
- type: Type must be isISBN
Returns a valid
result if the field value is a valid ISIN.
{
"type": "isISIN",
}
- type: Type must be isISIN
Returns a valid
result if the field value is a valid ISO31661 Alpha2 string.
{
"type": "isISO31661Alpha2",
}
- type: Type must be isISO31661Alpha2
Returns a valid
result if the field value is a valid ISO31661 Alpha3 string.
{
"type": "isISO31661Alpha3",
}
- type: Type must be isISO31661Alpha3
Returns a valid
result if the field value is a valid ISO8601 string.
{
"type": "isISO8601",
}
- type: Type must be isISO8601
Returns a valid
result if the field value is a valid ISSN string.
{
"type": "isISSN",
}
- type: Type must be isISSN
Returns a valid
result if the field value is a valid ISRC string.
{
"type": "isISRC",
}
- type: Type must be isISRC
Returns a valid
result if the field value is a valid RFC3339 date.
{
"type": "isRFC3339",
}
- type: Type must be isRFC3339
Returns a valid
result if the field value is a valid JSON string.
{
"type": "isJSON",
}
- type: Type must be isJSON
Returns a valid
result if the field value is a valid JWT string.
{
"type": "isJWT",
}
- type: Type must be isJWT
Returns a valid
result if the field value is a valid Latitude/Longitude string.
{
"type": "isLatLong",
}
- type: Type must be isLatLong
Returns a valid
result if the field value is a valid MAC Address.
{
"type": "isMACAddress",
}
- type: Type must be isMACAddress
Returns a valid
result if the field value is a valid magnet URI.
{
"type": "isMagnetURI",
}
- type: Type must be isMagnetURI
Returns a valid
result if the field value is a valid MD5 string
{
"type": "isMD5",
}
- type: Type must be isMD5
Returns a valid
result if the field value is a valid mime type
{
"type": "isMimeType",
}
- type: Type must be isMimeType
Returns a valid
result if the field value is a valid mongo id string
{
"type": "isMongoId",
}
- type: Type must be isMongoId
Returns a valid
result if the field value is a valid numeric string
{
"type": "isNumeric",
}
- type: Type must be isNumeric
Returns a valid
result if the field value is a valid octal string
{
"type": "isOctal",
}
- type: Type must be isOctal
Returns a valid
result if the field value is a valid port number
{
"type": "isPort",
}
- type: Type must be isPort
Returns a valid
result if the field value is a valid UUID
{
"type": "isUUID",
"version": "all"
}
- type: Type must be isUUID
- version can be
3
,4
,5
orall
. If not specified, defaults toall
Returns a valid
result if the field value is a valid URL
{
"type": "VALID_URL"
}
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.isValidUrl().build();
- type: Type must be VALID_URL
Returns a valid
result if the field length is exactly the specified value
{
"type": "LENGTH",
"length": 10
}
- type: Type must be LENGTH
- length: ho many character should compose the field value
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.length.withLength(10);
Returns a valid
result if the field value matches the provided regexp
{
"type": "matches",
"pattern": "abc"
}
- type: Type must be matches
- pattern a regexp to be matched
import {RuleBuilder} from "./RuleBuilder";
const rule = RuleBuilder.matches('abc').build();