@inixiative/json-rules 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -386,6 +386,13 @@ type ModelDefaultNarrowing = {
386
386
  * Composes via filter-first semantic at every visit of this model.
387
387
  */
388
388
  where?: Condition;
389
+ /**
390
+ * Per-field eligibility `where` over THIS model — decorates a field's option
391
+ * picker. The field's selectable values = DISTINCT(field) over this model
392
+ * filtered by `where` (plus the model's own narrowing). Composes like `where`:
393
+ * general via `mapDefaults`, path-specific via `root`/`relations`, AND-only.
394
+ */
395
+ sources?: Record<string, Condition>;
389
396
  };
390
397
  /** Narrowing for a model at a specific traversal path. Adds relations to the default shape. */
391
398
  type ModelNarrowing = ModelDefaultNarrowing & {
@@ -524,47 +531,13 @@ declare const getOperatorsForKind: (kind: FieldKind, target?: RuleTarget) => {
524
531
  declare const getArrayOperators: (target?: RuleTarget) => ArrayOperator[];
525
532
 
526
533
  type RuleDescription = {
527
- /** Map (source) names the rule's fields touch, sorted. */
528
534
  sources: string[];
529
- /** True if any field/path crosses a bridge into another source. */
530
535
  bridgesCrossed: boolean;
531
- /** Execution targets that can run this rule, in canonical order. */
532
536
  supportedTargets: RuleTarget[];
533
- /** Field paths that don't resolve through the lens. */
534
537
  violations: string[];
535
538
  };
536
- /**
537
- * Classifies a rule against a lens: which sources it touches, whether it crosses
538
- * a bridge, and which execution targets can run it. A bridge-crossing rule is
539
- * `check()`-only — `toPrisma`/`toSql` can't join across sources, so the host must
540
- * hydrate foreign rows (see `buildBridgeDictionary`) and evaluate in memory.
541
- * Windowing further restricts targets (`toSql` never; `toPrisma` only the
542
- * extremal array rewrite). `violations` lists field paths that don't resolve
543
- * through the lens — use `checkRuleAgainstLens` for the full security gate.
544
- */
545
539
  declare const describeRule: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleDescription;
546
540
 
547
- /**
548
- * Produces the total exposed surface of a (possibly narrowed) lens as a **Lens**
549
- * (maps intact — the navigable graph), NOT a projection (path-keyed view). This
550
- * is the leak-safe server→client surface: every model reachable from the anchor
551
- * through visible relation/bridge edges, with the FULL narrowing applied — root
552
- * at the anchor, path-specific narrowing along declared relation paths,
553
- * model-default (`mapDefaults`) everywhere else — then unioned per model. A field
554
- * appears iff it is visible on at least one reachable, narrowed path; fields
555
- * hidden on every path (including those hidden only by `root`) are absent, so it
556
- * never exposes the raw, un-narrowed lens.
557
- *
558
- * `where` (data-scope) narrowing is dropped — this is the client schema surface.
559
- * For a server→subtenant handoff that must preserve `where` and per-path
560
- * narrowing, use `seal` (planned) instead. Per-path divergence (a model that
561
- * looks different at two sibling paths) is not represented here; pair with
562
- * `projectByPath` when that distinction matters.
563
- *
564
- * Cycle-safe: declared-path visits are keyed by path (the declared tree is
565
- * finite) and off-path visits by model (visited once), so recursive schemas
566
- * (User → Org → members(User) → …) terminate.
567
- */
568
541
  declare const exposedSurface: (lensOrNarrowing: Lens | LensNarrowing) => Lens;
569
542
 
570
543
  declare const validateNarrowing: (narrowing: LensNarrowing) => void;
@@ -574,10 +547,58 @@ type ProjectedVisit = {
574
547
  modelName: string;
575
548
  fields: Record<string, FieldMapEntry>;
576
549
  whereClauses: Condition[];
550
+ /** Per-field source eligibility wheres, composed across layers (general + path). */
551
+ sources: Record<string, Condition[]>;
577
552
  };
578
553
  type PathProjection = Map<string, ProjectedVisit>;
579
554
  declare const projectByPath: (lensOrNarrowing: Lens | LensNarrowing) => PathProjection;
580
555
 
556
+ type SourcePrismaQuery = {
557
+ model: string;
558
+ distinct: string[];
559
+ select: Record<string, true>;
560
+ where: PrismaWhere;
561
+ /** Present only if the composed where used count operators (run via executePrismaQueryPlan). */
562
+ steps?: PrismaStep[];
563
+ };
564
+ /** `sql` is null when the composed where uses a predicate SQL can't express
565
+ * (e.g. array-condition operators); `error` then carries why. Prisma still
566
+ * compiles — run that, or fall back to fetch + `check()`. */
567
+ type SourceSqlQuery = {
568
+ sql: string | null;
569
+ params: unknown[];
570
+ error?: string;
571
+ };
572
+ type SourceQuery = {
573
+ path: string;
574
+ mapName: string;
575
+ model: string;
576
+ field: string;
577
+ composedWhere: Condition;
578
+ prisma: SourcePrismaQuery;
579
+ sql: SourceSqlQuery;
580
+ };
581
+ /**
582
+ * Compile a DISTINCT(value) query — Prisma and SQL — per sourced field across
583
+ * the projected lens. The WHERE is the field's composed eligibility: the model's
584
+ * own narrowing at that path AND its source where(s). The app runs these (with
585
+ * its own client) to materialize each field's option set.
586
+ */
587
+ declare const sourceQueries: (lensOrNarrowing: Lens | LensNarrowing) => SourceQuery[];
588
+ type SourceValues = {
589
+ path: string;
590
+ field: string;
591
+ values: readonly string[];
592
+ };
593
+ /**
594
+ * Decorate fetched DISTINCT values back onto the projected lens. Each result's
595
+ * values land on `projection.get(path).fields[field].values` — the existing
596
+ * pseudo-enum primitive the builder reads. Per-path, so the same model at two
597
+ * paths can carry different option sets. Pure: returns a new projection;
598
+ * results for unknown paths or fields are ignored.
599
+ */
600
+ declare const applySourceValues: (projection: PathProjection, results: readonly SourceValues[]) => PathProjection;
601
+
581
602
  /**
582
603
  * Execute a Prisma query plan produced by toPrisma().
583
604
  *
@@ -663,4 +684,4 @@ declare const assertValidRule: (condition: unknown, options?: {
663
684
  target?: RuleTarget;
664
685
  }) => asserts condition is Condition;
665
686
 
666
- export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, type EdgeExpr, type EnumNarrowing, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type ModelDefaultNarrowing, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, ORDERABLE_KINDS, Operator, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaStep, type PrismaWhere, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Rule, type RuleDescription, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SortDir, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, WINDOW_SELECTOR, type WeekStart, type WhereStep, type WindowFields, type WindowRuleType, WindowSupport, applyLens, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, describeRule, executePrismaQueryPlan, exposedSurface, getArrayOperators, getOperatorsForKind, getValueShape, getWindowSupport, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectByPath, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };
687
+ export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, type EdgeExpr, type EnumNarrowing, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type ModelDefaultNarrowing, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, ORDERABLE_KINDS, Operator, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaStep, type PrismaWhere, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Rule, type RuleDescription, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SortDir, type SourcePrismaQuery, type SourceQuery, type SourceSqlQuery, type SourceValues, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, WINDOW_SELECTOR, type WeekStart, type WhereStep, type WindowFields, type WindowRuleType, WindowSupport, applyLens, applySourceValues, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, describeRule, executePrismaQueryPlan, exposedSurface, getArrayOperators, getOperatorsForKind, getValueShape, getWindowSupport, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectByPath, sourceQueries, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };
package/dist/index.d.ts CHANGED
@@ -386,6 +386,13 @@ type ModelDefaultNarrowing = {
386
386
  * Composes via filter-first semantic at every visit of this model.
387
387
  */
388
388
  where?: Condition;
389
+ /**
390
+ * Per-field eligibility `where` over THIS model — decorates a field's option
391
+ * picker. The field's selectable values = DISTINCT(field) over this model
392
+ * filtered by `where` (plus the model's own narrowing). Composes like `where`:
393
+ * general via `mapDefaults`, path-specific via `root`/`relations`, AND-only.
394
+ */
395
+ sources?: Record<string, Condition>;
389
396
  };
390
397
  /** Narrowing for a model at a specific traversal path. Adds relations to the default shape. */
391
398
  type ModelNarrowing = ModelDefaultNarrowing & {
@@ -524,47 +531,13 @@ declare const getOperatorsForKind: (kind: FieldKind, target?: RuleTarget) => {
524
531
  declare const getArrayOperators: (target?: RuleTarget) => ArrayOperator[];
525
532
 
526
533
  type RuleDescription = {
527
- /** Map (source) names the rule's fields touch, sorted. */
528
534
  sources: string[];
529
- /** True if any field/path crosses a bridge into another source. */
530
535
  bridgesCrossed: boolean;
531
- /** Execution targets that can run this rule, in canonical order. */
532
536
  supportedTargets: RuleTarget[];
533
- /** Field paths that don't resolve through the lens. */
534
537
  violations: string[];
535
538
  };
536
- /**
537
- * Classifies a rule against a lens: which sources it touches, whether it crosses
538
- * a bridge, and which execution targets can run it. A bridge-crossing rule is
539
- * `check()`-only — `toPrisma`/`toSql` can't join across sources, so the host must
540
- * hydrate foreign rows (see `buildBridgeDictionary`) and evaluate in memory.
541
- * Windowing further restricts targets (`toSql` never; `toPrisma` only the
542
- * extremal array rewrite). `violations` lists field paths that don't resolve
543
- * through the lens — use `checkRuleAgainstLens` for the full security gate.
544
- */
545
539
  declare const describeRule: (rule: Condition, lensOrNarrowing: Lens | LensNarrowing) => RuleDescription;
546
540
 
547
- /**
548
- * Produces the total exposed surface of a (possibly narrowed) lens as a **Lens**
549
- * (maps intact — the navigable graph), NOT a projection (path-keyed view). This
550
- * is the leak-safe server→client surface: every model reachable from the anchor
551
- * through visible relation/bridge edges, with the FULL narrowing applied — root
552
- * at the anchor, path-specific narrowing along declared relation paths,
553
- * model-default (`mapDefaults`) everywhere else — then unioned per model. A field
554
- * appears iff it is visible on at least one reachable, narrowed path; fields
555
- * hidden on every path (including those hidden only by `root`) are absent, so it
556
- * never exposes the raw, un-narrowed lens.
557
- *
558
- * `where` (data-scope) narrowing is dropped — this is the client schema surface.
559
- * For a server→subtenant handoff that must preserve `where` and per-path
560
- * narrowing, use `seal` (planned) instead. Per-path divergence (a model that
561
- * looks different at two sibling paths) is not represented here; pair with
562
- * `projectByPath` when that distinction matters.
563
- *
564
- * Cycle-safe: declared-path visits are keyed by path (the declared tree is
565
- * finite) and off-path visits by model (visited once), so recursive schemas
566
- * (User → Org → members(User) → …) terminate.
567
- */
568
541
  declare const exposedSurface: (lensOrNarrowing: Lens | LensNarrowing) => Lens;
569
542
 
570
543
  declare const validateNarrowing: (narrowing: LensNarrowing) => void;
@@ -574,10 +547,58 @@ type ProjectedVisit = {
574
547
  modelName: string;
575
548
  fields: Record<string, FieldMapEntry>;
576
549
  whereClauses: Condition[];
550
+ /** Per-field source eligibility wheres, composed across layers (general + path). */
551
+ sources: Record<string, Condition[]>;
577
552
  };
578
553
  type PathProjection = Map<string, ProjectedVisit>;
579
554
  declare const projectByPath: (lensOrNarrowing: Lens | LensNarrowing) => PathProjection;
580
555
 
556
+ type SourcePrismaQuery = {
557
+ model: string;
558
+ distinct: string[];
559
+ select: Record<string, true>;
560
+ where: PrismaWhere;
561
+ /** Present only if the composed where used count operators (run via executePrismaQueryPlan). */
562
+ steps?: PrismaStep[];
563
+ };
564
+ /** `sql` is null when the composed where uses a predicate SQL can't express
565
+ * (e.g. array-condition operators); `error` then carries why. Prisma still
566
+ * compiles — run that, or fall back to fetch + `check()`. */
567
+ type SourceSqlQuery = {
568
+ sql: string | null;
569
+ params: unknown[];
570
+ error?: string;
571
+ };
572
+ type SourceQuery = {
573
+ path: string;
574
+ mapName: string;
575
+ model: string;
576
+ field: string;
577
+ composedWhere: Condition;
578
+ prisma: SourcePrismaQuery;
579
+ sql: SourceSqlQuery;
580
+ };
581
+ /**
582
+ * Compile a DISTINCT(value) query — Prisma and SQL — per sourced field across
583
+ * the projected lens. The WHERE is the field's composed eligibility: the model's
584
+ * own narrowing at that path AND its source where(s). The app runs these (with
585
+ * its own client) to materialize each field's option set.
586
+ */
587
+ declare const sourceQueries: (lensOrNarrowing: Lens | LensNarrowing) => SourceQuery[];
588
+ type SourceValues = {
589
+ path: string;
590
+ field: string;
591
+ values: readonly string[];
592
+ };
593
+ /**
594
+ * Decorate fetched DISTINCT values back onto the projected lens. Each result's
595
+ * values land on `projection.get(path).fields[field].values` — the existing
596
+ * pseudo-enum primitive the builder reads. Per-path, so the same model at two
597
+ * paths can carry different option sets. Pure: returns a new projection;
598
+ * results for unknown paths or fields are ignored.
599
+ */
600
+ declare const applySourceValues: (projection: PathProjection, results: readonly SourceValues[]) => PathProjection;
601
+
581
602
  /**
582
603
  * Execute a Prisma query plan produced by toPrisma().
583
604
  *
@@ -663,4 +684,4 @@ declare const assertValidRule: (condition: unknown, options?: {
663
684
  target?: RuleTarget;
664
685
  }) => asserts condition is Condition;
665
686
 
666
- export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, type EdgeExpr, type EnumNarrowing, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type ModelDefaultNarrowing, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, ORDERABLE_KINDS, Operator, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaStep, type PrismaWhere, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Rule, type RuleDescription, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SortDir, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, WINDOW_SELECTOR, type WeekStart, type WhereStep, type WindowFields, type WindowRuleType, WindowSupport, applyLens, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, describeRule, executePrismaQueryPlan, exposedSurface, getArrayOperators, getOperatorsForKind, getValueShape, getWindowSupport, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectByPath, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };
687
+ export { AGGREGATE_OPERATORS, ALL_KINDS, ARRAY_OPERATOR_CATALOG, type AggregateMode, type AggregateRule, type All, type Any, type ArrayCatalogEntry, ArrayOperator, type ArrayRule, type Bridge, type BridgeCardinality, type BridgeDictionary, type BridgeEndpoint, type BuildOptions, type CatalogEntry, type CheckOptions, type Condition, type CreateLensInput, DATE_OPERATOR_CATALOG, type DateConfig, type DateExpr, type DateInputOrExpr, type DateInputValue, DateOperator, type DateRule, type DateRuleValue, EQUATABLE_KINDS, type EdgeExpr, type EnumNarrowing, FIELD_OPERATOR_CATALOG, FieldKind, type FieldMap, type FieldMapEntry, type FieldMapSet, type GroupByStep, type IfThenElse, type Lens, type LensNarrowing, type ModelDefaultNarrowing, type ModelNarrowing, NUMERIC_KINDS, type NarrowingDefaults, ORDERABLE_KINDS, Operator, type OrderBy, type OrderedRuleValue, type PathProjection, type PeriodExpr, type PeriodUnit, type PrismaStep, type PrismaWhere, type ProjectedVisit, type RelativeUnits, type RollingExpr, type Rule, type RuleDescription, type RuleLensCheck, type RuleLensViolation, type RuleScalar, RuleTarget, type RuleValue, STRINGY_KINDS, type SortDir, type SourcePrismaQuery, type SourceQuery, type SourceSqlQuery, type SourceValues, type SqlResult, type StepRef, type StrictAggregateRule, type StrictAll, type StrictAny, type StrictArrayCountRule, type StrictArrayPredicateRule, type StrictArrayPresenceRule, type StrictArrayRule, type StrictCondition, type StrictContainsRule, type StrictDateComparisonRule, type StrictDateDayRule, type StrictDateRangeRule, type StrictDateRule, type StrictEqualityRule, type StrictIfThenElse, type StrictMembershipRule, type StrictOrderedComparisonRule, type StrictPatternRule, type StrictPresenceRule, type StrictRangeRule, type StrictRule, type StrictStringBoundaryRule, type ToPrismaResult, type ValidationIssue, type ValidationResult, ValueShape, WINDOW_SELECTOR, type WeekStart, type WhereStep, type WindowFields, type WindowRuleType, WindowSupport, applyLens, applySourceValues, assertValidRule, buildBridgeDictionary, check, checkRuleAgainstLens, createLens, describeRule, executePrismaQueryPlan, exposedSurface, getArrayOperators, getOperatorsForKind, getValueShape, getWindowSupport, isAggregateRangeOperator, isAggregateSingleOperator, isOperatorSupportedForTarget, projectByPath, sourceQueries, stitchFieldMaps, toPrisma, toSql, validateFieldMap, validateFieldMapSet, validateNarrowing, validateRule };