nettofarah / property-validator

:white_check_mark: Easy property validation for JavaScript, Node and Express.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sanitize option for some validators

bjrmatos opened this issue · comments

Hi! do you will accept a PR that implements a sanitize: true option? for example for isDate, isDecimal , isInt validations would be nice to have a sanitize option to automatically convert the value if is valid.

isInt('paramName', { sanitize: true })

what do you think?

that sounds like a good idea.
Though I'm not sure if this would be the right API.

maybe it makes sense to build a more composable API?

maybe, i was only throwing the idea (with minimal changes to the current API) to see if you liked 😄, but if you have some thoughts to make it better it will be awesome.

right now in every express app i've found myself doing a lot of parseInt, Boolean(), JSON.parse, parsing dates, etc so this feature will definitely be really useful.

Hey, @bjrmatos. Wanna pick this back up?

@nettofarah sure! do you have some ideas? or are you open to exploration? :D

how about something like:

sanitize(isInt('paramName'))

All that sanitize would do is pass the {sanizite: true} props to the validation function. Or if you wanna get real exotic find out the callee of the isInt method (arguments.callee.caller.name) and sanitize it. (deprecated)

I like where you're going, @DJWassink.
Maybe we do something a little simpler for now that wouldn't change the architecture too drastically.

We could provide an extra top-level sanitize function you could pass the same validations to.

The code could look like this:

let validations = [ isEmail('..'), isInt('age'), isArray('bla') ]
assertAll(obj, validations)
let sanitizedObject = sanitize(obj, validations)

The person could then compose both assert and sanitize and have them return the new params.

LGTM but I am wondering about how to implement a structure like that, aint there the problem now that a validation function (e.g. isEmail) doesn't know if it should assert/validate or sanitize?