adonisjs / validator

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

Incorrect typings of 'mobile' rule #144

Closed LookinGit closed 2 years ago

LookinGit commented 2 years ago

Hello, core team!

Prerequisites

A type of rule mobile has params 'strict' and 'locales': https://github.com/adonisjs/validator/blob/0ac7e4b24c320ade5a79d481791a7962165b10fd/adonis-typings/validator.ts#L868

but a validator check a 'locale' parameter: https://github.com/adonisjs/validator/blob/0ac7e4b24c320ade5a79d481791a7962165b10fd/src/Validations/string/mobile.ts#L29

Error:

rules.mobile({ strict: true, locale: ['pt-BR'] }) // TypeScript error: 'locale' not found

Temporary solution:

rules.mobile({ strict: true, locale: ['pt-BR'] } as any)

Need update typings or rename property in compile mobile rule:

compile: wrapCompile(RULE_NAME, ['string'], ([options]) => {
    options = Object.assign({}, options)
    return {
      compiledOptions: {
        strict: options.strict || false,
        locale: options.locales,
      },
    }
  }),

If need I can send PR with a fix. But need to decide what to fix - compile or typings.

Package version

v12.2.2

Node.js and npm version

16.14.2 8.5.0

Sample Code (to reproduce the issue)

public schema = schema.create({
  phone: schema.string({ trim: true }, [
    rules.mobile({ strict: true, locale: ['pt-BR'] })
  ]),
})