Validation using FrontQL
FrontQL provides a comprehensive set of validation rules to ensure data integrity and type safety. These validation rules can be applied to fields in your data models to enforce specific constraints and formats.
Available Validation Rules
Rule Name | Description |
---|---|
required | Validates that the field has a non-null or non-empty value (0 is considered valid) |
string | Validates that the field’s value is of type string |
boolean | Validates that the field’s value is of type boolean |
number | Validates that the field’s value is of type number |
integer | Validates that the field’s value is an integer |
email | Validates that the field’s value is a valid email format |
min:{x} | Validates that the field’s numerical value is greater than or equal to the specified value |
max:{x} | Validates that the field’s numerical value is less than or equal to the specified value |
len:{x} | Validates that the string representation of the field’s value has an exact length of the specified value |
minlen:{x} | Validates that the string representation of the field’s value has a minimum length of the specified value |
maxlen:{x} | Validates that the string representation of the field’s value has a maximum length of the specified value |
Usage Examples
Basic Type Validation
// Validate that a field is a required string{ name: "required|string";}
// Validate that a field is a number{ age: "number";}
String Length Validation
// Username must be between 3 and 20 characters{ username: "required|string|minlen:3|maxlen:20";}
// Exact length validation{ zipCode: "string|len:5";}
Numerical Range Validation
// Age must be between 18 and 65{ age: "required|number|min:18|max:65";}
// Rating must be an integer between 1 and 5{ rating: "integer|min:1|max:5";}
Email Validation
// Valid email format required{ email: "required|email";}
Combining Rules
Validation rules can be combined using the pipe (|
) separator to create complex validation scenarios:
{ email: "required|string|email|maxlen:255", password: "required|string|minlen:8|maxlen:128", age: "required|integer|min:0|max:120"}
Error Handling
When validation fails, FrontQL will return appropriate error messages indicating which fields failed validation and why. This helps in providing clear feedback to users and maintaining data quality.