json-schema-filter icon indicating copy to clipboard operation
json-schema-filter copied to clipboard

Question: null input

Open ariutta opened this issue 11 years ago • 3 comments

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 avatar Oct 14 '14 16:10 ariutta

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?

alank64 avatar Oct 14 '14 16:10 alank64

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"

alank64 avatar Oct 14 '14 16:10 alank64

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 .

ariutta avatar Oct 15 '14 15:10 ariutta