jurassix / react-validation-mixin

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

`getValidationMessages` is confusing #51

Closed fbaiodias closed 9 years ago

fbaiodias commented 9 years ago

Hey!

First of all, thanks for making this module. It's great and very useful!

However, I'm finding the changes introduced in v5.2.0 on getValidationMessages a bit confusing.

For example, if propA has two errors, and you do this.props.getValidationMessages('propA'), it only returns the first error message as a string. However, if the propA is valid, an empty array is returned instead.

I believe it would be clearer if it always returned an array as before, or instead returned undefined when there are no errors.

I would be happy to do a PR if you think this makes sense :)

Cheers

jurassix commented 9 years ago

Yes this is confusing and I didn't document very well either.

There were some compromises I had to make around getValidationMessages in order to support validating nested objects and object arrays. Basically, it was difficult to rehydrate the errors object.

The current implementation should return a single error when multiple exist (compromise) and should only return an array if the joi data structure was an array.

Related Issues:

42

39

38

If you would like to take a stab at this that would be greatly appreciated. The test coverage is pretty good for this library, so that will definitely help.

fbaiodias commented 9 years ago

Alright, thanks for the quick reply!

Will look into it then :)

jurassix commented 9 years ago

This was the compromise, and a good place to start looking for improvements:

https://github.com/jurassix/joi-validation-strategy/blob/master/src/joiValidationStrategy.js#L28

By doing this I believe it helped me maintain the original structure of the error object. Before this method would always guarantee an array of errors.

jurassix commented 9 years ago

Resolved in the following version:

react-validation-mixin v5.3.1 joi-validation-strategy v0.3.0