adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

Date - Custom Message #146

Closed ruanitto closed 2 years ago

ruanitto commented 2 years ago

I need a custom message on validator for data fields to show the date format, but the options passed to the message string is undefined

Package version

12.2.3

Node.js and npm version

node: 14 npm: 6

Sample Code (to reproduce the issue)

const dateField = await validator.validate({
        schema: schema.create({
          startDate: schema.date({ format: 'yyyy-MM-dd' })
        }),
        messages: {
          'date.format': '{{ field }} must be formatted as {{ format }}',
        },
        data: {
          startDate: 'anyValue'
        }
   })

When the error ocurrs, the options is caming undefined:

{
 ...
 messages: { startDate: [ 'startDate must be formatted as undefined' ] }
 ...
}
thetutlage commented 2 years ago
CleanShot 2022-04-05 at 16 30 35@2x

Seems like a typo in the code example. It should be options.format.

ruanitto commented 2 years ago

I also tried it before. but the same error occurs. Later I'll take a look at the validator's source code and see if I can find the fix, or where I'm going wrong. Apparently the options object arrives empty

Screenshot_26 .

thetutlage commented 2 years ago

Seems like a bug in the code. Fixed in https://github.com/adonisjs/validator/commit/f60dea0f88bcc3bb8955ec8c8443a6068fcde1d1

Will be released soon