Skip to main content

Validation

Introduction

Vania offers various approaches to validate incoming data in your application. While the most common method is using the validate method on HTTP requests, Vania supports other validation techniques as well.

With a comprehensive set of validation rules, Vania enables you to validate data conveniently, This guide will cover each validation rule extensively, ensuring you are well-acquainted with Vania's validation capabilities.

Simple Validation Example

Future<Response> index(Request req) async {
req.validate({
'name': 'required|string|alpha',
'email' : 'required|email',
});
}

In this example, the name field is required, must be a string, and should only contain alphabetic characters. The email field is required and must be a valid email address.

Validation with Custom Messages

You can also provide custom error messages for validation rules. Here’s how you can do it:

Future<Response> index(Request req) async {
req.validate({ 'name': 'required|string|alpha', 'email' : 'required|email', },
{
'name.required' :'Name is required',
'name.string' :'Name must be a string',
'name.alpha' :'Name must contain only alphabetic characters',
'email.required':'Email is required',
'email.email':'Invalid email format'
});
}

In this example, custom messages are provided for each validation rule, making it easier to understand and handle validation errors.

Nested Validation

Nested Validation Example with JSON Data

Validation

req.validate({
'address.name': 'required',
'address.type': 'required|numeric',
});

Request body

{
"address" : {
"name" : "Vania",
"type" : "Framework",
}
}

Nested Validation with Array

Validation

req.validate({
'products.*.item': 'required',
'products.*.options.*.price': 'required',
});

Request body

{
"products" : [
{
"item": "Item 1",
"options": [
{ "price" : "22000","color":"blue"},
]
},
{
"item": "Item 2",
"options": [
{ "price" : "38000","color":"blue"},
]
}
]
}
tip

Validation rules are applicable within route closures or controller methods, providing flexibility and ease of use.

Rules

required: Ensures that the field is present and not empty.

  • Example: 'name': 'required'
req.validate({'name': 'required'});

required_if: Requires the field to be present if another field has a specific value.

  • Example: 'name': 'required_if:status,active'
req.validate({
'name': 'required_if_not:status,active',
'status': 'in:active,inactive'
});
info

Name is required if status is inactive.

email: Validates that the field is a valid email address.

  • Example: 'email': 'email'
   req.validate({'email': 'email'});

string: Validates that the field is a string.

  • Example: 'name': 'string'
   req.validate({'name': 'string'});

numeric: Validates that the field contains only numeric characters.

  • Example: 'age': 'numeric'
   req.validate({'age': 'numeric'});

boolean: Validates that the field is a boolean value (true or false).

  • Example: 'active': 'boolean'
   req.validate({'active': 'boolean'});

integer: Validates that the field is an integer number.

  • Example: 'quantity': 'integer'
   req.validate({'quantity': 'integer'});

array: Validates that the field is an array.

  • Example: 'items': 'array'
   req.validate({'items': 'array'});

json: Validates that the field is a valid JSON string.

  • Example: 'data': 'json'
   req.validate({'data': 'json'});

ip: Validates that the field is a valid IP address.

  • Example: 'ip_address': 'ip'
req.validate({'ip_address': 'ip'});

alpha: Validates that the field contains only alphabetic characters.

  • Example: 'name': 'alpha'
req.validate({'name': 'alpha'});

alpha_dash: Validates that the field contains only alpha-numeric characters, dashes, and underscores.

  • Example: 'username': 'alpha_dash'
req.validate({'username': 'alpha_dash'});

alpha_numeric: Validates that the field contains only alpha-numeric characters.

  • Example: 'password': 'alpha_numeric'
req.validate({'password': 'alpha_numeric'});

date: Validates that the field is a valid date format.

  • Example: 'dob': 'date'
req.validate({'dob': 'date'});

url: Validates that the field is a valid URL.

  • Example: 'website': 'url'
req.validate({'website': 'url'});

uuid: Validates that the field is a valid UUID.

  • Example: 'user_id': 'uuid'
req.validate({'user_id': 'uuid'});

in: Validates that the field's value is in a predefined list of values.

  • Example: 'status': 'in:active,inactive'
req.validate({'status': 'in:active,inactive'});

not_in: Validates that the field's value is not in a predefined list of values.

  • Example: 'role': 'not_in:admin,superuser'
req.validate({'role': 'not_in:admin,superuser'});

start_with: Validates that the field's value starts with a specific string.

  • Example: 'title': 'start_with:vania'
req.validate({'title': 'start_with:vania'});

end_with: Validates that the field's value ends with a specific string.

  • Example: 'title': 'end_with:framework'
req.validate({'title': 'end_with:framework'});

confirmed: Validates that the field matches another field's value (usually used for password confirmation).

  • Example: 'password': 'confirmed'
req.validate({'password': 'confirmed'});

file: Validates that the field is a file upload.

  • Example: 'avatar': 'file'
req.validate({'avatar': 'file'});

min_length: Validates that the field's length is at least a minimum value.

  • Example: 'password': 'min_length:8'
req.validate({'password': 'min_length:8'});

max_length: Validates that the field's length does not exceed a maximum value.

  • Example: 'username': 'max_length:20'
req.validate({'username': 'max_length:20'});

length_between: Validates that the field's length is between a minimum and maximum value.

  • Example: 'name': 'length_between:3,50'
req.validate({'name': 'length_between:3,50'});

min: Validates that the field's value is at least a minimum numeric value.

  • Example: 'age': 'min:18'
req.validate({'age': 'min:18'});

max: Validates that the field's value does not exceed a maximum numeric value.

  • Example: 'price': 'max:1000'
req.validate({'price': 'max:1000'});

between: Validates that the field's value is between a minimum and maximum numeric value.

  • Example: 'rating': 'between:1,5'
req.validate({'rating': 'between:1,5'});

greater_than: Validates that the field's value is greater than a specified numeric value.

  • Example: 'quantity': 'greater_than:0'
req.validate({'quantity': 'greater_than:0'});

less_than: Validates that the field's value is less than a specified numeric value.

  • Example: 'age': 'less_than:100'
req.validate({'age': 'less_than:100'});