Built-in Rules

after

It checks that it is after the specified date.

Parameters:

  • string | number | Date

Example:

{
  after: new Date(),
}

alpha

Must be entirely alphabetic characters.

Parameters:

  • None

Example:

{
  alpha: true,
}

alphaDash

Must be alphabetic characters and dashes and underscores.

Parameters:

  • None

Example:

{
  alphaDash: true,
}

alphaNumeric

Must be alphanumeric and underscore and dash.

Parameters:

  • None

Example:

{
  alphaNumeric: true,
}

array

Check if the value is Array.

Parameters:

  • None

Example:

{
  array: true,
}

before

It checks that it is before the specified date.

Parameters:

  • string | number | Date

Example:

{
  before: new Date(),
}

between

For character strings and arrays, check the length attribute, if it is a number, check that the value is within the specified range.

Parameters:

  • min: number
  • max: number

Example:

{
  between: {
    min: 10,
    max: 20,
  },
}

betweenWith

For character strings and arrays, check the length attribute, if it is a number, make sure that they are within the field specified by the min andmax options.

Parameters:

  • min: string
  • max: string

Example:

{
  betweenWith: {
    min: 'minKey',
    max: 'maxKey',
  },
}

date

Check if the value is Date.

Parameters:

  • None

Example:

{
  date: true,
}

dateFormat

It checks whether the value matches the specified date format.

Parameters:

  • string

Example:

{
  dateFormat: 'YYYY-MM-DD HH:mm:ss',
}

Reference:

different

It checks whether the specified field and value are different.

Parameters:

  • string

Example:

{
  different: 'otherField',
}

email

Check that the email address format.

Parameters:

  • None

Example:

{
  email: true,
}

equals

It checks whether it matches the specified value.

Parameters:

  • any

Example:

{
  equals: 'Foo',
}

falsy

Check if the value is falsy.

Valid:

  • false
  • "false"
  • 0
  • "0"
  • "NO" ("No", "no")

Parameters:

  • None

Example:

{
  falsy: true,
}

float

Check if the value is a float number.

Parameters:

  • None

Example:

{
  float: true,
}

format

It checks whether it conforms to the specified format.

Parameters:

  • RegExp

Example:

{
  format: /^foo.*bar$/,
}

in

It checks whether the same value exists in the specified array.

Parameters:

  • any[]

Example:

{
  in: ['foo', 'bar', 'baz'],
}

integer

Check if the value is a integer.

Parameters:

  • None

Example:

{
  integer: true,
}

lowercase

Check if the value is a lowercase.

Parameters:

  • None

Example:

{
  lowercase: true,
}

max

For character strings and arrays, check the length attribute, for numeric values, check that the value does not exceed the specified number.

Parameters:

  • number

Example:

{
  max: 120,
}

maxWith

For character strings and arrays, check the length attribute, for numeric values, make sure that it does not exceed the numerical value of the specified field. Good approach if you need to fit into the size of the table fields. Look here for more clarity.

Parameters:

  • string

Example:

{
  maxWith: 'maxKey',
}

min

For character strings and arrays, check the length attribute, and for numeric values, check that the value exceeds the specified number.

Parameters:

  • number

Example:

{
  min: 24,
}

minWith

For character strings and arrays, check the length attribute, and for numeric values, make sure that it is larger than the numeric value of the specified field

Parameters:

  • string

Example:

{
  minWith: 'minKey',
}

natural

Check if the value is a natural number.
By default 0 returns true, but invalidates 0 by passing false to disallowZero.

Parameters:

  • disallowZero?: boolean

Example:

{
  natural: { disallowZero: true },
}

notIn

It checks whether the same value not exists in the specified array.

Parameters:

  • any[]

Example:

{
  notIn: ['foo', 'bar', 'baz'],
}

number

Check if the value is number.

Parameters:

  • None

Example:

{
  number: true,
}

numeric

Check if the value is numeric.

Parameters:

  • None

Example:

{
  numeric: true,
}

object

Check if the value is plain object.

Parameters:

  • None

Example:

{
  object: true,
}

present

It checks whether the field exists, but it pass even if the value is empty.

Parameters:

  • None

Example:

{
  present: true,
}

required

Check the field exists and the value is not empty.

Parameters:

  • None

Example:

{
  required: true,
}

same

It checks whether the specified field and value are same.

Parameters:

  • string

Example:

{
  same: 'otherField',
}

size

For character strings and arrays, check the length attribute, if it is a number, check that the value is match the specified number.

Parameters:

  • number

Example:

{
  size: 48,
}

string

Check if the value is string.

Parameters:

  • None

Example:

{
  string: true,
}

time

Check if the time is in the correct format. (Format: "HH:mm:ss")

Parameters:

  • None

Example:

{
  time: true,
}

truthy

Check if the value is truthy.

Valid:

  • true
  • "true"
  • "YES" ("Yes", "yes")
  • 1
  • "1"

Parameters:

  • None

Example:

{
  truthy: true,
}

uppercase

Check if the value is a uppercase.

Parameters:

  • None

Example:

{
  uppercase: true,
}

url

Check if the value is valid URL format.

Parameters:

  • protocols: string[]
  • require_protocol: boolean
  • require_tld: boolean
  • require_host: boolean
  • require_valid_protocol: boolean
  • allow_underscores: boolean
  • host_whitelist: boolean
  • host_blacklist: boolean
  • allow_trailing_dot: boolean
  • allow_protocol_relative_urls: boolean

Example:

{
  url: true,
}

// -- or --

{
  // This parameters is default.
  url: {
    protocols: ['http', 'https', 'ftp'],
    require_tld: true,
    require_protocol: true,
    require_host: true,
    require_valid_protocol: true,
    allow_underscores: false,
    allow_trailing_dot: false,
    allow_protocol_relative_urls: false,
  },
}

Reference:

results matching ""

    No results matching ""