Closed kklash closed 9 months ago
The error seems to originate at this line:
fix(functions.deepEqual(stat.type, ['object']), 'has to be checked for type:', 'object')
I logged the value of stat
while running my demo test:
stat: {
type: null,
items: 0,
properties: [ 'method', 'type' ],
patterns: [],
required: [ 'method' ],
fullstring: false,
dyn: { item: false, items: 0, properties: [Array], patterns: [] },
unknown: false
}
Seems like stat
is not properly kept in sync with the current validation node?
@ChALkeR Can you take a look?
I see the error and a there is a work-around, working on a fix.
There are three typecheck levels here:
1) top-level 2) discriminator oneOf branches 3) discriminator oneOf -> discriminator oneOf branches.
@olistic @kklash https://github.com/ExodusMovement/schemasafe/pull/177 should stop failing now if type: object
is added to lvl 2 or level 3 subbranches, in addition or without the lvl 1 check.
Without #177 (i.e. on current releases), removing lvl1 check and leaving only 2/3 or a combo of them will also work.
A future fix #179 is needed to be able to specify only lvl 1 typecheck, it needs some testing to ensure nothing is broken.
Explanation:
Each discriminator expects either itself or each of it sub-branches to be typechecked to object
.
Nested useless typechecks are ignored, but branches didn't report them as typechecked in the stat
/delta
info objects.
So, when lvl 1 typecheck was present, the safeguard in the second discriminator didn't see that all the branches are already checked. This happened regardless of if the second discriminator or it subbranches had type: object
checks because those were short-circuited due to the presence of the typecheck check on lvl 1 which guaranteed that already and the actual validation logic traced that.
Side note: you probably want unevaluatedProperties
, not additionalProperties
, as the latter one cares only about sibling, not nested properties rules, and will fail on e.g. {type:'request', method: 'dance'}
as it only expects type
.
Thanks for the tip @ChALkeR! Moving type: object
down to the oneOf
branches indeed solves the issue. :heavy_check_mark:
Context
Consider an object which can have three different shapes:
To validate such an object, i first set a
discriminator
on thetype
property, and branched based on that. Then, within thetype: 'request'
branch ofoneOf
, i specified anotherdiscriminator
on themethod
property. However, Schemasafe fails to compile in this scenario, throwing the following error:Note that we clearly did check the node for
type: 'object'
before applying thediscriminator
keyword.Demo
This test case demonstrates the problem:
Compilation fails even if
type: 'object'
is added to both of the relevantoneOf
branches.