Skip to content
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

Open
abcd-ca opened this issue Sep 6, 2013 · 6 comments

Comments

@abcd-ca
Copy link

abcd-ca commented Sep 6, 2013

No serialization. This works:

var Duck = schema({              // A duck
  swim : Function,               //  - can swim
});

var myDuck = { swim : function() {}, quack : function() {}, age : 2, color : 'yellow' };

Duck(myDuck);

Now, serialize then deserialize the Duck validator and the validator fails:

var Duck = schema({              // A duck
  swim : Function,               //  - can swim
});

var myDuck = { swim : function() {}, quack : function() {}, age : 2, color : 'yellow' };

//serialize
var objVersion = Duck.toJSON()

//deserialize
DuckReborn = schema.fromJSON(objVersion);

DuckReborn(myDuck);

I think this is because swim gets serialized as {} rather than as something that the fromJSON function can identify it as instanceof Function

I can tell you that the offending line of code seems to be in the toJSON function just a few lines after json.type = "object"; where it says json.properties[property.key] = property.value.toJSON();. Here, property.value.constructor is a function but the return value from property.value.toJSON() is an empty object

@molnarg
Copy link
Owner

molnarg commented Sep 7, 2013

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.

@abcd-ca
Copy link
Author

abcd-ca commented Sep 7, 2013

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

@abcd-ca
Copy link
Author

abcd-ca commented Sep 7, 2013

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.

@molnarg
Copy link
Owner

molnarg commented Sep 8, 2013

Reading the JSON Schema standard again, I discovered that extensions are supported quite well:

Implementations MAY choose to define additional keywords to JSON Schema. Save for explicit agreement, schema authors SHALL NOT expect these additional keywords to be supported by peer implementations. Implementations SHOULD ignore keywords they do not support.

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.

@molnarg
Copy link
Owner

molnarg commented Sep 8, 2013

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 errors(object) method would return a tree of errors with the same structure as the original object.

It would be implemented with an additional errors() function on each type of validation. It would return these detailed error reports: simple string for simple validations, or object with nested error reports for compound types (like object or array).

@parhelium
Copy link
Contributor

Attempt to implement errors API was done with pull request: #15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants