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
acceptedbooleanThe field must be
true, 'true', 'yes', 'on', '1', or 1booleanbooleanThe field must be
true, false, 'true', 'false', '1', '0', 1, or 0datestring | DateThe field must be a valid date
declinedbooleanThe field must be
false, 'false', 'no', 'off', '0', or 0doesntEndWithstringThe field must not end with the given value
doesntStartWithstringThe field must not start with the given value
endsWithstringThe field must end with the given value
emailbooleanThe field must be a valid email
floatbooleanThe field must be a floating point number
instring[]The field must be included in the given list of values
integerbooleanThe field must be an integer number
ipbooleanThe field must be a valid IP address
ipv4booleanThe field must be a valid IPv4 address
ipv6booleanThe field must be a valid IPv6 address
jsonbooleanThe field must be a valid JSON string
lengthnumberThe field must have a length of the given value
lowercasebooleanThe field must have a value that is a lowercased string
maxnumberThe field must be less than given value
maxLengthnumberThe field must have a length less than given value
maxOrEqualnumberThe field must be less than or equal to given value
maxOrEqualLengthnumberThe field must have a length less than or equal to given value
minnumberThe field must be greater than given value
minLengthnumberThe field must have a length greater than given value
minOrEqualnumberThe field must be greater than or equal to given value
minOrEqualLengthnumberThe field must have a length greater than or equal to given value
notInstring[]The field must not be included in the given list of values
numericbooleanThe field must be a numeric value
otherThanstringThe field must be other than the given value
regexpRegExpThe field must match the given RegExp pattern
requiredbooleanThe field must be present and not empty
sameAsstringThe field must be the same as the given value
startsWithstringThe field must start with the given value
uppercasebooleanThe field must have a value that is an uppercased string
usernamebooleanThe 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) {
// ...
}
}