Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Validator Models #73

Merged
merged 2 commits into from
Dec 20, 2017
Merged

Conversation

nonplus
Copy link
Contributor

@nonplus nonplus commented Dec 20, 2017

Now that we can specify property models (#72), it would be great to be able to configure restifyValidation to automatically apply a model based on which validator is configured. That is, if a property uses { isInt: true } validation, its value would be automatically converted to a number without having to explicitly specify { model: Number }.

This PR adds a validatorModels configuration option and provides standard validatorModels for the built-in validators (isBoolean, isDate, isDecimal, isDivisibleBy, isFloat, isInt, isNatural). The README has more info.

The following example uses restifyValidation.validatorModels but then overrides the model for the isDate validator to map values to moment rather than to Date:

var moment = require('moment');
server.use(restifyValidation.validationPlugin({
    validatorModels: [
        restifyValidation.validatorModels,
        { isDate: moment }
    ]
}));

@nonplus
Copy link
Contributor Author

nonplus commented Dec 20, 2017

@gchauvet, this is the last of my changes. With this, rest handlers will be able to work with typed data without having to modify the validation configuration for the individual routes.

Once this has been merged, would you mind doing a build and publishing to npm? Thanks!

@gchauvet gchauvet merged commit 1b467c0 into z0mt3c:master Dec 20, 2017
@gchauvet
Copy link
Collaborator

gchauvet commented Dec 20, 2017

@nonplus okay I merge changes in master branch. I will it release in 1 week max under version 1.2.0
Tank you for your contribs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants