json-schema-filter
json-schema-filter copied to clipboard
Question: null input
What do you think of adding code to this library to check that the input is an object? Right now, the code below would throw an error:
var schema = {
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
}
}
};
filter(schema, null);
filter(schema, 'mystring');
For my use case, I'm going to add code like the following before calling filter:
if (typeof input !== 'object') {
return {}; // or maybe return null?
}
Want it in this library? Any extra checks will decrease performance, so maybe it belongs outside this library.
ariutta, thanks for contributing.
the above use case, to the best of my knowledge does not throw an error, it will simply return the string "mystring". I assume the first call would throw an error though... in fact would it not just return 'null'?
As I've mentioned in my readme, it is usually expected that prior to calling json-schema-filter one would first validate at some point the the document against the schema, or some other schema. Therefore I would consider you last comment very valid, that is keeping it out for performance, and validating it just before, or filtering out nulls etc. Yup.
Does this make sense?
Or best said: "json-schema-filter assumes your input is valid for performance reasons, therefore validate or check your input before or else considering every bad condition would decrease performance and potentially duplicate validation where this should be flexible dependent on your implementation"
Sure, that sounds good. Thanks!
On Tue, Oct 14, 2014 at 9:52 AM, Andrew Lank [email protected] wrote:
Or best said: "json-schema-filter assumes your input is valid for performance reasons, therefore validate or check your input before or else considering every bad condition would decrease performance and potentially duplicate validation where this should be flexible dependent on your implementation"
— Reply to this email directly or view it on GitHub https://github.com/alank64/json-schema-filter/issues/6#issuecomment-59078122 .