Validation

Web apps always need some kind of form data validation. Entropy ships with a built-in validator with many useful validation rules.

Validating Form Requests

In order to check if request input data is valid, use the assert field in route config. Example validation may look like this:

      

src/users/user.controller.ts

import { HttpRequest } from '@entropy/http'; import { Controller, Route } from '@entropy/router'; export class UserController extends Controller { @Route.Post('/users', { assert: { name: { minLength: 3, maxLength: 25, // must be between 3 and 25 characters long }, age: { min: 18, // must be at least 18 years old }, email: { email: true, // must be a valid email address }, terms: { accepted: true, // checkbox must be checked }, }, }) public async store([], request: HttpRequest) { // ... } }

Available Rules

Below you have listed all available validator rules:

Rule
Type
Description
accepted
boolean
The field must be true, 'true', 'yes', 'on', '1', or 1
boolean
boolean
The field must be true, false, 'true', 'false', '1', '0', 1, or 0
date
string | Date
The field must be a valid date
declined
boolean
The field must be false, 'false', 'no', 'off', '0', or 0
doesntEndWith
string
The field must not end with the given value
doesntStartWith
string
The field must not start with the given value
endsWith
string
The field must end with the given value
email
boolean
The field must be a valid email
float
boolean
The field must be a floating point number
in
string[]
The field must be included in the given list of values
integer
boolean
The field must be an integer number
ip
boolean
The field must be a valid IP address
ipv4
boolean
The field must be a valid IPv4 address
ipv6
boolean
The field must be a valid IPv6 address
json
boolean
The field must be a valid JSON string
length
number
The field must have a length of the given value
lowercase
boolean
The field must have a value that is a lowercased string
max
number
The field must be less than given value
maxLength
number
The field must have a length less than given value
maxOrEqual
number
The field must be less than or equal to given value
maxOrEqualLength
number
The field must have a length less than or equal to given value
min
number
The field must be greater than given value
minLength
number
The field must have a length greater than given value
minOrEqual
number
The field must be greater than or equal to given value
minOrEqualLength
number
The field must have a length greater than or equal to given value
notIn
string[]
The field must not be included in the given list of values
numeric
boolean
The field must be a numeric value
otherThan
string
The field must be other than the given value
regexp
RegExp
The field must match the given RegExp pattern
required
boolean
The field must be present and not empty
sameAs
string
The field must be the same as the given value
startsWith
string
The field must start with the given value
uppercase
boolean
The field must have a value that is an uppercased string
username
boolean
The field must be a valid username and must not start with a number. Allowed characters are: [a-zA-Z0-9 _-]

Custom Rules

You can also create your own validation rules. To do so, you need to adjust the server configuration:

      

src/main.ts

const server = await createServer({ validationRules: [ { name: 'dateMonthFormat', errorMessage: 'Field :field must be a valid date in format MM/YYYY', validate: ([value]) => { return value?.match(/^\d{2}\/\d{4}$/); }, }, ], // ... });

Then you can use it in validation assertions:

      

src/users/user.controller.ts

import { Controller, HttpRequest } from '@entropy/http'; export class UserController extends Controller { @Route.Post('/users', { assert: { birthDate: { dateMonthFormat: true, }, }, }) public async store([], request: HttpRequest) { // ... } }
Dependency Injection Session