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

Component Props within validator #17

Closed
vinayrajp opened this issue Sep 14, 2016 · 3 comments
Closed

Component Props within validator #17

vinayrajp opened this issue Sep 14, 2016 · 3 comments

Comments

@vinayrajp
Copy link

Hello Jeremy,
Thanks for creating revalidate. I really love how the validators can be combined and reused. Keep up the good work.
This is more of a question than an issue. Redux-form's validate function is as follows:
validate : (values:Object, props:Object) => errors:Object [optional]
My question is about the second argument props which can be utilized if the component's props are needed within validation. How can the component props be passed to createValidator/composeValidator/combineValidators, so that it can be used in validation? Is this even possible?

Thanks once again.
Vinay.

@jfairbank
Copy link
Owner

Hi, @vinayrajp!

There isn't a built-in way to access the component props. However, you could wrap your validate function with another function and then merge the values and props into one object that you pass in to your validate function. I believe that should work.

const validator = combineValidators({/* validation definition */});

function validate(values, props) {
  return validator({ ...values, ...props });
}

If you want to keep values and props separate, then you could pass them in as sub objects. You would then define your validate function in the terms of that.

const validator = combineValidators({
  'props.name': isRequired('Name Prop'),
  'values.age': isRequired('Age Value'),
});

function validate(values, props) {
  return validator({ values, props });
}

Does that help?

@jfairbank
Copy link
Owner

Closing due to inactivity. If the suggested example doesn't/didn't help, please let me know.

@ayoola-solomon
Copy link

@jfairbank can you please provide more examples... Thanks

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

No branches or pull requests

3 participants