adonisjs / validator

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

`minLength` rule doesn't respect `trim` rule #147

Closed cawa-93 closed 2 years ago

cawa-93 commented 2 years ago

Package version

"@adonisjs/core": "5.7.6",

Node.js and npm version

node v16.14.0 npm 8.3.1

Sample Code (to reproduce the issue)

await validator.validate({
  schema: schema.create({
    text: schema.string([
      rules.trim(),
      rules.minLength(1),
    ]),
  }),
  data: {
    text: ' ',
  },
})

Validation is not expected to pass, but it is passing

cawa-93 commented 2 years ago

Here is

https://github.com/adonisjs/validator/blob/2d63698452957fd946c73862b9fbf9b4c633eb51/src/Validations/string-and-array/minLength.ts#L40

For some reason rule check originalValue but not actual value

thetutlage commented 2 years ago

Thanks for reporting.

Yup. That was required earlier when trim was just an option and not the rule itself. But, we should now rely on the mutated value.