jurassix / react-validation-mixin

Simple validation mixin (HoC) for React.
MIT License
283 stars 38 forks source link

Is there a way to validate nested objects? #25

Closed chodorowicz closed 8 years ago

chodorowicz commented 9 years ago

I'm using nested object to keep state and https://github.com/EarthlingInteractive/react-deep-link-state to link state. Is there a way to make it work?

jurassix commented 9 years ago

The limitation here is with the way I'm flattening the validation errors. Joi handles complex object validation fine.

I think you can get it to work with the following API's:

getValidatorData: function() {
  return {
    username: this.state.user.profile.username,
    password: this.state.user.profile.password
  };
}

validatorTypes: {
  username: Joi.string().alphanum().min(3).max(30).required(),
  password: Joi.string().regex(/[a-zA-Z0-9]{3,30}/)
}

getValidatorData is going to flatten your object to be used by this validation library and validatorTypes will just be a simple Joi schema for the resulting flattened data. This should allow you validate the field and get the resulting validations messages from the api.

Give it a try I think this is currently the way to do this. I am working towards a pretty heavy refactor of this lib that will fix this limitation but I'm not sure when those will be ready; weeks/month not days.

chodorowicz commented 9 years ago

It's working. Thanks! Will be enough until rewrite is ready. Greets!

jurassix commented 9 years ago

I'll try to get validation messages for deep object paths in the next release. It didn't make it in my latest release but should not be to much hard to support now.

Documentation for 5.0

chodorowicz commented 9 years ago

Cool! :+1:

jurassix commented 8 years ago

This has been fixed with 5.2.0: https://github.com/jurassix/react-validation-mixin/releases/tag/v5.2.0

jurassix commented 8 years ago

Ensure you upgrade joi-validation-strategy to v0.2.1

chodorowicz commented 8 years ago

Awesome! I will test it out soon!