adonisjs / validator

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

Invalid Validation by `before` Rule #165

Closed ndianabasi closed 1 year ago

ndianabasi commented 1 year ago

The before rule seems not to be validating properly. And I've traced the issue to this line: https://github.com/adonisjs/validator/blob/aded92080371ea5c12070236e2b159cb07a1c264/src/Validations/date/helpers/offset.ts#L33

The duration should be added to the current date before checking if the provided date is before the maximum allowable date.

Package version

"@adonisjs/core": "^5.9.0"

Node.js and npm version

Node v14.21.1 NPM v9.1.2

Sample Code (to reproduce the issue)

This is the sample of the request made:

const response = await client
        .post(route('user_api_tokens.store'))
        .loginAs(loginUser)
        .form({
          name: 'Awesome API token',
          expires_in: DateTime.now().plus({ years: 1 }).toISODate(),
        })

This is the validation schema:

{
        name: schema.string.optional([rules.trim(), rules.stripTags(), rules.maxLength(50)]),
        expires_in: schema.date({ format: 'iso' }, [
          rules.before(2, 'years'),
          rules.after('today'),
        ]),
      }

And this is the response:

'{"errors":[{"rule":"before","field":"expires_in","message":"before validation failed on expires_in","args":{"before":"2021-05-11T20:30:32.922+01:00"}}]}',

As seen, the compiled date for the before arg is 2 years behind.

Changing https://github.com/adonisjs/validator/blob/aded92080371ea5c12070236e2b159cb07a1c264/src/Validations/date/helpers/offset.ts#L33 to plus fixes the issue.

thetutlage commented 1 year ago

Isn't it working correctly? I will use some absolute numbers to make the conversation easy to understand.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

RomainLanz commented 1 year ago

Closing since no answer from issue reporter.