adonis-validators
v0.0.3
Published
AdonisJS Validators extensions provider
Downloads
13
Readme
Adonis validators
This is an extension for Adonis validator, adding new validators to it.
Installation
In order to install, run the following command:
yarn add adonis-validators
After that, add Adonis Validators to the list of providers on start/app.js
:
const providers = [
....,
'adonis-validators/providers/AdonisValidatorsProvider'
]
Validators
- ShouldNotExistWhen - a field should not be present when another field is present on the request.
Example: password should not be sent if token is sent; token should not be sent if password is sent.
Usage:
get rules() {
return {
password: 'shouldNotExistWhen:token',
token: 'shouldNotExistWhen:password',
}
}
Contributing
If you want to contribute and add more validators:
- Add a new validator class which extends
CustomValidator
- Implement validate method
- Set
validatorName
. This validator name is the name used on the validation rule.
import CustomValidator from './CustomValidator';
class ExampleValidator extends CustomValidator {
validatorName: string = 'exampleValidator';
async validate(data: any, field: any, message: any, args: any, get: any) {
const value = get(data, field);
if (!value) {
return;
}
throw message
}
}
export default new ExampleValidator();
- Add the new validator to
modules
onproviders/index.ts