validatorjs
validatorjs copied to clipboard
Rewrite and refactoring
It took me a little longer than expected due to lack of time but I managed to rewrite whole core library to Typescript. Also some improvements are included.
My branch is here https://github.com/LKay/validatorjs/tree/lkay/typescript-rewrite, so @garygreen and @skaterdav85 please check it out.
Highlights:
- Unify the way custom rules are registered
- Rewrite async validation to use Promises. In node library uses default Promise, for the browser
es6-promise
is injected to keep compatibility. Any Promise A+ compatible library can be used ie.bluebird
,Q
etc. by just replacingValidator.Promise = CustomPromise
. - Async rule can be now defined either the old way or as named function
function async () {}
or can return Promise which will be detected and resolved automatically. - Added timeout for async validators in case the Promise is not resolved or callback is not called.
- Added "stop on first error" feature the way it's implemented in Laravel so just defining
bail
rule for the field is enough. Should be enough to close #63. - Changed the way errors are handled and returned. Now errors are stored as abstracts and actual localised messages are applied when accessing them by
errors.all()
,errors.get("foo")
orerrors.first("foo")
. - The API of the libraries is not broken and only some additional parameters have changed so basically everything is backwards compatible.
- Moved rules outside
Rules
object so they can be defined one by one. This should help with maintaining and adding new rules to the library. - Thanks to Typescript the code is type safe which should help to eliminate possible bugs with passing incorrect data types.
What needs to be done next:
- Reimplement validation rules (currently only
required
andmin
are added). - Need to set up testing environment and write some sample tests.
- Rewrite tests
- Add support for wildcard rules for array validation discussed in #101.
- Add instance context only custom rules mentioned in #98.
From now we can maybe assign rules and tests between each others. This will not be very difficult as it is just applying the template but it's a bit tedious.
@LKay I don't know TS but this looks pretty readable from my perspective and it probably would be easier to maintain. Originally I was concerned that this might slow the growth of this library like @garygreen mentioned and fewer would contribute to this project, but from what I can tell, TS is pretty easy to follow, so I'm ok with moving torwards TS. Would you be able to merge the latest from master into your branch and update it all? Also, I know that there is this outstanding PR. I think it'd be good if we merged that into master first before merging yours in since that PR is much smaller. So once he finishes, I'll let you know so that you can update things on your side to be TS, and then we'll merge yours in. Sound good?
@skaterdav85 I would rather cerate a new branch such as 4.0.0-wip (I believe it would be next major release eventually) and work out other things there and not rashly merge it to the master straight away. There is still some work left with porting validation rules and tests.
Dears, any updates about "stop on first error" option ?