You can install this package using either npm or Yarn.
# npm
npm install @voxoco/validator
# Yarn
yarn add @voxoco/validator
The VOXO Validator package is a wrapper for the Indicative validator package for the Adonis Framework. It provides both the .validate
and .sanitize
methods, as well as all of the schemas necessary for both frontend and backend validation.
You can validate any incoming request by importing this package into wherever needed.
Here are a list of rules the validator can use.
import { bcrypt } from 'encryptor';
import { validator } from 'voxo-validator';
import User from '../models/User.model';
class Auth {
/**
* Create a new user account.
*
* @param {Request} request
* @return {JsonResponse}
*/
register({ request, response }) {
const rules = {
email: 'required|email',
password: 'required|string|min:8',
};
validator.validate(request.all(), validator.rules.auth.REGISTER))
.then(() => {
const password = bcrypt(request.password);
const user = new User({
email: request.get('email').
password,
});
return response.json(user);
})
.catch((errors) => {
console.error(errors);
});
}
}
The .sanitize
method uses the same schema specification as the validations schema and is recommended to sanitize the user input before storing it to a database.
Here are a list of rules the sanitizer can use.
const unsafe = {
email: '[email protected]',
address: '<p><strong>1600</strong> Pennsylvania Ave.</p>',
};
const schema = {
email: 'normalize_email',
address: 'strip_tags'
}
validator.sanitze(data, schema);
The unsafe
object will now contain:
{
email: '[email protected]',
address: '1600 Pennsylvania Ave.',
}
.rules.dids.CREATE
.rules.dids.UPDATE
.rules.e911.CREATE
.rules.e911.UPDATE
.rules.passwordReset.UPDATE
.rules.porting.CREATE
.rules.porting.UPDATE
.rules.tenants.CREATE
.rules.tenants.UPDATE
.rules.tnOrdering.COVERAGE
.rules.tnOrdering.INVENTORY
.rules.tnOrdering.NUMBER_DETAIL
.rules.users.ANY
.rules.users.UPDATE