jfairbank / revalidate

Elegant and composable validations
http://revalidate.jeremyfairbank.com
MIT License
363 stars 20 forks source link

Nested composed validators do not curry as expected. #54

Open jaridmargolin opened 7 years ago

jaridmargolin commented 7 years ago

Nested composed validators do not curry as expected.

* This may just be a misunderstanding of the lib is intended to work.

Expected:

// imagine I have created a validator `containsNoNumbers`
const isName = composeValidators(hasLengthBetween(2, 32), containsNoNumbers)
composeValidators(isRequired, isName)('First Name')
composeValidators(isRequired, isName)('Last Name')

isRerquired works as expected and allows the field to propagate through. isName however always returns a function. I can fix the issue by doing something like:

const isName = composeValidators(hasLengthBetween(2, 32), containsNoNumbers)
composeValidators(isRequired, isName('First Name'))('First Name')
composeValidators(isRequired, isName('Last Name'))('Last Name')

but it seems as though the composed validator should work exactly the same as a created validator.

The composed validator is also curried and takes the same arguments as an individual validator made with createValidator.

jfairbank commented 7 years ago

Hey, @jaridmargolin.

Sorry for the misunderstanding. I originally wrote composeValidators with validators created via createValidator in mind, so I never thought about the nested way you're trying to use it here. That being said, I don't see any reason why we can't support your use case too.

I'm swamped at the moment, so I may not be able to hop on fixing this immediately. You're welcome to open a PR if you like, though. Just let me know.

Thanks!

jaridmargolin commented 7 years ago

I was poking around in the source code but it wasn't immediately clear to me where / what the patch would look like. If I have some more time to dig into a fix, I will.

Thanks for the quick response.