Skip to content

Commit 33ac332

Browse files
authored
fix: enhance unevaluatedProperties handling for composite rules (#28)
1 parent 50f2525 commit 33ac332

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

lib/vocabularies/unevaluated/unevaluatedProperties.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ const def: CodeKeywordDefinition = {
4747
: gen.if(unevaluatedStatic(props, key), () => unevaluatedPropCode(key))
4848
)
4949

50-
if (isForced && it.errorPath.emptyStr()) {
50+
if (isForced && it.errorPath.emptyStr() && !it.compositeRule) {
5151
// $refs are compiled into functions
5252
// We need to check in runtime if function was called from allOf.
5353
// We need to check only on the top level of the function:
5454
// it is ensured with `it.errorPath.emptyStr()` check
5555
gen.if(_`${N.isAllOfVariant} === 0`, staticCheck)
5656
} else {
57-
staticCheck()
57+
if (!it.compositeRule || cxt.schema !== undefined) {
58+
staticCheck()
59+
}
5860
}
5961
}
6062

spec/default_unevaluated_properties.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,75 @@ describe("defaultUnevaluatedProperties=false option", function () {
191191
assertInvalid(schemas, {foo: {baz: 1}})
192192
assertInvalid(schemas, {foo: {bar: "string", baz: 1}})
193193
})
194+
195+
it("should work correctly with anyOf", () => {
196+
const schema1 = {
197+
type: "object",
198+
properties: {
199+
address: {type: "string"},
200+
},
201+
anyOf: [
202+
{
203+
type: "object",
204+
required: ["firstName", "lastName"],
205+
properties: {
206+
firstName: {type: "string"},
207+
lastName: {type: "string"},
208+
},
209+
},
210+
{
211+
type: "object",
212+
required: ["name"],
213+
properties: {
214+
name: {type: "string"},
215+
},
216+
},
217+
],
218+
}
219+
220+
const schemas = [schema1]
221+
assertInvalid(schemas, {})
222+
assertValid(schemas, {address: "someWhere", firstName: "John", lastName: "Doe"})
223+
assertValid(schemas, {address: "someWhere", name: "John Doe"})
224+
assertInvalid(schemas, {address: "someWhere", firstName: "John"})
225+
assertInvalid(schemas, {address: "someWhere", foo: 1})
226+
assertValid(schemas, {firstName: "John", lastName: "Doe"})
227+
})
228+
229+
it("should work correctly with oneOf", () => {
230+
const schema1 = {
231+
type: "object",
232+
properties: {
233+
address: {type: "string"},
234+
},
235+
oneOf: [
236+
{
237+
type: "object",
238+
required: ["firstName", "lastName"],
239+
properties: {
240+
firstName: {type: "string"},
241+
lastName: {type: "string"},
242+
},
243+
},
244+
{
245+
type: "object",
246+
required: ["name"],
247+
properties: {
248+
name: {type: "string"},
249+
},
250+
},
251+
],
252+
}
253+
254+
const schemas = [schema1]
255+
256+
assertInvalid(schemas, {})
257+
assertValid(schemas, {address: "someWhere", firstName: "John", lastName: "Doe"})
258+
assertValid(schemas, {address: "someWhere", name: "John Doe"})
259+
assertInvalid(schemas, {firstName: "John", lastName: "Doe", name: "JD"})
260+
assertInvalid(schemas, {address: "someWhere", foo: 1})
261+
assertValid(schemas, {firstName: "John", lastName: "Doe"})
262+
})
194263
})
195264

196265
describe("validation with $refs", () => {

0 commit comments

Comments
 (0)