garycourt / JSV

A JavaScript implementation of an extendable, fully compliant JSON Schema validator.
618 stars 84 forks source link

coercion #5

Closed dvv closed 13 years ago

dvv commented 13 years ago

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

garycourt commented 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.

dvv commented 13 years ago

I see. How do I persist coerced instance? I see there are .getType(), .getValue(), but not _set_Value()?

garycourt commented 13 years ago

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.

dvv commented 13 years ago

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?

garycourt commented 13 years ago

Instances can be created using:

environment.createInstance(newValue, "uri");

dvv commented 13 years ago

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

garycourt commented 13 years ago

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?

dvv commented 13 years ago

Great idea! I'd call it request for feature. Can I ask you to implement this?

garycourt commented 13 years ago

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.

dvv commented 13 years ago

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!

garycourt commented 13 years ago

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.

dvv commented 13 years ago
garycourt commented 13 years ago

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.

dvv commented 13 years ago

Nevermind, I respect your "no".