mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

✨ `validated()` method #444

Closed cednore closed 2 years ago

cednore commented 2 years ago

validated() method

Fixes #307

Description

This PR adds two methods to Validator's prototype; validated() and _onlyInputWithRules(). Inspired from Laravel's validation concepts.

validated() method will retrieve the input data without the attributes not defined in rules provided. This helps to extract only valid dataset from input and filter out unnecessary or additional attributes from input.

While validated() throws an error if the validation fails, _onlyInputWithRules() does the pure job to filter out non-listed-in-rules attributes. validated() method relies on _onyInputWithRules() method under the hood.

Type of change

cednore commented 2 years ago

@mikeerickson Please take care of version bump if above changes are g2g.

cednore commented 2 years ago

Improving to handle nested rules...

cednore commented 2 years ago

@mikeerickson Just pushed the fix for _onlyInputWithRules function to handle nested rules. Please review.