npm i --save adonis-validation-provider
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
Validating user data is an essential requirement for any application. AdonisJs makes use of Indicative to sanitize and validate user inputs. It supports all validation rules from indicative, so make sure to check indicative’s documentation.
Validator is not the part of the base installation and hence you are required to install and register it manually.
npm i --save adonis-validation-provider
const providers = [
// ...
'adonis-validation-provider/providers/ValidatorProvider'
// ...
]
const aliases = {
// ...
Validator: 'Adonis/Addons/Validator'
// ...
}
Let’s take the most basic example of validating a form to create a user. For now, we will perform all the validations within the controller, but you are free to organize them into separate services.
Let’s start by defining the rules on the User model. Make sure also to check out indicative docs on schema rules.
'use strict'
const Lucid = use('Lucid')
class User extends Lucid {
static get rules () { (1)
return {
username: 'required|unique:users',
email: 'required|email|unique:users',
password: 'required|confirmed',
}
}
}
'use strict'
const Validator = use('Validator')
const User = use('App/Model/User')
class UsersController {
* store(request, response) {
const userData = request.all()
const validation = yield Validator.validate(userData, User.rules) (2)
if (validation.fails()) { (3)
response.json(validation.messages()) (4)
return
}
// Validation passed, create the user.
}
}
1 | We start by defining the rules inside the User Model. |
2 | Next we validate the user data from request against the previously defined rules. |
3 | The validation.fails() returns true, if validation has failed. |
4 | The validation.messages() will return all the error messages as an array. |
Below is the list of methods exposed by the Validation Provider.
Validate user data against defined rules and returns as soon as first validation get’s failed.
const validation = yield Validator.validate(data, rules, messages)
Same as validate
but instead continue till the last validation and returns multiple error messages.
const validation = yield Validator.validateAll(data, rules, messages)
Returns a boolean indicating whether the validation has been failed or not.
const validation = yield Validator.validate(data, rules, messages)
if (validation.fails ()) {
// validation failed
}
Returns an array of validation error message
const validation = yield Validator.validate(data, rules, messages)
if (validation.fails ()) {
response.send({error: validation.messages()})
}
Sanitize user data. Also make sure to check the sanitization docs for more info.
const data = request.all()
const rules = {
email: 'normalize_email',
bio: 'strip_links',
fullname: 'capitalize'
}
const sanitizedData = Validator.sanitize(data, rules)
You can make use of is method to do inline validations instead of going through a complete validation lifecycle.
if (Validator.is.email('foo')) {
// ...
}
Same as is
, you can do inline data sanitisation.
const sanitizedEmail = Validator
.sanitizor
.normalizeEmail('bar.sneaky+foo@googlemail.com')
// returns barsneaky@gmail.com
AdonisJs supports all the validation rules by Indicative, but also adds a few who are specific to AdonisJs only. Below is the list of custom rules.
Makes sure a given value is unique in a given database table.
'use strict'
class User extends Lucid {
static get rules () {
return {
email: 'unique:users,email'
}
}
}
Now when trying to update a user, you would never want to run the unique validation for the same user. Same can be achieved by defining a whereNot
clause.
'use strict'
class User extends Lucid {
static rules (userId) {
return {
email: `unique:users,email,id,${userId}`
}
}
}
Inside you controller, you can do
'use strict'
const User = use('App/Model/User')
class UsersController {
* update (request, response) {
const userId = request.param('id')
const rules = User.rules(userId) (1)
const validation = yield Validator.validate(request.all(), rules)
}
}
1 | When fetching the rules from the User Model, we pass along the user id which gets ignored when checking the email uniqueness. |
Quite often you have the requirement of extending the Validator Provider by adding new validation rules. You can make use of the extend
method provided by Indicative.
For application specific rules you can make use of app/Listeners/Http.js
file to listen for the start event and your custom rule.
Http.onStart = function () {
const Validator = use('Adonis/Addons/Validator')
Validator.extend('adult', (data, field, message, args, get) => {
return new Promise((resolve, reject) => {
const fieldValue = get(data, field)
if (fieldValue > 18) {
resolve('Allowed')
return
}
reject(message)
})
}, 'You must be an adult')
}
If you are writing a module/addon for AdonisJs, you can add your custom rules inside the boot
method of your service provider.
const ServiceProvider = require('adonis-fold').ServiceProvider
class MyServiceProvider extends ServiceProvider {
_adultValidation (data, field, message, args, get) {
return new Promise((resolve, reject) => {
const fieldValue = get(data, field)
if (fieldValue > 18) {
resolve('Allowed')
return
}
reject(message)
})
}
boot () {
const Validator = use('Adonis/Addons/Validator')
Validator.extend('adult', this._adultValidation, 'You must be an adult')
}
* register () {
// register bindings
}
}
You can make use of the above defined adult
like any other validation rule.