poppinss / indicative

Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
https://indicative.adonisjs.com/
MIT License
417 stars 52 forks source link

validator:In fails on integers #218

Closed pixelpaulaus closed 5 years ago

pixelpaulaus commented 5 years ago

when validating an integer is one of... required|integer|in:18451,18367,18364 It returns a fail as it is matching against a string. The submitted value is indeed a integer tho. validator:equals works, but not validator:In

thetutlage commented 5 years ago

Yup coz in:18451,18367,18364 is interpreted as string. You have to use the rule method here.

{
  code: [
    rule('required'),
    rule('integer'),
    rule('in', [18451,18367,18364])
  ]
}
pixelpaulaus commented 5 years ago

hmmm, not sure that is possible in adonisjs :-(

shouldn't it see the interger requirement and match against integers?

thetutlage commented 5 years ago

Why you think it's not possible in Adonis?

pixelpaulaus commented 5 years ago

sorry i could not find any example of writing in the rule method you mentioned. so i could rewrite...

const rules = { subject: 'required|string|max:200', department: 'required|integer|in:18451,18367,18364', service: 'integer', }

to be like...

const rules = { subject: 'required|string|max:200', department: code: [ rule('required'), rule('integer'), rule('in', [18451,18367,18364]) ], service: 'integer', }

thetutlage commented 5 years ago

You can use it as follows:

const { rule } = use('Validator')
pixelpaulaus commented 5 years ago

thanks, but that is not very clear. are there any examples in the docs to do it this way?

const validation = await validate(request.post(), rules)

thetutlage commented 5 years ago

Indicative docs does talk about it https://indicative.adonisjs.com/docs/syntax-guide#_limitations