Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/29419-list-comparand-operator-predicate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/spec': patch
'@objectstack/service-automation': patch
---

feat(spec): `@objectstack/spec/data` exports `isListComparandOperator`, and a flow's record nodes read it instead of a local copy of the list operators

Clause-②: yes
Metadata-protocol: none

- **`@objectstack/spec/data`** now exports `isListComparandOperator(op)`: `true` exactly for the field operators whose declared comparand is a list (`$in`, `$nin`, `$between`), `false` for every other spelling. It reads the same split the comparand-type and comparand-shape faces judge with, so a consumer that judges a comparand by its operator no longer keeps its own copy. The two operator sets behind it stay unexported.
- **`@objectstack/service-automation`**: the `get_record`, `update_record` and `delete_record` nodes decide where an evaluated list is accepted in a filter with that predicate. Which operands are accepted or refused is unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { VALUE_SLOT_TEMPLATE_REFUSAL } from '@objectstack/spec/automation';
import { FieldOperatorsSchema } from '@objectstack/spec/data';
import { AutomationEngine } from '../engine.js';
import { registerCrudNodes } from './crud-nodes.js';
import { assertEngineFindOnePredicate } from '@objectstack/metadata-core';
Expand Down Expand Up @@ -395,22 +394,6 @@ describe('a computed filter operand is a value, never filter structure', () => {
expect(data.seen[verb]).toEqual({ id: { $in: ['r1', 'r2'] }, stage: { $nin: ['lost', 'won'] }, amount: { $between: [10, 90] } });
});

it('every declared operator whose comparand is a list takes an evaluated list, and no other does', async () => {
const operators = Object.keys((FieldOperatorsSchema as unknown as { shape: Record<string, unknown> }).shape);
expect(operators.length, 'control: the grammar declares its operators').toBeGreaterThan(10);
const pair = ['2026-01-01', '2026-12-31'];
for (const op of operators) {
const takesList = FieldOperatorsSchema.safeParse({ [op]: pair }).success;
data = fakeData();
engine = new AutomationEngine(createTestLogger());
registerCrudNodes(engine, ctxWith(data.service));
engine.registerFlow('crud_flow', crudFlow('get_record', { objectName: 'deal', filter: { due: { [op]: CEL('record.pair') } } }));
const result = await engine.execute('crud_flow', { record: { pair } } as any);
expect(result.success, `${op}: the grammar ${takesList ? 'takes' : 'refuses'} a list there`).toBe(takesList);
expect(data.seen.find, `${op}: the data engine was asked`).toEqual(takesList ? { due: { [op]: pair } } : undefined);
}
});

