Repository navigation
fix type error handler to handle allOf conflicting type - #101
Conversation
Signed-off-by: Diya <diyasrivastava2023@gmail.com>
jdesrosiers
left a comment
There was a problem hiding this comment.
I left a couple notes and I pushed some additional tests to expose some gaps we need to fill. There were two main issues. One is dealing with "integer" because it's not a true JSON type and has some overlap with "number". The other issue is about removing duplicate type messages. There should only ever be one per schema.
Signed-off-by: Diya <diyasrivastava2023@gmail.com>
| const errors = []; | ||
|
|
||
| if (normalizedErrors["https://json-schema.org/keyword/type"]) { | ||
| let allowedTypes = new Set(ALL_TYPES); |
There was a problem hiding this comment.
There's no reason to create a new set every time you initialize allowedTypes. ALL_TYPES can be a set.
There was a problem hiding this comment.
I've changed that, ALL_TYPES is now a set instead of an array
There was a problem hiding this comment.
This is still creating a new set unnecessarily.
| let allowedTypes = new Set(ALL_TYPES); | |
| let allowedTypes = ALL_TYPES; |
jdesrosiers
left a comment
There was a problem hiding this comment.
Almost there. A couple more things to clean up.
| const errors = []; | ||
|
|
||
| if (normalizedErrors["https://json-schema.org/keyword/type"]) { | ||
| let allowedTypes = new Set(ALL_TYPES); |
There was a problem hiding this comment.
This is still creating a new set unnecessarily.
| let allowedTypes = new Set(ALL_TYPES); | |
| let allowedTypes = ALL_TYPES; |
Hey @jdesrosiers Since I’m used to C++, where assignment makes a deep copy, I had to remind myself that JavaScript Set works differently. Here assigning a Set just passes the reference and any change to allowedType would also mutate ALL_TYPES ,but if we look at our code then any mutation never happens on ALL_TYPE because we are performing intersection on allowedTypes before any mutation which returns a new Set anyways hence the code works just fine. But i still think we should stick to Example: |
|
@srivastava-diya, I appreciate the effort you're putting into code clarity. I see what you mean, but I think it's clear enough without recreating the set. It feels wrong to me to recreate the set and I don't think it takes too much to understand why it works. |
I understand your point @jdesrosiers . And i've done what you suggested. |
Add Conflicting Type Detection to type Error Handler
This update implements support for detecting conflicting type constraints entirely within the type error handler, as recommended. The previously skipped “
allOfconflicting type” test passes with this change in type error handler.This fixes : #99
To keep the existing
typelogic isolated and safe, the changes made are as follows:Starting with a set of all allowed JSON types:
let allowedTypes = new Set(["null", "boolean", "number", "string", "array", "object", "integer"]);now we intersect it with the type set from each type keyword found at the same instance location. If the resulting set becomes empty, that means the schema’s type constraints are mutually exclusive, so I return a single getConflictingTypeMessage(). If the intersection is not empty, then it isn’t a conflict and the normal type-error handling continues as usual.
This keeps allOf functioning purely as a simple applicator while placing all conflict detection logic inside the type handler, as intended.
I’ve implemented this and run the full test suite — all tests pass.