sagold / json-schema-library

Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation
MIT License
164 stars 19 forks source link

Top level "oneOf" does not validate correctly #33

Closed Carl-Foster closed 1 year ago

Carl-Foster commented 1 year ago

Reproduce

const draft = new Draft07();
assert.strict.notDeepEqual(draft.validate(true, { oneOf: [{type: string}, {type: number}]}, [])

Expectation

Validation should properly validate a top level schema that does not specify a top level type

Notes

Edit

On further inspection, this may only occur when non-object values are passed with the "oneOf" top level schema.

sagold commented 1 year ago

Hi.

Thank you for reporting this issue. I can confirm that oneOf resolution on root level does not invalidate the input, but if it is contained on an object it does. This is inconsistend and I will fix this.

Note that json-schema-library usually requires a defined property type. If you pass in a schema with an type-array, the validation works as expected. Still, type-arrays are not fully supported in helper functions yet (e.g. getTemplate), so this might not be an option for you:

draft = new Draft07({
    type: ["string", "number"],
    oneOf: [{ type: "string" }, { type: "number" }]
});
const valid = draft.validate([]);
assert.notEqual(valid.length, 0);
sagold commented 1 year ago

I have published a patch to adress this issue with json-schema-library@7.4.6 and json-schema-library@8.0.0-rc5.

Cheers.