Closed dvv closed 13 years ago
That's a tough question. I believe the best place to add such a feature would be in the validator functions of each attribute. You could check the type of the instance, and locally convert as necessary.
I see. How do I persist coerced instance? I see there are .getType(), .getValue(), but not _set_Value()?
Yea, JSV is not designed to handle changes on the fly. If you want to change an instance, you have to create a new instance with the new value.
By instance you mean the whole object being validated? Couldn't you throw a gist on how do I recreate the instance given a validation error occured?
Instances can be created using:
environment.createInstance(newValue, "uri");
I mean if I validate {a:{b:{c:"true"}}} and during validation the current execution point has dived two levels deep and is validating {c: "true"}, and I want to coercively validate "true" to be true
, how do I patch the instance being validated so that it contained {c: true}?
Possible?
--Vladimir
Not without complications. I wouldn't recommend changing the instance being validated, but perhaps attaching a new instance to the report object that is a coerced copy of the instance being validated?
Great idea! I'd call it request for feature. Can I ask you to implement this?
Well, it's pretty low on my priority list; and I'm quite busy till January. If some other people express interest in this, I'll look into doing it then.
I'll point you to one very interesting case imho. When storing hierarchical data to redis, one chooses between storing the whole JSON, or each key separately. The latter case eases querying, sorting and so on, but the stored data are always stringy.
Being able to pipe fetched set of strings be coerced to real object by simple validation by your schema opens the way to store rich objects in redis!
Could you not store the value in a JSON format? What I mean is that in the redis value, you store the JSON primitive (null, boolean, number, string) as JSON:
null, true, false, 0.0, "string"
Then, when you extract the value from redis you use JSON.parse(), which will properly convert.
JSON.parse is way faster then using JSV. And if you wanted to store that string, you surround it in quotation (") marks, like you did there.
Nevermind, I respect your "no".
Hi!
At what point could I add coercion in order to mangle the data to fit the schema? Of particular interest is simple workaround to make autoconversion of strings obtained from web forms to booleans ('on' --> true), dates, arrays (by
.split(/\W+/)
)?Do you ever consider feasible to have coersive validators?
TIA, --Vladimir