feat: add discriminator support for JSON schema validation#292
feat: add discriminator support for JSON schema validation#292aeschli merged 4 commits intomicrosoft:mainfrom
Conversation
|
@aeschli , Can you please look into this PR when you have some time. Thanks a lot |
|
It's a VS Code only extension of schemas and only schemas with that new 'discriminator' property would profit. We have other VS Code specific schemas extensions, but they are all in the space of improving the editing experience for our settings editors. We don't want to propagate them, and we don't want to create a new flavor of schemas. Ideally the 'discriminator' could be computed when loading the schema. I'd prefer such a solution |
89c8a5b to
3c7aac5
Compare
3c7aac5 to
d8e9b80
Compare
|
@aeschli , Thanks for the feedback. I have removed the discriminator property since it deviates from official JSON schema, and i have added now an I have also added a test case named |
|
Thanks @mohankumarelec. I polished the code a bit, hope that's ok. |
Add Discriminator Support for JSON Schema Validation
Overview
This PR introduces discriminator support for JSON schema validation, significantly improving performance for schemas using
oneOf,anyOf, andallOfby intelligently selecting which schema alternatives to test based on a discriminator property.Problem Solved
Without discriminators, the validator must test all schema alternatives in
oneOf/anyOf/allOf, leading to exponential complexity in deeply nested or self-referencing schemas. This causes severe performance degradation and can even make validation impractically slow for certain schema patterns.See the test case
self-referencing schema with anyOf and deep nesting (exploding complexity test)for a concrete example.Implementation
Added discriminator support with two modes:
propertyName: Discriminate based on an object property (e.g.,"type": "cat")propertyIndex: Discriminate based on the first N properties or array itemsThe discriminator can include an explicit
mappingobject to map discriminator values to schema indices, or auto-detect based onconstvalues in the alternative schemas.Example:
Test Coverage
Added tests for:
propertyNameandpropertyIndexoneOf,anyOfPerformance Impact
For schemas with discriminators, validation performance improves significantly. This is especially critical for self-referencing schemas with deep nesting.