adonis-validators

0.0.3 • Public • Published

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:

  1. Add a new validator class which extends CustomValidator
  2. Implement validate method
  3. 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();
  1. Add the new validator to modules on providers/index.ts

Readme

Keywords

none

Package Sidebar

Install

npm i adonis-validators

Weekly Downloads

5

Version

0.0.3

License

MIT

Unpacked Size

5.25 kB

Total Files

11

Last publish

Collaborators

  • josemiguelmelo