String Rules

string

Checks that value is primitive type and coerces it to the string. Better use more strict rules.

Example:

{
    data: 'any' // validator will pass "data" field
    payload: ['required', 'string']
}

You can treat this rule as modifier.

eq

Error code: 'NOT_ALLOWED_VALUE'

Example:

{ first_name: {'eq': 'Anton'} }

one_of

Error code: 'NOT_ALLOWED_VALUE'

Example:

// new syntax (introduced in v0.4)
{ first_name: {'one_of': ['Anton', 'Igor']} }

// old syntax
{ first_name: {'one_of': [['Anton', 'Igor']]} }

max_length

Error code: 'TOO_LONG'

Example:

{ first_name: { max_length: 10 } }

min_length

Error code: 'TOO_SHORT'

Example:

{ first_name: { min_length: 2 } }

length_between

Error code: 'TOO_LONG' or 'TOO_SHORT'

Example:

{ first_name: { length_between: [2, 10] }

length_equal

Error code: 'TOO_LONG' or 'TOO_SHORT'

Example:

{ first_name: { length_equal: 7 }

like

Error code: 'WRONG_FORMAT'

Example:

{ first_name: { like: '^\w+?$' }
{ first_name: { like: ['^\w+?$', 'i'] } // with flags

Only 'i' flag is currently required by specification.

Be aware that regular expressions can be language dependent. Try to use most common syntax. This rule is experimental as it requires more strict semantics.

results matching ""

    No results matching ""