validate.js
validate.js copied to clipboard
Better Typings for constraints
I have added typings for constraints. This PR differs in some ways from #258:
- it is stricter for length constraint: mi/max may not be used together with "s
- it is stricter for the numericality constraint: equal may not be used together with greater/less
- allows custom messages to be optional
- allows any instead of only Date for earlieast/latest in datetime constraint. According to the docs earliest/latest are parse by the parse function, Thus it makes sense to allow the user any values as long as he can parse them
- allows functions that return the message
- allows functions that return the validation constraint
- adds return type for the validation function
I did not use Record<string, ConstraintsTypes> as you suggested in the other PR. I think an index signature is better in this case because with the index signature we can indicate that the first string is meant to be an attributeName. This is not possible with records. Aside from that, they are the same regarding type safety.