Skip to content

Latest commit

 

History

History
997 lines (809 loc) · 21.7 KB

rules.md

File metadata and controls

997 lines (809 loc) · 21.7 KB

Table of Contents

JSON-DATA-VALIDATOR Rules

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)

Composite Rule

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.

Configuration

JSON

{
   "type": "COMPOSITE",
   "algorithm": "all|any",
   "subrules": [
      ...
   ]
}
  • type: Type must be COMPOSITE
  • algorithm: this can be all or any with the behaviour below:
    • all: this rule will return valid if all of its sub-rules returns valid
    • any: this rule will return valid if any of its sub-rules returns valid
  • subrules: the list of subrules composing this composite rule. A subrule can be a composite rule itself.

Builder

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();

ExactValue Rule

This rule returns valid only if the field to be evaluated has exactly the specified value.

Configuration

JSON

{
   "type": "EXACT_VALUE",
   "value": "value to be checked"
}
  • type: Type must be EXACT_VALUE
  • value: the value that we want to check

Builder

import {RuleBuilder} from "./RuleBuilder";

// The field value must be *value*
RuleBuilder.exactValue.withValue('value').build();

Example

JSON

// 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);

Builder

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);

MaxLength Rule

Validates the length of the configured field

Configuration

{
   "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'

Required Rule

Checks that a particular field has a value

Configuration

JSON

{
   "type": "REQUIRED",
}
Example
// 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);

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.required.build();
Example
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);

RequiredBy Rule

Checks that the field has a value if the field identified by the specified path has a value

Configuration

JSON

{
   "type": "REQUIREDBY", 
   "parent": "pathtoparent"
}

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.isRequiredBy.withParent('pathtoparent').build();

Example

JSON

// 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);

Builder

// 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);

Contains Rule

Checks that a field value contains the configured value.

Configuration

{
  "type": "contains",
  "seed": "value that must be contained"
}
  • type: Type must be contains
  • seed: the value that must be contained by the field value

isAfter Rule

Returns valid if the field value is a date that comes after the configured date.

Configuration