it.each(NODES)('%s: an evaluated scalar passes — a string, a number, a boolean and a date', async (nodeType, verb, extra) => {
const { result } = await run(nodeType, {
objectName: 'deal',
Expand Down
20 changes: 5 additions & 15 deletions packages/services/service-automation/src/builtin/crud-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
DeleteRecordConfigParsed,
} from '@objectstack/spec/automation';
import type { AutomationContext, IDataEngine } from '@objectstack/spec/contracts';
import { isAcceptedFilterComparand, type DroppedFieldsEvent } from '@objectstack/spec/data';
import { isAcceptedFilterComparand, isListComparandOperator, type DroppedFieldsEvent } from '@objectstack/spec/data';
import { StandardErrorCode } from '@objectstack/spec/api';
import { isStoredMetadataBodyObject, STORED_METADATA_BODY_PRESCRIPTION } from '@objectstack/spec/kernel';
import {
Expand Down Expand Up @@ -149,16 +149,6 @@ function resolveNodeFilter(
return { filter };
}

/**
* The operators whose comparand is a LIST — the split
* `@objectstack/spec`'s comparand faces read (`LIST_COMPARAND_OPERATORS` in
* `data/filter-comparand-operators.ts`, kept out of the published barrel on
* purpose). `crud-filter-guard.test.ts` reconciles this set against
* `FieldOperatorsSchema`: every declared operator whose enforced slot takes a
* list is here, and no other.
*/
const LIST_COMPARAND_OPERATORS: ReadonlySet<string> = new Set(['$in', '$nin', '$between']);

/**
* What an evaluated filter operand is when it is NOT a value its position
* takes, worded for the refusal (`an object`, `a list`, `a list whose entry
Expand All @@ -168,9 +158,9 @@ const LIST_COMPARAND_OPERATORS: ReadonlySet<string> = new Set(['$in', '$nin', '$
* (`isAcceptedFilterComparand`, `@objectstack/spec/data`: a string, number,
* bigint, boolean, `null` or `Date`); an operand that IS `null` never gets
* here, the nothing guard owns it. At the comparand of a list operator
* ({@link LIST_COMPARAND_OPERATORS}: the operand's path ends in that
* operator) a list of such values is one too, provided no entry is `null`:
* an entry that is nothing is refused as the nothing guard refuses an operand
* (`isListComparandOperator`, `@objectstack/spec/data`: the operand's path
* ends in that operator) a list of such values is one too, provided no entry
* is `null`: an entry that is nothing is refused as the nothing guard refuses an operand
* that is nothing, since it adds "the field is empty" to the condition, a
* match the author never wrote. Every other position takes one
* value: a field's implicit equality, a scalar operator's comparand, a list
Expand All @@ -184,7 +174,7 @@ function operandValueRefusal(value: unknown, path: readonly (string | number)[])
if (isAcceptedFilterComparand(value)) return undefined;
if (!Array.isArray(value)) return kindOfOperand(value);
const last = path[path.length - 1];
if (typeof last !== 'string' || !LIST_COMPARAND_OPERATORS.has(last)) return 'a list';
if (typeof last !== 'string' || !isListComparandOperator(last)) return 'a list';
const index = value.findIndex((member) => member === null || !isAcceptedFilterComparand(member));
return index < 0 ? undefined : `a list whose entry [${index}] is ${kindOfOperand(value[index])}`;
}
Expand Down
1 change: 1 addition & 0 deletions packages/spec/api-surface/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@
"isInlineRowFormOffered (function)",
"isKnownFilterToken (function)",
"isLegacyApiMethod (function)",
"isListComparandOperator (function)",
"isMaskedOnReadFieldType (function)",
"isMultiValueField (function)",
"isNowDefaultToken (function)",
Expand Down
1 change: 1 addition & 0 deletions packages/spec/export-origins/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@
"isInlineRowFormOffered": "src/data/inline-grid-columns.ts#isInlineRowFormOffered (function)",
"isKnownFilterToken": "src/data/context-tokens.zod.ts#isKnownFilterToken (function)",
"isLegacyApiMethod": "src/data/api-derivation.ts#isLegacyApiMethod (function)",
"isListComparandOperator": "src/data/filter-comparand-operators.ts#isListComparandOperator (function)",
"isMaskedOnReadFieldType": "src/data/masked-field-types.ts#isMaskedOnReadFieldType (function)",
"isMultiValueField": "src/data/field-value.zod.ts#isMultiValueField (function)",
"isNowDefaultToken": "src/data/default-value-tokens.ts#isNowDefaultToken (function)",
Expand Down
32 changes: 32 additions & 0 deletions packages/spec/src/data/filter-comparand-operators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* `isListComparandOperator`, the one piece of the operator split the `data`
* barrel publishes. A consumer reads it instead of keeping its own copy of
* the list half, so this file holds it to the enforced grammar: it answers
* `true` exactly for the operators whose `FieldOperatorsSchema` slot accepts
* a list, and a list operator added to the grammar reds here.
*/

import { describe, it, expect } from 'vitest';
import { FieldOperatorsSchema, isListComparandOperator } from './index';

describe('isListComparandOperator — the published list half of the operator split', () => {
const declared = Object.keys(FieldOperatorsSchema.shape);

it('answers true exactly for every declared operator whose enforced slot takes a list', () => {
expect(declared.length, 'control: the grammar declares its operators').toBeGreaterThan(10);
const pair = ['2026-01-01', '2026-12-31'];
for (const op of declared) {
const takesList = FieldOperatorsSchema.safeParse({ [op]: pair }).success;
expect(isListComparandOperator(op), `${op}: the grammar ${takesList ? 'takes' : 'refuses'} a list`).toBe(takesList);
}
expect(declared.filter(isListComparandOperator).sort()).toEqual(['$between', '$in', '$nin']);
});

it('answers false for a spelling the grammar does not declare', () => {
for (const op of ['in', 'between', '$and', '$or', '$not', '$IN', '', 'has']) {
expect(isListComparandOperator(op), op).toBe(false);
}
});
});
26 changes: 19 additions & 7 deletions packages/spec/src/data/filter-comparand-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
* set to the schema's other half: every declared operator whose enforced slot
* refuses an array.
*
* ## Why it is NOT in the `data` barrel
* ## What the `data` barrel publishes from here: one predicate
*
* Nothing here is a contract a consumer calls; the faces are, and they are
* already published (`normalizeFilterComparandTypes`,
* `assertListComparandShapes`). Exporting the split would widen
* `@objectstack/spec/data` for no reader, so this file stays out of
* `./index.ts`, as `./filter-comparand-refusal-text.ts` does. It imports
* nothing, so no face can reach a cycle through it.
* The faces are the contract most consumers call, and they are published on
* their own (`normalizeFilterComparandTypes`, `assertListComparandShapes`).
* A consumer that judges a comparand by its operator without running a face
* (a flow's record nodes take an evaluated list only at a list operator's
* comparand) reads {@link isListComparandOperator}, so it holds no copy of
* the split to drift. The barrel names that predicate alone: the two sets
* stay module-private, because a published `ReadonlySet` is still a live
* `Set` at run time, and a caller that cast and mutated it would change what
* every face here judges. This file imports nothing, so no face can reach a
* cycle through it.
*/

/** The operators whose declared comparand is ONE literal. */
Expand All @@ -43,3 +47,11 @@ export const SCALAR_COMPARAND_OPERATORS: ReadonlySet<string> = new Set([
export const LIST_COMPARAND_OPERATORS: ReadonlySet<string> = new Set([
'$in', '$nin', '$between',
]);

/**
* Whether `op` (a `$`-spelled field operator) declares a LIST comparand
* (`$in`, `$nin`, `$between`); `false` for every other spelling.
*/
export function isListComparandOperator(op: string): boolean {
return LIST_COMPARAND_OPERATORS.has(op);
}
5 changes: 5 additions & 0 deletions packages/spec/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export * from './filter-comparand-type';
// `assertListComparandShapes` now delegates here and `parseFilterAST` enforces
// it for every direct driver caller too.
export * from './filter-comparand-shape';
// Which field operators declare a LIST comparand (`$in`, `$nin`, `$between`),
// as a predicate over the split both faces above read, for a consumer that
// judges a comparand by its operator without running a face. The two sets
// themselves stay module-private (the module says why).
export { isListComparandOperator } from './filter-comparand-operators';
// Canonical conformance cases for the filter logical combinators — the shared
// standard the five independent FilterCondition backends are each checked
// against, so they cannot drift apart again (#3774; the fifth — MongoDB's
Expand Down
Loading