@amritk/helpers 0.17.0 → 0.18.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.
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Emits code for the JSON Schema numeric *bound* keywords — `minimum`,
3
+ * `maximum`, `exclusiveMinimum`, `exclusiveMaximum` — that agrees with the
4
+ * runtime interpreter (`packages/runtime-validators/src/interpreter/interpret.ts`).
5
+ *
6
+ * There is one rule here and it is easy to get wrong: **a bound is written as a
7
+ * pass condition and negated, never as a failure condition directly.** For every
8
+ * ordinary value the two spellings are identical:
9
+ *
10
+ * ```
11
+ * !(x >= min) // pass condition, negated
12
+ * x < min // failure condition, written directly
13
+ * ```
14
+ *
15
+ * For `NaN` they are opposite. `NaN` compares `false` against every relational
16
+ * operator, so `!(NaN >= 5)` is `true` — the value fails the bound, which is
17
+ * Ajv's verdict and the interpreter's — while `NaN < 5` is `false`, which
18
+ * silently *accepts* it. A generated validator spelled the second way was more
19
+ * permissive than the interpreter for the same schema, and nothing caught it:
20
+ * a conformance corpus is JSON, and JSON cannot express `NaN`.
21
+ *
22
+ * The keyword-level semantics this encodes, all matching Ajv:
23
+ *
24
+ * - `NaN` fails any bound it is checked against.
25
+ * - `±Infinity` follows the ordinary comparison, so `Infinity` passes
26
+ * `minimum: 0` and fails `maximum: 10`.
27
+ * - A bare `type: 'number'` with no bound still accepts non-finite numbers.
28
+ * Only a bound (or `multipleOf`) rejects them.
29
+ * - Draft-04's boolean `exclusiveMinimum: true` / `exclusiveMaximum: true`
30
+ * makes a sibling `minimum` / `maximum` strict; draft-06+ replaced it with
31
+ * the standalone numeric keywords. Both forms are spelled through `strict`.
32
+ *
33
+ * `valueExpr` is inlined once per expression, so it must be a side-effect-free
34
+ * expression (a property read or a cached variable — which is all the generators
35
+ * ever pass). Every expression is self-parenthesized, so a caller can drop one
36
+ * into a `&&` / `||` chain or negate it without re-bracketing.
37
+ *
38
+ * Companion to `multiple-of-check`, which does the same for `multipleOf`.
39
+ */
40
+ /** Which bound a check is for, in the direction it compares. */
41
+ export type BoundKind = 'minimum' | 'maximum';
42
+ /**
43
+ * Boolean expression that is TRUE when `valueExpr` satisfies the bound.
44
+ *
45
+ * @param strict `>` / `<` rather than `>=` / `<=` — draft-06+'s
46
+ * `exclusiveMinimum` / `exclusiveMaximum`, or draft-04's boolean form.
47
+ */
48
+ export declare const boundPassExpr: (valueExpr: string, kind: BoundKind, bound: number, strict?: boolean) => string;
49
+ /**
50
+ * Boolean expression that is TRUE when `valueExpr` VIOLATES the bound (the error
51
+ * condition) — the negated pass condition, which is the whole point of this
52
+ * module. See the file comment for why the direct spelling is wrong.
53
+ */
54
+ export declare const boundFailExpr: (valueExpr: string, kind: BoundKind, bound: number, strict?: boolean) => string;
55
+ /**
56
+ * The relational operator a bound's *message* names (`must be >= 5`), kept here
57
+ * so the message and the check it describes cannot drift apart.
58
+ */
59
+ export declare const boundOperator: (kind: BoundKind, strict?: boolean) => string;
@@ -0,0 +1,11 @@
1
+ const boundPassExpr = (valueExpr, kind, bound, strict = false) => {
2
+ const operator = kind === "minimum" ? strict ? ">" : ">=" : strict ? "<" : "<=";
3
+ return `(${valueExpr} ${operator} ${bound})`;
4
+ };
5
+ const boundFailExpr = (valueExpr, kind, bound, strict = false) => `!${boundPassExpr(valueExpr, kind, bound, strict)}`;
6
+ const boundOperator = (kind, strict = false) => kind === "minimum" ? strict ? ">" : ">=" : strict ? "<" : "<=";
7
+ export {
8
+ boundFailExpr,
9
+ boundOperator,
10
+ boundPassExpr
11
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,