Skip to content

Commit

Permalink
fix: ensure "exists" js interpreter returns value depends on parent f…
Browse files Browse the repository at this point in the history
…ield existance for nested fields
  • Loading branch information
serhiistotskyi committed Jan 15, 2024
1 parent bd61a28 commit ec1c7d6
Show file tree
Hide file tree
Showing 3 changed files with 3,378 additions and 2,241 deletions.
9 changes: 9 additions & 0 deletions packages/js/spec/interpreters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ describe('Condition Interpreter', () => {
expect(interpret(condition, { address: { building: 1 } })).to.be.true
})

it('can check that nested property does not exist', () => {
const condition = new Field('exists', 'group.objectId', false)

expect(interpret(condition, {})).to.be.true
expect(interpret(condition, { group: {} })).to.be.true
expect(interpret(condition, { group: null })).to.be.true
expect(interpret(condition, { group: { objectId: 1 } })).to.be.false
})

it('checks that at least one item from array satisfies condition', () => {
const condition = new Field('exists', 'items.age', true)

Expand Down
5 changes: 4 additions & 1 deletion packages/js/src/interpreters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const exists: Interpret<Field<boolean>> = (node, object, { get }) => {
}

const [item, field] = getObjectFieldCursor<{}>(object, node.field, get);
const test = (value: {}) => !!value && value.hasOwnProperty(field) === node.value;
const test = (value: {}) => {
if (value == null) return Boolean(value) === node.value;
return value.hasOwnProperty(field) === node.value;
}

return isArrayAndNotNumericField(item, field) ? item.some(test) : test(item);
};
Expand Down
Loading

0 comments on commit ec1c7d6

Please sign in to comment.