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 "properties" and "required" compilers in loose and…
Browse files Browse the repository at this point in the history
… boolean mode

Correct the output for the boolean values of the JSONSchema7Definition (e.g. "property": true) and
make a few adjustments to the loose mode (condition was a bit wrong).
  • Loading branch information
pheekus committed Sep 13, 2019
1 parent 02d23f4 commit 18905c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions src/compile-type/compile-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ export default function(schema: JSONSchema7, ref: string, strictRef: string): st
const value = properties[key];
const valueRef = cel.ref(ref, key);

if (typeof value === 'boolean') {
guard = cel.calc(guard, '&&', valueRef);
} else if (typeof value === 'object') {
let valueGuard = compile(value, valueRef, strictRef);

if (requiredProperties.has(key)) {
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);
}

guard = cel.calc(guard, '&&', valueGuard);
}
const loose = cel.calc(strictRef, '==', cel.val(false));
const includesKey = cel.call(cel.ref(actualKeys, 'hasAll'), cel.val([key]));
const doesNotIncludeKey = cel.calc(includesKey, '==', cel.val(false));

const skipAssert = requiredProperties.has(key)
? cel.calc(loose, '&&', doesNotIncludeKey)
: doesNotIncludeKey;

const valueGuard = typeof value === 'boolean'
? value
? includesKey
: doesNotIncludeKey
: compile(value, valueRef, strictRef);

guard = cel.calc(guard, '&&', cel.calc(skipAssert, '||', valueGuard));
});

if (typeof schema.minProperties === 'number') {
Expand Down
6 changes: 3 additions & 3 deletions test/compile-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ describe('type compilers', () => {
it('supports "properties"', () => {
assert.equal(
compileObject({ type: 'object', properties: { a: true, b: true }}, 'ref', 's'),
'((((ref is map)&&ref.keys().hasOnly(["a","b"]))&&ref.a)&&ref.b)'
'((((ref is map)&&ref.keys().hasOnly(["a","b"]))&&((ref.keys().hasAll(["a"])==false)||ref.keys().hasAll(["a"])))&&((ref.keys().hasAll(["b"])==false)||ref.keys().hasAll(["b"])))'
);
assert.equal(
compileObject({ type: 'object', properties: { a: { type: 'boolean' }}}, 'ref', 's'),
'(((ref is map)&&ref.keys().hasOnly(["a"]))&&(ref.a&&(ref.a is bool)))'
'(((ref is map)&&ref.keys().hasOnly(["a"]))&&((ref.keys().hasAll(["a"])==false)||(ref.a is bool)))'
);
});

Expand All @@ -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.keys().hasAll(["a"]))&&(ref.a is bool)))'
'(((ref is map)&&ref.keys().hasOnly(["a"]))&&(((s==false)&&(ref.keys().hasAll(["a"])==false))||(ref.a is bool)))'
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('function compiler', () => {
assert.equal(
compile([{ $id: 'Sample', type: 'object', properties: { a: true }}]),
[
'function _s0(d,s){return (((d is map)&&d.keys().hasOnly(["a"]))&&d.a)}',
'function _s0(d,s){return (((d is map)&&d.keys().hasOnly(["a"]))&&((d.keys().hasAll(["a"])==false)||d.keys().hasAll(["a"])))}',
'function _s(n,d,s){return ((n=="Sample")&&_s0(d,s))}',
'function isValid(n){return _s(n,request.resource.data,(request.method=="create"))}'
].join('')
Expand Down

0 comments on commit 18905c5

Please sign in to comment.