@inixiative/json-rules 4.0.0 → 4.2.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/README.md CHANGED
@@ -302,6 +302,37 @@ What a consumer checks before saving one:
302
302
 
303
303
  Value expressions evaluate in memory only; `toPrisma` / `toSql` do not compile them.
304
304
 
305
+ ### Categorize
306
+
307
+ `categorize(categorization, row, options)` puts a row in a category: cases checked in order, each
308
+ an `if` (a rule) and a `then` (a value expression), with the options `check()` takes.
309
+
310
+ ```ts
311
+ categorize(
312
+ {
313
+ cases: [
314
+ { if: { field: 'deals', operator: 'greaterThanEquals', value: 10 }, then: 'gold' },
315
+ { if: { field: 'deals', operator: 'greaterThanEquals', value: 3 }, then: 'silver' },
316
+ ],
317
+ default: 'bronze',
318
+ },
319
+ { deals: 4 },
320
+ ); // 'silver'
321
+ ```
322
+
323
+ | Key | Meaning |
324
+ | --- | --- |
325
+ | `cases` | `[{ if, then }]`, in order |
326
+ | `match` | `'first'` (the default): the first case that holds gives the value, and later cases are not checked. `'all'`: every case that holds, in case order, as a list |
327
+ | `default` | the value when no case holds (under `all`, a list of it); with none, `null` (`[]` under `all`) |
328
+
329
+ A `then` can be any value expression: a field/value map is `{ value: { status: 'canceled' } }`.
330
+ A case that throws stops the categorization; it never falls through to a later case or the
331
+ default, which would give a value its author ordered below it. `validateCategorization` /
332
+ `assertValidCategorization` check the shape without data: known keys, a known `match`, each `if`
333
+ as `validateRule` sees it and each `then` and the `default` as `validateValue` does. In memory
334
+ only, like value expressions.
335
+
305
336
  ### Date Rule
306
337
 
307
338
  ```ts
@@ -610,7 +641,7 @@ An enum compares exactly against its declared values. String, pattern and ordere
610
641
  don't apply to one, and the compilers refuse them. A case-insensitive equality or membership, or
611
642
  one naming a value the enum doesn't declare, compiles to a membership test over the declared
612
643
  values `check()` would match (plus the NULL arm for a negation). The field map must list the
613
- values (prisma-map does).
644
+ values (field-map's Prisma reader does).
614
645
 
615
646
  ```ts
616
647
  // map: { models: { U: { fields: { role: { kind: 'enum', type: 'Role' } } } }, enums: { Role: ['admin', 'member'] } }
@@ -1052,7 +1083,7 @@ and `in [null, …]` carry the same hop arms: a user with no profile has a NULL
1052
1083
 
1053
1084
  `toPrisma()` can only add the null arm when it knows the column is nullable, because
1054
1085
  an `equals: null` on a NOT NULL column is a Prisma validation error. Nullability
1055
- comes from the field map: `FieldMapEntry.isRequired: false` (prisma-map emits it).
1086
+ comes from the field map: `FieldMapEntry.isRequired: false` (field-map's Prisma reader emits it).
1056
1087
  Without `{ map, model }`, or on an entry that doesn't declare it, the bare
1057
1088
  `not` / `notIn` is emitted and NULL rows fall out.
1058
1089
 
@@ -1163,7 +1194,8 @@ Rules:
1163
1194
  Lens & bridges:
1164
1195
 
1165
1196
  - `Lens`, `LensNarrowing`, `ModelNarrowing`, `ModelDefaultNarrowing`, `NarrowingDefaults`, `EnumNarrowing`, `SourceSpec`, `SourceEntry`
1166
- - `FieldMap`, `FieldMapEntry`, `ModelEntry`, `SourceOption`, `FieldMapSet`, `Bridge`, `BridgeEndpoint`, `BridgeCardinality`, `BridgeDictionary`
1197
+ - `LensFieldMap`, `LensModelEntry`, `FieldMapEntry`, `BridgeEntry`, `SourceOption`, `FieldMapSet`, `Bridge`, `BridgeEndpoint`, `BridgeCardinality`, `BridgeDictionary` (the schema types themselves, `FieldMap` / `ModelEntry` / `FieldEntry`, are [`@inixiative/field-map`](https://github.com/inixiative/field-map)'s)
1198
+ - Typed spellings over a literal map (`typeof prismaMap`, as `field-map generate` writes it): `ConditionOn`, `ModelNarrowingOn`, `LensNarrowingOn`, `UncheckedPath`
1167
1199
  - `createLens`, `storeLens`, `composeLens`, `StoredLens`, `stitchFieldMaps`, `indexBridges`, `validateFieldMaps`, `assertValidFieldMaps`
1168
1200
  - `validateNarrowing`, `assertValidNarrowing`, `validateRuleInLens`, `narrowRule`, `coerceRule`
1169
1201
  - `bindLens`, `listLensBindings`, `getLensRoot`, `clampLens`, `LensClamps`
@@ -1370,7 +1402,7 @@ The `Lens` primitive is a schema-aware view layer over one or more `FieldMap`s.
1370
1402
 
1371
1403
  ### FieldMap & FieldMapSet
1372
1404
 
1373
- A `FieldMap` is `{ models, enums? }`: models keyed by name, plus an optional enum registry scoped to that source. A `FieldMapSet` groups one or more `FieldMap`s and declares the cross-source edges between them:
1405
+ A `FieldMap` is `{ models, enums? }`: models keyed by name, plus an optional enum registry scoped to that source. The type, the Prisma reader (`field-map generate`), the JSON Schema reader and the own-property readers are [`@inixiative/field-map`](https://github.com/inixiative/field-map)'s; json-rules reads a map as a `LensFieldMap` (a `FieldMap`, with its bridges stitched in and a projection's options). A `FieldMapSet` groups one or more `FieldMap`s and declares the cross-source edges between them:
1374
1406
 
1375
1407
  ```ts
1376
1408
  import { stitchFieldMaps } from '@inixiative/json-rules';