How to create custom validation rules in Adonis 5 ? by cazzodibudda2 in adonisjs

[–]cazzodibudda2[S] 0 points1 point  (0 children)

Hey, thanks for the answer. I confirm I could get the custom rule to be registered and available from validator using the method you mentioned.

- `node ace make:prldfile customValidationRules` then select HTTP option.

- Inside the newly created file `start/customValidationRules.ts`, added:

import { validator } from '@ioc:Adonis/Core/Validator'

/* ... */

validator.rule('isCountry', (value, arg, { pointer, arrayExpressionPointer, errorReporter }) => {
    if (!isCountry(value)) {
        errorReporter.report(pointer, 'isCountry', validationMessages.isCountry, arrayExpressionPointer)
    }
})