{
  "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'

isAlpha Rule

Returns a valid result if the field value is an alphabetic string.

Configuration

{
  "type": "isAlpha",
  "locale": "the locale" 
}
  • type: Type must be isAlpha
  • locale: the locale. Defaults to en-US. Check here for the allowed values

isAlphaNumeric Rule

Returns a valid result if the field value is an alphanumeric string.

Configuration

{
  "type": "isAlphaNumeric",
  "locale": "the locale" 
}
  • type: Type must be isAlphaNumeric
  • locale: the locale. Defaults to en-US. Check here for the allowed values

isAscii Rule

Returns a valid result if the field value is an ASCII string.

Configuration

{
  "type": "isAscii",
}
  • type: Type must be isAscii

isBase32 Rule

Returns a valid result if the field value is an Base32 string.

Configuration

{
  "type": "isBase32",
}
  • type: Type must be isBase32

isBase64 Rule

Returns a valid result if the field value is an Base64 string.

Configuration

JSON

{
  "type": "isBase64",
}
  • type: Type must be isBase64

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.isBase64.build();

isBefore Rule

Returns valid if the field value is a date that comes before the configured one.

Configuration

{
  "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'

isBIC Rule

Returns a valid result if the field value is a valid BIC string.

Configuration

{
  "type": "isBIC",
}
  • type: Type must be isBIC

isBoolean Rule

Returns a valid result if the field value is a valid boolean string (true/false).

Configuration

{
  "type": "isBoolean",
}
  • type: Type must be isBoolean

isCreditCard Rule

Returns a valid result if the field value is a valid credit card string.

Configuration

{
  "type": "isCreditCard",
}
  • type: Type must be isCreditCard

isDataURI Rule

Returns a valid result if the field value is a valid data URI string (example: data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D).

Configuration

{
  "type": "isDataURI",
}
  • type: Type must be isDataURI

isDecimal Rule

Returns a valid result if the field value is a valid decimal value.

Configuration

{
  "type": "isDecimal",
}
  • type: Type must be isDecimal

isDivisibleBy Rule

Returns a valid result if the field value is a number divisible by the specified number.

Configuration

{
  "type": "isDivisibleBy",
  "number": "3"
}
  • type: Type must be isDivisibleBy
  • number: the field value must be divisible by this number

isEmail Rule

Returns a valid result if the field value is a valid email address.

Configuration

{
  "type": "isEmail",
}
  • type: Type must be isEmail

isFQDN Rule

Returns a valid result if the field value is a valid FQDN string.

Configuration

{
  "type": "isFQDN",
}
  • type: Type must be isFQDN

isHASH Rule

Returns a valid result if the field value is a valid HASH string. Supported algorithms are sha1, sha256, sha512, MD5.

Configuration

{
  "type": "isHASH",
  "algorithm": "sha1"
}
  • type: Type must be isHASH
  • algorithm: The hashing algorithm. Defaults to sha1 if not specified

isHexadecimal Rule

Returns a valid result if the field value is a valid HEX string.

Configuration

{
  "type": "isHexadecimal",
}
  • type: Type must be isHexadecimal

isHexColor Rule

Returns a valid result if the field value is a valid HEX color string.

Configuration

{
  "type": "isHexColor",
}
  • type: Type must be isHexColor

isIn Rule

Returns a valid result if the field value one of the specified values.

Configuration

JSON

{
  "type": "isIn",
  "values": "value1,value2,value3"
}
  • type: Type must be isIn
  • values: a string containing all the values (comma separated)

Builder

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');

isInt Rule

Returns a valid result if the field value is an Int.

Configuration

{
  "type": "isInt",
}
  • type: Type must be isInt

isIP Rule

Returns a valid result if the field value is an IP address (v4 or V6).

Configuration

{
  "type": "isIP",
}
  • type: Type must be isIP

isIPRange Rule

Returns a valid result if the field value is an IP Range address (v4).

Configuration

{
  "type": "isIPRange",
}
  • type: Type must be isIPRange

isISBN Rule

Returns a valid result if the field value is a valid ISBN.

Configuration

{
  "type": "isISBN",
}
  • type: Type must be isISBN

isISIN Rule

Returns a valid result if the field value is a valid ISIN.

Configuration

{
  "type": "isISIN",
}
  • type: Type must be isISIN

isISO31661Alpha2 Rule

Returns a valid result if the field value is a valid ISO31661 Alpha2 string.

Configuration

{
  "type": "isISO31661Alpha2",
}
  • type: Type must be isISO31661Alpha2

isISO31661Alpha3 Rule

Returns a valid result if the field value is a valid ISO31661 Alpha3 string.

Configuration

{
  "type": "isISO31661Alpha3",
}
  • type: Type must be isISO31661Alpha3

isISO8601 Rule

Returns a valid result if the field value is a valid ISO8601 string.

Configuration

{
  "type": "isISO8601",
}
  • type: Type must be isISO8601

isISSN Rule

Returns a valid result if the field value is a valid ISSN string.

Configuration

{
  "type": "isISSN",
}
  • type: Type must be isISSN

isISRC Rule

Returns a valid result if the field value is a valid ISRC string.

Configuration

{
  "type": "isISRC",
}
  • type: Type must be isISRC

isRFC3339 Rule

Returns a valid result if the field value is a valid RFC3339 date.

Configuration

{
  "type": "isRFC3339",
}
  • type: Type must be isRFC3339

isJSON Rule

Returns a valid result if the field value is a valid JSON string.

Configuration

{
  "type": "isJSON",
}
  • type: Type must be isJSON

isJWT Rule

Returns a valid result if the field value is a valid JWT string.

Configuration

{
  "type": "isJWT",
}
  • type: Type must be isJWT

isLatLong Rule

Returns a valid result if the field value is a valid Latitude/Longitude string.

Configuration

{
  "type": "isLatLong",
}
  • type: Type must be isLatLong

isMACAddress Rule

Returns a valid result if the field value is a valid MAC Address.

Configuration

{
  "type": "isMACAddress",
}
  • type: Type must be isMACAddress

isMagnetURI Rule

Returns a valid result if the field value is a valid magnet URI.

Configuration

{
  "type": "isMagnetURI",
}
  • type: Type must be isMagnetURI

isMD5 Rule

Returns a valid result if the field value is a valid MD5 string

Configuration

{
  "type": "isMD5",
}
  • type: Type must be isMD5

isMimeType Rule

Returns a valid result if the field value is a valid mime type

Configuration

{
  "type": "isMimeType",
}
  • type: Type must be isMimeType

isMongoId Rule

Returns a valid result if the field value is a valid mongo id string

Configuration

{
  "type": "isMongoId",
}
  • type: Type must be isMongoId

isNumeric Rule

Returns a valid result if the field value is a valid numeric string

Configuration

{
  "type": "isNumeric",
}
  • type: Type must be isNumeric

isOctal Rule

Returns a valid result if the field value is a valid octal string

Configuration

{
  "type": "isOctal",
}
  • type: Type must be isOctal

isPort Rule

Returns a valid result if the field value is a valid port number

Configuration

{
  "type": "isPort",
}
  • type: Type must be isPort

isUUID Rule

Returns a valid result if the field value is a valid UUID

Configuration

{
  "type": "isUUID",
  "version": "all"
}
  • type: Type must be isUUID
  • version can be 3, 4, 5 or all. If not specified, defaults to all

isValidUrl Rule

Returns a valid result if the field value is a valid URL

Configuration

JSON

{
  "type": "VALID_URL"
}

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.isValidUrl().build();
  • type: Type must be VALID_URL

Length Rule

Returns a valid result if the field length is exactly the specified value

Configuration

JSON

{
  "type": "LENGTH",
  "length": 10
}
  • type: Type must be LENGTH
  • length: ho many character should compose the field value

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.length.withLength(10);

matches Rule

Returns a valid result if the field value matches the provided regexp

Configuration

JSON

{
  "type": "matches",
  "pattern": "abc"
}
  • type: Type must be matches
  • pattern a regexp to be matched

Builder

import {RuleBuilder} from "./RuleBuilder";

const rule = RuleBuilder.matches('abc').build();