HTTP Fundamentals

Validation

Validate request data and return consistent field-level errors.

Beta · v0.2.0Lanjit APIs may change before the stable release. Review the release notes and upgrade guide before updating.

request.validate() validates merged query and body input and returns only declared fields. Invalid input raises a ValidationException with status 422. Built-in rules include required, string, email, number, integer, boolean, min, max, in, and confirmed.

Examples

Route.post('/register', async (request, response) => {
  const data = await request.validate({
    name: ['required', 'string', 'min:2', 'max:100'],
    email: ['required', 'email'],
    password: ['required', 'string', 'min:8'],
  });

  return response.json(data, 201);
});
{
  "message": "The given data was invalid.",
  "errors": {
    "email": ["email failed the email validation rule."]
  }
}
const data = await request.validate({
  email: 'required|email',
  age: 'integer|min:18',
});
Edit this page on GitHub

Type to search all documentation.