Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
fix(compiler): fix loose property checks on objects
Browse files Browse the repository at this point in the history
CEL doesn't allow accessing undefined properties, so it has to be done via .keys() and .hasAll()
checks instead.

fix #13
  • Loading branch information
pheekus committed Sep 13, 2019
1 parent b11dc4d commit 447ef08
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/compile-type/compile-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default function(schema: JSONSchema7, ref: string, strictRef: string): st
let valueGuard = compile(value, valueRef, strictRef);

if (requiredProperties.has(key)) {
const definedOrStrict = cel.calc(strictRef, '||', valueRef);
const includesKey = cel.call(cel.ref(actualKeys, 'hasAll'), cel.val([key]))
const definedOrStrict = cel.calc(strictRef, '||', includesKey);
valueGuard = cel.calc(definedOrStrict, '&&', valueGuard);
} else {
valueGuard = cel.calc(valueRef, '&&', valueGuard);
Expand Down
2 changes: 1 addition & 1 deletion test/compile-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ describe('type compilers', () => {
it('supports "required"', () => {
assert.equal(
compileObject({ type: 'object', properties: { a: { type: 'boolean' }}, required: ['a']}, 'ref', 's'),
'(((ref is map)&&ref.keys().hasOnly(["a"]))&&((s||ref.a)&&(ref.a is bool)))'
'(((ref is map)&&ref.keys().hasOnly(["a"]))&&((s||ref.keys().hasAll(["a"]))&&(ref.a is bool)))'
);
});
});
Expand Down

0 comments on commit 447ef08

Please sign in to comment.