Validation of regex with Indicative (adonisJs)

Asked

Viewed 281 times

0

I am trying to validate a CPF with regex using the Adonis Database.

I tried that way:

const { validations } = use('indicative/validator')
const { validate } = use('Validator')

const rules = (values) => ({
   doc: 'string|required|max:11|min:11|regex:'+validations.regex([new RegExp(/([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/g)]),
})

const messages = {
 regex: field => `${field} don't have the correct format`
}
class StudentController {

  async store ({ request, response }) {

    const estudante = { ...request.all(), user_id: request.user_id }

    const validation = await validate(estudante, rules(rules), messages)

    if (validation.fails()) {

      return response.validateError(validation)

    }
}

But when I make a request for the route that has this function with the value of doc: 39257443828

I get this error report:

doc don’t have the correct format

I tested this regex on some regex sites and this value passes, but in the Validator of the Indicative is indicating that it is not in the format expected by regex.

1 answer

0


If anyone needs it, I can use rules as array and using the rule():

const { validate, rule } = use('Validator')
doc: [rule("regex", /([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/g),
      rule("required"),
      rule("min", 11),
      rule("max", 11),
      rule("string")]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.