@@ -19,6 +19,7 @@ import type {
1919 AggregateResult ,
2020} from '@object-ui/types' ;
2121import {
22+ ACCEPTED_FILTER_COMPARAND_TYPES_SENTENCE ,
2223 asciiCaseInsensitiveContains ,
2324 canonicalAstOperator ,
2425 FILTER_OPERATORS ,
@@ -135,6 +136,20 @@ const DOLLAR_FLAG_OPERATORS = new Set(['$null', '$exists', '$empty']);
135136 */
136137const DOLLAR_COMPARAND_OPERATORS : ReadonlySet < string > = new Set ( FILTER_OPERATORS ) ;
137138
139+ /**
140+ * The operators that compare their comparand by EQUALITY, MEMBERSHIP or TEXT
141+ * rather than by order, in each dialect. Under these a class instance is
142+ * refused (see {@link classInstanceComparandRefusalReason}); under the
143+ * orderings and `$between` it is compared through its `valueOf`. `$icontains`
144+ * is not here: its own text door already refuses every non-string comparand.
145+ */
146+ const DOLLAR_BY_VALUE_OPERATORS : ReadonlySet < string > = new Set ( [
147+ '$eq' , '$ne' , '$in' , '$nin' , '$contains' , '$notContains' , '$startsWith' , '$endsWith' ,
148+ ] ) ;
149+ const AST_BY_VALUE_OPERATORS : ReadonlySet < string > = new Set ( [
150+ '=' , '!=' , 'in' , 'nin' , 'contains' , 'not_contains' , 'starts_with' , 'ends_with' ,
151+ ] ) ;
152+
138153/**
139154 * Is this stored value EMPTY, in the sense the spec's `$empty` operator gives
140155 * the word (objectui#11094)?
@@ -503,6 +518,9 @@ function nestedRelationRefusalReason(field: string, key: string): string {
503518 * The first key of an AST comparand that is a nested relation condition, or
504519 * `undefined` when it is not one. The shape is the one `convertFiltersToAST`
505520 * lowers to `[rel, '=', condition]`: a non-empty PLAIN object with no `$` key.
521+ * The two name one population: the converter's operator-map test is the same
522+ * prototype test, so a plain object of another realm is a comparand to both,
523+ * refused by the converter and by {@link objectComparandRefusal} here.
506524 *
507525 * Plain means prototype `Object.prototype` or `null`, the test the spec's
508526 * filter walk uses to tell filter structure from data. A class instance is
@@ -530,6 +548,16 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
530548 return proto === Object . prototype || proto === null ;
531549}
532550
551+ /**
552+ * The refusal for a flag operator (`$null`, `$exists`, `$empty`) whose flag is
553+ * not a boolean. `SpecialOperatorSchema` declares all three `z.boolean()`, and
554+ * the spec's save door and every query face refuse anything else.
555+ */
556+ function nonBooleanFlagRefusalReason ( field : string , operator : string , flag : unknown ) : string {
557+ return `filter operator '${ operator } ' on field '${ field } ' takes a boolean flag (true or false); `
558+ + `received ${ describeComparand ( flag ) } ` ;
559+ }
560+
533561/**
534562 * The refusal for an EMPTY plain object, `{}`, in comparand position. There is
535563 * no key to name, and it is neither a comparison value nor a relation
@@ -542,19 +570,107 @@ function emptyObjectComparandRefusalReason(field: string): string {
542570 + `neither a comparison value nor a nested relation constraint` ;
543571}
544572
545- /** {@link plainObjectComparandRefusal } for one value: a whole comparand or one list member. */
546- function plainObjectValueRefusal ( field : string , value : unknown ) : string | undefined {
573+ /**
574+ * The refusal for a `{ $now, addDays }` date anchor in comparand position.
575+ * `NowAnchorSchema` declares it and the platform resolves it, but the shift is
576+ * made in the compared column's temporal form (a calendar day for a `date`
577+ * column, an instant for a `datetime` one), and this matcher has no field types
578+ * to read a form from: the same reason {@link resolveFieldReference} refuses a
579+ * `{ $field, addDays }` offset. Compared as an object, it read as the text
580+ * `[object Object]`.
581+ */
582+ function nowAnchorComparandRefusalReason ( field : string ) : string {
583+ return `filter comparand on field '${ field } ' is a { $now, addDays } date anchor, which the `
584+ + 'in-memory matcher does not resolve: the platform shifts the current instant in the '
585+ + "compared column's temporal form, and this matcher has no field types to read one from" ;
586+ }
587+
588+ /**
589+ * The refusal for a plain object with a `$` key that is neither a `{ $field }`
590+ * reference nor a `{ $now }` anchor, such as `{ $foo: 1 }`, in comparand
591+ * position. The spec's comparand door refuses it as a plain object no driver
592+ * can compare.
593+ */
594+ function operatorKeyedComparandRefusalReason ( field : string , key : string ) : string {
595+ return `filter comparand on field '${ field } ' is an object carrying the key '${ key } ', which is `
596+ + 'neither a comparison value nor a { $field } reference' ;
597+ }
598+
599+ /**
600+ * Is `value` a plain object of ANOTHER realm (an iframe, a `vm` context)? Its
601+ * prototype is that realm's `Object.prototype`, whose own prototype is `null`.
602+ * The spec's filter walk does not read it as filter structure, and it has no
603+ * `valueOf` worth comparing, so in comparand position it is refused under every
604+ * operator, the way the spec's comparand door refuses it.
605+ */
606+ function isForeignRealmPlainObject ( value : unknown ) : boolean {
607+ if ( ! value || typeof value !== 'object' || Array . isArray ( value ) || isPlainObject ( value ) ) return false ;
608+ const proto = Object . getPrototypeOf ( value ) as object | null ;
609+ return proto !== null && Object . getPrototypeOf ( proto ) === null ;
610+ }
611+
612+ function foreignRealmComparandRefusalReason ( field : string ) : string {
613+ return `filter comparand on field '${ field } ' is a plain object from another realm, which is `
614+ + 'neither a comparison value nor filter structure here' ;
615+ }
616+
617+ /** `a Moment instance`, or a fallback when the constructor cannot be read. */
618+ function instanceName ( value : object ) : string {
619+ try {
620+ const name = ( Object . getPrototypeOf ( value ) as { constructor ?: { name ?: unknown } } | null )
621+ ?. constructor ?. name ;
622+ if ( typeof name === 'string' && name !== '' ) return `a ${ name } instance` ;
623+ } catch {
624+ // fall through: a refusal sentence must never throw
625+ }
626+ return 'an object of an unrecognised type' ;
627+ }
628+
629+ /**
630+ * The refusal for a class instance (a moment or luxon date, a decimal object,
631+ * a `RegExp`, a `Map`) as the comparand of an equality, membership or text
632+ * operator. It is data, and the ordering operators compare it through its
633+ * `valueOf`; these operators would compare it by reference (`$eq` no row,
634+ * `$ne` / `$nin` every row) or as its text, and the spec's comparand door
635+ * refuses it at every one of them. One sentence for both dialects: it names no
636+ * operator.
637+ */
638+ function classInstanceComparandRefusalReason ( field : string , value : object ) : string {
639+ return `filter comparand on field '${ field } ' is ${ instanceName ( value ) } , which the in-memory `
640+ + 'matcher compares only by order (through its valueOf, under $gt / $gte / $lt / $lte / '
641+ + '$between); an equality, membership or text comparison would read it by reference or as '
642+ + `text, so a comparison value there must be ${ ACCEPTED_FILTER_COMPARAND_TYPES_SENTENCE } ` ;
643+ }
644+
645+ /**
646+ * {@link objectComparandRefusal } for one value: a whole comparand or one list
647+ * member. `byValue` is whether the operator compares by equality, membership or
648+ * text rather than by order.
649+ */
650+ function objectValueRefusal ( field : string , value : unknown , byValue : boolean ) : string | undefined {
547651 const key = nestedRelationComparandKey ( value ) ;
548652 if ( key !== undefined ) return nestedRelationRefusalReason ( field , key ) ;
549- if ( isPlainObject ( value ) && Object . keys ( value ) . length === 0 ) {
550- return emptyObjectComparandRefusalReason ( field ) ;
653+ if ( isPlainObject ( value ) ) {
654+ const keys = Object . keys ( value ) ;
655+ if ( keys . length === 0 ) return emptyObjectComparandRefusalReason ( field ) ;
656+ // A `{ $field }` reference is resolved, or refused by name, further on.
657+ if ( isFieldReferenceComparand ( value ) ) return undefined ;
658+ if ( '$now' in value ) return nowAnchorComparandRefusalReason ( field ) ;
659+ return operatorKeyedComparandRefusalReason ( field , keys . find ( ( k ) => k . startsWith ( '$' ) ) ?? keys [ 0 ] ) ;
660+ }
661+ if ( isForeignRealmPlainObject ( value ) ) return foreignRealmComparandRefusalReason ( field ) ;
662+ if (
663+ byValue && value !== null && typeof value === 'object' && ! Array . isArray ( value )
664+ && ! isAcceptedFilterComparand ( value )
665+ ) {
666+ return classInstanceComparandRefusalReason ( field , value ) ;
551667 }
552668 return undefined ;
553669}
554670
555671/**
556- * The refusal for a comparand that is a PLAIN object with no `$` key, empty or
557- * not, or `undefined` when the comparand is not one. ONE builder for both arms:
672+ * The refusal for an OBJECT comparand this matcher cannot compare, or
673+ * `undefined` when the comparand is not one. ONE builder for both arms:
558674 * {@link matchesComparisonNode} calls it for every AST operator that reads the
559675 * value slot, and {@link matchesDollarOperator} for every `$` operator of the
560676 * spec's `FILTER_OPERATORS` that reads a comparand.
@@ -568,22 +684,32 @@ function plainObjectValueRefusal(field: string, value: unknown): string | undefi
568684 * (`normalizeFilterComparandTypes`) refuses the same comparand in both
569685 * positions, so a `provider: 'value'` list refuses where the platform does.
570686 *
571- * A NON-EMPTY one is a nested relation condition and gets
572- * {@link nestedRelationRefusalReason}, which names its first key. An EMPTY one,
573- * `{}`, has no key to name and gets {@link emptyObjectComparandRefusalReason}.
574- * A `Date`, a class instance and a `{ $field }` reference are not plain objects
575- * with no `$` key, so they are still compared.
687+ * What is refused, under every operator that reads a comparand:
688+ *
689+ * - a plain object with no `$` key: a NON-EMPTY one is a nested relation
690+ * condition and gets {@link nestedRelationRefusalReason}, which names its
691+ * first key; an EMPTY one, `{}`, gets {@link emptyObjectComparandRefusalReason};
692+ * - a plain object with a `$` key that is not a `{ $field }` reference: the
693+ * `{ $now, addDays }` date anchor gets {@link nowAnchorComparandRefusalReason},
694+ * any other one {@link operatorKeyedComparandRefusalReason};
695+ * - a plain object of another realm ({@link isForeignRealmPlainObject}).
696+ *
697+ * And, only under an operator that compares by equality, membership or text
698+ * (`byValue`), a class instance ({@link classInstanceComparandRefusalReason}).
699+ * Under an ordering it is data compared through its `valueOf`, so it is not
700+ * refused there. A `Date` and a `{ $field }` reference are still compared.
576701 */
577- function plainObjectComparandRefusal (
702+ function objectComparandRefusal (
578703 field : string ,
579704 rawTarget : unknown ,
580705 listOperator : boolean ,
706+ byValue : boolean ,
581707) : string | undefined {
582- const whole = plainObjectValueRefusal ( field , rawTarget ) ;
708+ const whole = objectValueRefusal ( field , rawTarget , byValue ) ;
583709 if ( whole !== undefined ) return whole ;
584710 if ( listOperator && Array . isArray ( rawTarget ) ) {
585711 for ( const member of rawTarget ) {
586- const refusal = plainObjectValueRefusal ( field , member ) ;
712+ const refusal = objectValueRefusal ( field , member , byValue ) ;
587713 if ( refusal !== undefined ) return refusal ;
588714 }
589715 }
@@ -647,10 +773,14 @@ function matchesComparisonNode(
647773 // the object form of the same filter is refused by name. Checked for every
648774 // operator that reads the slot, so `!=` cannot answer it as every row, and
649775 // for each member of a list comparand, so `nin` cannot either. An EMPTY plain
650- // object, `{}`, is refused by the same builder in a sentence of its own.
776+ // object, `{}`, a `$`-keyed object that is no `{ $field }` reference (the
777+ // `{ $now, addDays }` anchor among them), a plain object of another realm and,
778+ // under an equality, membership or text operator, a class instance are
779+ // refused by the same builder, each in a sentence of its own.
651780 if ( ! AST_NO_COMPARAND_OPERATORS . has ( operator ) ) {
652- const plainRefusal = plainObjectComparandRefusal (
781+ const plainRefusal = objectComparandRefusal (
653782 field , rawTarget , AST_LIST_COMPARAND_OPERATORS . has ( operator ) ,
783+ AST_BY_VALUE_OPERATORS . has ( operator ) ,
654784 ) ;
655785 if ( plainRefusal !== undefined ) return refuseFilterNode ( refusals , plainRefusal ) ;
656786 }
@@ -931,20 +1061,20 @@ function matchesDollarOperator(
9311061 return refuseListMemberFieldReference ( refusals , field , operator ) ;
9321062 }
9331063
934- // A plain object with no `$` key as the comparand, NON-EMPTY or EMPTY (`{}`),
935- // whole or as a list member: the AST arm's refusals, from the same builder,
936- // so `$ne` / `$notContains` / `$nin` cannot answer it as every row. A
937- // non-empty one gets the nested-relation sentence, `{}` its own. Judged for
938- // every declared operator that reads a comparand. The three flag operators
939- // are not: `$empty` refuses a non-boolean flag on its own arm, and `$null` /
940- // `$exists` read their flag's truthiness, as their lowering does. Keys
941- // outside `FILTER_OPERATORS` keep the refusal the `default` arm gives them.
1064+ // An object comparand this matcher cannot compare, whole or as a list member:
1065+ // the AST arm's refusals, from the same builder, so `$ne` / `$notContains` /
1066+ // `$nin` cannot answer it as every row. See objectComparandRefusal for the
1067+ // population. Judged for every declared operator that reads a comparand. The
1068+ // three flag operators are not: each refuses a non-boolean flag on its own
1069+ // arm below. Keys outside `FILTER_OPERATORS` keep the refusal the `default`
1070+ // arm gives them.
9421071 if (
9431072 DOLLAR_COMPARAND_OPERATORS . has ( operator )
9441073 && ! DOLLAR_FLAG_OPERATORS . has ( operator )
9451074 ) {
946- const plainRefusal = plainObjectComparandRefusal (
1075+ const plainRefusal = objectComparandRefusal (
9471076 field , rawTarget , DOLLAR_LIST_COMPARAND_OPERATORS . has ( operator ) ,
1077+ DOLLAR_BY_VALUE_OPERATORS . has ( operator ) ,
9481078 ) ;
9491079 if ( plainRefusal !== undefined ) return refuseFilterNode ( refusals , plainRefusal ) ;
9501080 }
@@ -1009,15 +1139,20 @@ function matchesDollarOperator(
10091139 // -- Null-ness. Unlike the AST spelling, direction comes from the VALUE:
10101140 // `$null: true` is IS NULL and `$null: false` is IS NOT NULL, which is the
10111141 // lowering `convertFiltersToAST` already performs.
1142+ //
1143+ // The flag is a BOOLEAN by declaration (`SpecialOperatorSchema`), so
1144+ // anything else is refused here rather than read for its truthiness, as
1145+ // `$empty` below is and as the converter refuses it before lowering: a
1146+ // truthy `'no'` would have answered the IS NULL rows.
10121147 case '$null' :
1013- return target
1014- ? value === null || value === undefined
1015- : value !== null && value !== undefined ;
1148+ if ( target === true ) return value === null || value === undefined ;
1149+ if ( target === false ) return value !== null && value !== undefined ;
1150+ return refuseFilterNode ( refusals , nonBooleanFlagRefusalReason ( field , operator , target ) ) ;
10161151
10171152 // `$exists` is the exact INVERSE of `$null`, and that is the platform's own
10181153 // reading rather than one invented here: `convertFiltersToAST`
10191154 // (`../utils/filter-converter.ts`) lowers `$exists: true` to `is_not_null`
1020- // and `$exists: false` to `is_null`, three lines below where it lowers
1155+ // and `$exists: false` to `is_null`, in the same arm where it lowers
10211156 // `$null` the other way round. Both operators are absent from that file's
10221157 // `convertOperatorToAST` map and both are special-cased BEFORE it, so that
10231158 // map's silence is not a decision about either of them. Two producers in
@@ -1027,9 +1162,9 @@ function matchesDollarOperator(
10271162 // would have turned "every row" into "no rows" on a filter that looks like
10281163 // it works, which is the one outcome worse than the bug.
10291164 case '$exists' :
1030- return target
1031- ? value !== null && value !== undefined
1032- : value === null || value === undefined ;
1165+ if ( target === true ) return value !== null && value !== undefined ;
1166+ if ( target === false ) return value === null || value === undefined ;
1167+ return refuseFilterNode ( refusals , nonBooleanFlagRefusalReason ( field , operator , target ) ) ;
10331168
10341169 // objectui#11094 — `$empty`, which `@objectstack/spec` 17.6.0 admitted to
10351170 // `FILTER_OPERATORS` (objectstack#20446) and to which it lowers `is_empty` /
@@ -1044,11 +1179,7 @@ function matchesDollarOperator(
10441179 case '$empty' :
10451180 if ( target === true ) return isEmptyStoredValue ( value ) ;
10461181 if ( target === false ) return ! isEmptyStoredValue ( value ) ;
1047- return refuseFilterNode (
1048- refusals ,
1049- `filter operator '$empty' on field '${ field } ' takes a boolean flag (true or false); `
1050- + `received ${ describeComparand ( target ) } ` ,
1051- ) ;
1182+ return refuseFilterNode ( refusals , nonBooleanFlagRefusalReason ( field , operator , target ) ) ;
10521183
10531184 // objectui#8515 — the hand-authored IMPLICIT form `{ amount: { $field: 'x' } }`.
10541185 // It is not a reference comparand: an object whose only key starts with `$`
0 commit comments