Rules Examples

Simple registration data (demo)

{
    name: 'required',
    email: ['required', 'email'],
    gender: { one_of: ['male', 'female'] },
    phone: {max_length: 10},
    password: ['required', {min_length: 10} ]
    password2: { equal_to_field: 'password' }
}
  • 'required' is a shorter form of { 'required': [] }
  • {max_length: 10} is a shorter form of {max_length: [10]}
  • See "How it works" section

Simple validation of nested object (demo)

{
    name: 'required',
    phone: {max_length: 10},
    address: { 'nested_object': {
        city: 'required',
        zip: ['required', 'positive_integer']
    }}
}
  • {nested_object: {}} is a shorter form of {nested_object: [{}]}
  • See "How it works" section

Simple list validation (demo)

{
    order_id: ['required', 'positive_integer'],
    product_ids: { 'list_of': [ 'required',  'positive_integer' ] }
}

Validating list of objects (demo)

{
    order_id: ['required', 'positive_integer'],
    products: [ 'not_empty_list', { 'list_of_objects': {
        product_id: ['required','positive_integer'],
        quantity: ['required', 'positive_integer']
    }}]
}

Validating list of different objects (demo)

{
    order_id: ['required', 'positive_integer'],
    products: ['required', { 'list_of_different_objects': [
        'product_type', {
            material: {
                product_type: 'required',
                material_id: ['required', 'positive_integer'],
                quantity: ['required', {'min_number': 1} ],
                warehouse_id: 'positive_integer'
            },
            service: {
                product_type: 'required',
                name: ['required', {'max_length': 10} ]
            }
        }
    ]}]
}

Output modification

{
    email: ['trim', 'required', 'email', 'to_lc']
}
  • "trim" removes leading and trailing spaces. (skips empty values and object references)
  • "to_lc" transforms string to lower case. (skips empty values and object references)

You can create pipeline with any modifiers you like.

results matching ""

    No results matching ""