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

Regex fails when using : #182

Closed eNzyOfficial closed 6 years ago

eNzyOfficial commented 6 years ago

Hey,

I had a quick look and saw there was an issue opened and closed as fixed. However I seem to be still getting the same problem.

return {
  slug: 'required|regex:/^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/'
}

Returns Invalid flags supplied to RegExp constructor '-[a-zA-Z0-9]+)*$/'

(On a side note, I am aware that there is the slug sanitizer, but I would like to check the db for uniqueness before hand, plus, it seems to add dashes between numbers, regardless of where they are, eg. h3ll0-w0rld would become h-3-ll-0-w-0-rld rather than h3ll0-w0rld)

thetutlage commented 6 years ago

If you will go through the docs, under Syntax guide, it talks about the limitations of the syntax https://indicative.adonisjs.com/docs/syntax-guide#_limitations

What you need is to use the rule function over a string.

slug:  [
  rule('required'),
  rule('regex', '/^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/')
]
eNzyOfficial commented 6 years ago

When I had a look through the adonis docs I couldn't find anything about using rule(), could you point me in the right direction?

thetutlage commented 6 years ago

I believe it will be same

const { rule } = use('Validator')
eNzyOfficial commented 6 years ago

Awesome, thanks!