-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
serialize/deserialize schema causes validator to fail when Function type is used #8
Comments
The thing is, that checking for 'function' type is something that is not supported in JSON Schema format (see this list of primitive types). :( One possible solution would be creating JSON Schema extensions for these kind of things, but I don't want to go that way. It would possibly make the serialization output unreadable for other JSON Schema implementations. A better solution would be to properly inform the user of the API of the information loss, and to handle these cases gracefully. For example, a serialization/deserialization cycle should always result in a valid js-schema function (although maybe not identical to the original). Do you really need to serialize this schema? You could maybe get away with a less strict schema that is serializable, or rebuilding the schema every time instead of serialization/deserialization. |
I was serializing the schema so that I could include it in the same JSON document that it validates. I was also thinking of having a form that a user fills out that then creates my JSON document and includes its serialized schema in that document so that the back-end can see how to validate the document when it uses it to create a visualization. So user enters the data, back-end visualizes it. This is for some kind of user-configurable dashboard of widgets. Alternatively I guess I could save only the schema version number and the back-end could retrieve the a corresponding JS class with the schema validator in it, unserialized. I do think we need to at least document that the serialization methods don't work for the Duck example provided in the readme |
How would you tackle some simple validation as well? It would be nice to know at least which part of my document failed to validate if not why as well. I might be into adding this functionality if you can point me in the right direction – maybe something that could be switched on with debug = true or something if you're concerned about performance. |
Reading the JSON Schema standard again, I discovered that extensions are supported quite well:
A simple extension to the serialization would be a warning message that this schema could not be serialized perfectly. So that when you look at the serialized version, you can see explicitly that there was information loss. Later, maybe js-schema specific extensions could be defined, but that's more work. As for your usecase: if you validate JSON documents, you would in theory never need the 'function type' validation, because JSON can not contain functions. |
Validating and then knowing which fields failed is something I planned for a long time, but did not get to implementing it in the end due to lack of time. It would be awesome if you could work on this! I think it should be an extension to the current API, something like this: var Duck = schema({ // A duck
swim : Function, // - can swim
quack : Function, // - can quack
age : Number.min(0).max(5), // - is 0 to 5 years old
color : ['yellow', 'brown'] // - has either yellow or brown color
});
var myDuck = { swim : function() {}, quack : function() {}, age : 2, color : 'yellow' };
console.log(Duck(myDuck)); // true
console.log(Duck.errors(myDuck)); // false
var myCat = { walk : function() {}, purr : function() {}, age : 3, color : 'black' };
console.log(Duck(myDuck)); // false
console.log(Duck.errors(myDuck)); // { swim: 'should be a function', quack: 'should be a function' } So the It would be implemented with an additional |
Attempt to implement errors API was done with pull request: #15 |
No serialization. This works:
Now, serialize then deserialize the Duck validator and the validator fails:
I think this is because swim gets serialized as
{}
rather than as something that the fromJSON function can identify it asinstanceof Function
I can tell you that the offending line of code seems to be in the
toJSON
function just a few lines afterjson.type = "object";
where it saysjson.properties[property.key] = property.value.toJSON();
. Here,property.value.constructor
is a function but the return value fromproperty.value.toJSON()
is an empty objectThe text was updated successfully, but these errors were encountered: