Field Rules

Koa Validation has a host of rules that can be applied to validate either the body, headers, params or queries sent over a request. Field rules are declared as value to the keys. They can either be declared as strings or objects.

accepted

The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

Usage:

  • { foo: 'accepted' }
  • { foo: { accepted: [] } }

Placeholders:

  • :attribute
  • :value

after

The field under validation must be a value after a given date. The date can be provided in any of the ISO 8601 Date formats + DD-MM-YYYY, DD.MM.YYYY, DD/MM/YYYY, D-M-YYYY, D.M.YYYY, D/M/YYYY, YYYY-MM-DD HH:mm:Z, YYYY-MM-DD HH:mm:ZZ, YYYY-MM-DD HH:mm Z. More formats can be used if the dateFormat Rule is used.

Usage:

  • { christmas: 'after:12/11/2016' }
  • { christmas: { after: ['12/11/2016'] } }

Placeholders:

  • :attribute
  • :value
  • :afterDate

alpha

The field under validation must be entirely alphabetic characters.

Usage:

  • { foo: 'alpha' }
  • { foo: { alpha: [] } }

Placeholders:

  • :attribute
  • :value

alphaDash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

Usage:

  • { foo: 'alphaDash' }
  • { foo: { alphaDash: [] } }

Placeholders:

  • :attribute
  • :value

alphaNumeric

The field under validation must be entirely alpha-numeric characters.

Usage:

  • { foo: 'alphaNumeric' }
  • { foo: { alphaNumeric: [] } }

Placeholders:

  • :attribute
  • :value

before

The field under validation must be a value preceding the given date. The date can be provided in any of the ISO 8601 Date formats + DD-MM-YYYY, DD.MM.YYYY, DD/MM/YYYY, D-M-YYYY, D.M.YYYY, D/M/YYYY, YYYY-MM-DD HH:mm:Z, YYYY-MM-DD HH:mm:ZZ, YYYY-MM-DD HH:mm Z. More formats can be used if the dateFormat Rule is used.

Usage:

  • { easter: 'after:12/11/2016' }
  • { easter: { after: ['12/11/2016'] } }

Placeholders:

  • :attribute
  • :value
  • :beforeDate

between

The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.

Usage:

  • { password: 'between:6,15' }
  • { password: { between: [6,15] } }

Placeholders:

  • :attribute
  • :value
  • :min
  • :max

boolean

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

Usage:

  • { foo: 'boolean' }
  • { foo: { boolean: [] } }

Placeholders:

  • :attribute
  • :value

contains

The field under validation must contain the given string

Usage:

  • { foo: 'contains:bar' }
  • { foo: { contains: ['bar'] } }

Placeholders:

  • :attribute
  • :value

date

The field under validation must be a valid date. The date can be provided in any of the ISO 8601 Date formats + DD-MM-YYYY, DD.MM.YYYY, DD/MM/YYYY, D-M-YYYY, D.M.YYYY, D/M/YYYY, YYYY-MM-DD HH:mm:Z, YYYY-MM-DD HH:mm:ZZ, YYYY-MM-DD HH:mm Z

The field under validation must contain the given string

Usage:

  • { christmas: 'date' }
  • { christmas: { date: [] } }

Placeholders:

  • :attribute
  • :value

dateFormat

The field under validation must match the given format. Incase the format used does not form a logical date, an error will be thrown

Usage:

  • { christmas: 'dateFormat:MMDDYYYY' }
  • { christmas: { date: ['MMDDYYYY'] } }

Placeholders:

  • :attribute
  • :value

different

The field under validation must have a different value than another field. Mention the name of another field in the argument.

Usage:

  • { foo: 'different' }
  • { foo: { different: ['bar'] } }

Placeholders:

  • :attribute
  • :value

digits

The field under validation must be numeric and must have an exact length of value.

Usage:

  • { foo: 'digits:400045' }
  • { foo: { digits: [400045] } }

Placeholders:

  • :attribute
  • :value

digitsBetween

The field under validation must have a length between the given min and max.

Usage:

  • { foo: 'digits:2000,2300' }
  • { foo: { different: [2000,3000] } }

Placeholders:

  • :attribute
  • :value

email

The field under validation must be formatted as an e-mail address.

Usage:

  • { foo: 'email' }
  • { foo: { email: [] } }

Placeholders:

  • :attribute
  • :value

equals

The field under validation must be equal to the given value.

Usage:

  • { foo: 'equals:bar' }
  • { foo: { equals: ['bar'] } }

Placeholders:

  • :attribute
  • :value

in

The field under validation must be included in the given list of values.

Usage:

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

Placeholders:

  • :attribute
  • :value

integer

The field under validation must be an integer.

Usage:

  • { foo: 'integer' }
  • { foo: { integer: [] } }

Placeholders:

  • :attribute
  • :value

ip

The field under validation must be an IP address.

Usage:

  • { foo: 'ip' }
  • { foo: { ip: [] } }

Placeholders:

  • :attribute
  • :value

json

The field under validation must a valid JSON string.

Usage:

  • { foo: 'json' }
  • { foo: { json: [] } }

Placeholders:

  • :attribute
  • :value

max

The field under validation needs to be an integer and should be less than or equal to the maximum value. This rule does not check against the length of the integer but the value of the integer itself.

Usage:

  • { ageOfEmployment: 'max:60' }
  • { ageOfEmployment: { max: [60] } }

Placeholders:

  • :attribute
  • :value

maxLength

The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.

Usage:

  • { name: 'maxLength:8' }
  • { name: { maxLength: [8] } }

Placeholders:

  • :attribute
  • :value

min

The field under validation needs to be an integer and should be greater than or equal to the minimum value. This rule does not check against the length of the integer but the value of the integer itself

Usage:

  • { ageForDrinking: 'min:18' }
  • { ageForDrinking: { min: [18] } }

Placeholders:

  • :attribute
  • :value

minLength

The field under validation must be greater than or equal to a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.

Usage:

  • { name: 'minLength:2' }
  • { name: { minLength: [2] } }

Placeholders:

  • :attribute
  • :value

notContains

The field under validation must not contain the given string

Usage:

  • { foo: 'notContains:bar' }
  • { foo: { notContains: ['bar'] } }

Placeholders:

  • :attribute
  • :value

notIn

The field under validation must not be included in the given list of values.

Usage:

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

Placeholders:

  • :attribute
  • :value

numeric

The field under validation must be a numeric value. This could also include a numeric string or decimals.

Usage:

  • { epoch:'numeric' }
  • { epoch: { numeric: [] } }

Placeholders:

  • :attribute
  • :value

regex

The field under validation must match the given regular expression.

Usage:

  • { tenDigits: { notIn: [/^\d{10}$/g] } }

Placeholders:

  • :attribute
  • :value

same

The given field must match the field under validation.

Usage:

  • { foo:same:'bar' }
  • { foo: { same: ['bar'] } }

Placeholders:

  • :attribute
  • :value

string

The field under validation must be a string.

Usage:

  • { foo:'string' }
  • { foo: { string: [] } }

Placeholders:

  • :attribute
  • :value

timezone

The field under validation must be a valid timezone. The validation is handled by the Javascript moment-timezone library.

Usage:

  • { tz:'timezone' }
  • { tz: { timezone: [] } }

Placeholders:

  • :attribute
  • :value

url

The field under validation must be a valid URL

Usage:

  • { website:'url' }
  • { website: { url: [] } }

Placeholders:

  • :attribute
  • :value