@amritk/generate-parsers 0.18.0 → 0.19.1
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/AI.md +10 -0
- package/README.md +120 -8
- package/dist/generators/assert-supported-keywords.d.ts +13 -9
- package/dist/generators/assert-supported-keywords.js +58 -18
- package/dist/generators/build-schema.d.ts +9 -1
- package/dist/generators/build-schema.js +2 -2
- package/dist/generators/generate-deep-equal-check.d.ts +24 -0
- package/dist/generators/generate-deep-equal-check.js +35 -0
- package/dist/generators/generate-enum-check.d.ts +8 -3
- package/dist/generators/generate-enum-check.js +8 -6
- package/dist/generators/generate-parser-function.d.ts +1 -1
- package/dist/generators/generate-parser-function.js +201 -57
- package/dist/generators/generate-schema-checks.js +10 -8
- package/dist/generators/generate-strict-assertion.d.ts +82 -5
- package/dist/generators/generate-strict-assertion.js +381 -53
- package/dist/generators/generate-type-checks.d.ts +31 -10
- package/dist/generators/generate-type-checks.js +65 -16
- package/dist/generators/generate-unique-items-check.d.ts +16 -0
- package/dist/generators/generate-unique-items-check.js +32 -0
- package/dist/generators/generate-validation-expression.d.ts +16 -0
- package/dist/generators/generate-validation-expression.js +10 -4
- package/dist/generators/subschema-match.d.ts +23 -4
- package/dist/generators/subschema-match.js +312 -48
- package/dist/generators/unevaluated-match.d.ts +19 -0
- package/dist/generators/unevaluated-match.js +206 -0
- package/dist/helpers/get-default-value.js +22 -1
- package/package.json +6 -3
|
@@ -14,6 +14,74 @@ export type StrictAssertionContext = {
|
|
|
14
14
|
readonly rootSchema?: Record<string, unknown>;
|
|
15
15
|
readonly stripUnknown?: boolean;
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* Why a node needs the whole-schema backstop, or `null` when the specialized
|
|
19
|
+
* assertions above already cover it. Each reason is a keyword whose enforcement
|
|
20
|
+
* no per-property / per-type check can express:
|
|
21
|
+
*
|
|
22
|
+
* - `unevaluated*` depends on which *other* keywords evaluated which keys, so it
|
|
23
|
+
* can only be answered against the whole node at once.
|
|
24
|
+
* - a `$ref` is normally enforced by the parser generated for its target, but
|
|
25
|
+
* that delegation covers neither an inlined build (no ref imports) nor the
|
|
26
|
+
* 2020-12 rule that a `$ref`'s *siblings* still apply.
|
|
27
|
+
* - `items: false` (with no tuple positions) admits only the empty array, and
|
|
28
|
+
* the item-check builder reads `items` through a guard that a boolean fails.
|
|
29
|
+
* - a constraint keyword with no `type` never reaches the typed constraint pass.
|
|
30
|
+
*/
|
|
31
|
+
export declare const backstopReason: (schema: JSONSchema, context: StrictAssertionContext) => string | null;
|
|
32
|
+
/**
|
|
33
|
+
* The slice of a node the backstop actually has to prove.
|
|
34
|
+
*
|
|
35
|
+
* `unevaluated*` and `$ref` need the node whole — the first because its answer
|
|
36
|
+
* depends on what every sibling keyword evaluated, the second because a ref's
|
|
37
|
+
* siblings apply alongside its target. A bare constraint needs only itself, and
|
|
38
|
+
* proving it in isolation is what keeps an ordinary
|
|
39
|
+
* `{ allOf: [{…}, { $ref: … }], required: ['role'] }` — the OpenAPI idiom —
|
|
40
|
+
* generating: the `required` is provable even when the surrounding `allOf`
|
|
41
|
+
* member is too deep (or too unresolvable) to inline, and that member has its
|
|
42
|
+
* own enforcement anyway.
|
|
43
|
+
*
|
|
44
|
+
* `properties` rides along, flattened to `true` schemas, only because
|
|
45
|
+
* `additionalProperties` needs the declared key names to know which keys it
|
|
46
|
+
* owns. Their values are irrelevant to that question and are asserted
|
|
47
|
+
* per-property elsewhere.
|
|
48
|
+
*/
|
|
49
|
+
export declare const backstopSchema: (schema: JSONSchema, reason: string) => JSONSchema;
|
|
50
|
+
/**
|
|
51
|
+
* The strict-mode backstop: a single whole-node match, emitted only for the
|
|
52
|
+
* keywords {@link backstopReason} identifies as unreachable by the specialized
|
|
53
|
+
* assertions. It is the same exact matcher `contains` and `propertyNames` use, so
|
|
54
|
+
* a `true` verdict means valid and a `false` one means invalid — which is what
|
|
55
|
+
* lets it stand alone as the enforcement for those keywords.
|
|
56
|
+
*
|
|
57
|
+
* Emitting nothing here is only safe because `assertNoUnsupportedKeywords`
|
|
58
|
+
* refuses generation for any node that needs a backstop the matcher cannot
|
|
59
|
+
* build.
|
|
60
|
+
*/
|
|
61
|
+
export declare const generateBackstopAssertion: (acc: string, schema: JSONSchema, label: string, context: StrictAssertionContext, optional?: boolean) => string[];
|
|
62
|
+
/**
|
|
63
|
+
* Strict-mode `minProperties` / `maxProperties`. Both were read by the coercing
|
|
64
|
+
* expression builder and the subschema matcher but by nothing on the strict
|
|
65
|
+
* path, so a strict parser accepted `{}` against `{ minProperties: 2 }` — at the
|
|
66
|
+
* root, on a property, and inside the empty-object/record parsers alike. `guard`
|
|
67
|
+
* wraps the comparison in a runtime `isObject` for accessors whose type is
|
|
68
|
+
* reported separately; callers that have already proven an object pass `false`.
|
|
69
|
+
*/
|
|
70
|
+
export declare const generateObjectSizeChecks: (acc: string, schema: JSONSchema, label: string, guard: boolean) => string[];
|
|
71
|
+
/**
|
|
72
|
+
* Strict-mode value checks for keys the schema constrains by *pattern* rather
|
|
73
|
+
* than by name: `patternProperties` (every key matching the regex) and a
|
|
74
|
+
* schema-valued `additionalProperties` (every key matched by neither
|
|
75
|
+
* `properties` nor a pattern). Both were read only by the *building* code paths
|
|
76
|
+
* — which coerce or copy — so a strict parser accepted `{ s_a: 1 }` against
|
|
77
|
+
* `patternProperties: { '^s_': { type: 'string' } }`.
|
|
78
|
+
*
|
|
79
|
+
* One `Object.keys` walk covers both, and a term is emitted only when
|
|
80
|
+
* {@link subschemaMatchExpr} proves its subschema exactly. `additionalProperties:
|
|
81
|
+
* false` is not handled here — that is the unknown-key rejection the parsers
|
|
82
|
+
* already emit.
|
|
83
|
+
*/
|
|
84
|
+
export declare const generateKeyedValueChecks: (obj: string, schema: JSONSchema, label: string, context?: StrictAssertionContext) => string[];
|
|
17
85
|
/**
|
|
18
86
|
* Strict-mode `contains` / `minContains` / `maxContains`: at least `minContains`
|
|
19
87
|
* (default 1) and at most `maxContains` (default ∞) items of the array must
|
|
@@ -24,7 +92,15 @@ export type StrictAssertionContext = {
|
|
|
24
92
|
* `contains` or its subschema is not matchable inline (the generation-time guard
|
|
25
93
|
* rejects the latter, so a strict parser never silently drops the constraint).
|
|
26
94
|
*/
|
|
27
|
-
export declare const generateContainsCheck: (acc: string, schema: JSONSchema, label: string) => string[];
|
|
95
|
+
export declare const generateContainsCheck: (acc: string, schema: JSONSchema, label: string, context?: StrictAssertionContext) => string[];
|
|
96
|
+
/**
|
|
97
|
+
* The composition checks (`not`, `allOf`, `if`/`then`/`else`) for a value the
|
|
98
|
+
* caller has already proven present. Exported for the specialized object parsers
|
|
99
|
+
* (empty-object, pattern-properties, additionalProperties), which build their
|
|
100
|
+
* result outside {@link generateObjectStrictAssertion} and so would otherwise
|
|
101
|
+
* emit no composition check at all.
|
|
102
|
+
*/
|
|
103
|
+
export declare const generateCompositionChecks: (acc: string, schema: JSONSchema, label: string, context?: StrictAssertionContext) => string[];
|
|
28
104
|
/**
|
|
29
105
|
* Emits the object-level keyword checks (`dependentRequired`, `dependentSchemas`,
|
|
30
106
|
* `propertyNames`) for the object reached by `obj`. When `guard` is true the
|
|
@@ -34,7 +110,7 @@ export declare const generateContainsCheck: (acc: string, schema: JSONSchema, la
|
|
|
34
110
|
* `guard = false` because the enclosing parser has already proven the value is
|
|
35
111
|
* an object.
|
|
36
112
|
*/
|
|
37
|
-
export declare const generateObjectKeywordChecks: (obj: string, schema: JSONSchema, label: string, guard: boolean) => string[];
|
|
113
|
+
export declare const generateObjectKeywordChecks: (obj: string, schema: JSONSchema, label: string, guard: boolean, context?: StrictAssertionContext) => string[];
|
|
38
114
|
/**
|
|
39
115
|
* Generates strict-mode assertions for the body of an object parser.
|
|
40
116
|
* Throws on:
|
|
@@ -55,7 +131,8 @@ export declare const generateObjectKeywordChecks: (obj: string, schema: JSONSche
|
|
|
55
131
|
export declare const inlineAllOfMembers: (schema: JSONSchema) => JSONSchema[];
|
|
56
132
|
export declare const generateObjectStrictAssertion: (schema: JSONSchema, typeName: string, context?: StrictAssertionContext) => string[];
|
|
57
133
|
/**
|
|
58
|
-
* Generates
|
|
59
|
-
* Returns null when the schema
|
|
134
|
+
* Generates the strict-mode assertion lines for a non-object (scalar, array,
|
|
135
|
+
* type-less) parser. Returns null when the schema constrains nothing this
|
|
136
|
+
* generator can assert on, which is the caller's cue to emit a bare cast.
|
|
60
137
|
*/
|
|
61
|
-
export declare const generateScalarStrictAssertion: (schema: JSONSchema, typeName: string,
|
|
138
|
+
export declare const generateScalarStrictAssertion: (schema: JSONSchema, typeName: string, context?: StrictAssertionContext) => string | null;
|