Closed idolize closed 9 years ago
It appears get(list, key)
is returning another Object
mapped by key when the error comes from inside a nested object.
Maybe instead of:
export default function decode(list, key) {
if (defined(list, key)) {
const value = get(list, key);
if (Array.isArray(value)) {
return value.map(he.decode);
}
return value ? he.decode(value) : '';
}
}
it should be recursive? Something like:
export default function decode(list, key) {
if (defined(list, key)) {
const value = get(list, key);
if (isPlainObject(value)) { // isPlainObject from lodash
return Object.keys(value).map(decode.bind(this, value));
}
if (Array.isArray(value)) {
return value.map(he.decode);
}
return value ? he.decode(value) : '';
}
}
(I updated the title because it seems like it only happens on validation errors that come from within nested objects)
Thanks for the report. I'll try to get some tests around this.
Thanks @jurassix
I'm thinking the easiest place to handle this is inside the Joi strategy and not a top level concern of this library. Also, the Joi validation results are returned flat, mapping over them and applying decode is simpler.
However, I have one issue now. getValidationMessages
needs to return an array of error messages, in order to achieve this I have to flatten a complex object. Looking for a good solution for this.
If the Joi strategy is updated to return objects keyed by the "invalid property name" with the values being the error strings, wouldn't something like Object.keys(errors).map(key => errors[key])
suffice?
We support nested schema's so objects can have a structure like:
{a: {b: {c: "is required"}}}
function flattenErrorsObject(obj) {
var values = [];
if(_.isObject(obj)) {
values = _.values(obj);
} else if(_.isString(obj)) {
return obj;
}
var flat = values.map(value => {
if (!_.isString(value)) {
return flattenErrorsObject(value);
}
return value;
});
return _.flatten(flat);
}
var test = {a:'me', b: {c: 'c', d: {e:['f','g']}}};
> flattenErrorsObject(test)
< ["me", "c", "f", "g"]
@idolize Think you can code review this? Is there a better way to do this?
I've noticed that react-validation-mixin contains code to decode error messages using the following branching paths:
However, when I run
getValidationMessages()
in my project it blows up here, becausevalue
is coming back as anObject
keyed withArrays
for each invalid field. Thus, whendecode
runs it tries to runhe.decode
on anObject
(which expects aString
and thus can't find theString.replace
method and throws anException
).I'm using react-validation-mixin version 5.3.1.