@amritk/validation 0.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/AI.md +92 -0
- package/LICENSE +21 -0
- package/README.md +184 -0
- package/dist/generate.d.ts +135 -0
- package/dist/generate.js +74 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/parsers/generators/assert-supported-keywords.d.ts +30 -0
- package/dist/parsers/generators/assert-supported-keywords.js +87 -0
- package/dist/parsers/generators/build-schema.d.ts +111 -0
- package/dist/parsers/generators/build-schema.js +74 -0
- package/dist/parsers/generators/generate-deep-equal-check.d.ts +24 -0
- package/dist/parsers/generators/generate-deep-equal-check.js +35 -0
- package/dist/parsers/generators/generate-discriminated-union-validation.d.ts +15 -0
- package/dist/parsers/generators/generate-discriminated-union-validation.js +28 -0
- package/dist/parsers/generators/generate-enum-check.d.ts +35 -0
- package/dist/parsers/generators/generate-enum-check.js +31 -0
- package/dist/parsers/generators/generate-files.d.ts +138 -0
- package/dist/parsers/generators/generate-files.js +89 -0
- package/dist/parsers/generators/generate-non-discriminated-union-validation.d.ts +6 -0
- package/dist/parsers/generators/generate-non-discriminated-union-validation.js +26 -0
- package/dist/parsers/generators/generate-parser-function.d.ts +120 -0
- package/dist/parsers/generators/generate-parser-function.js +1973 -0
- package/dist/parsers/generators/generate-schema-checks.d.ts +5 -0
- package/dist/parsers/generators/generate-schema-checks.js +260 -0
- package/dist/parsers/generators/generate-strict-assertion.d.ts +155 -0
- package/dist/parsers/generators/generate-strict-assertion.js +759 -0
- package/dist/parsers/generators/generate-type-checks.d.ts +148 -0
- package/dist/parsers/generators/generate-type-checks.js +454 -0
- package/dist/parsers/generators/generate-unique-items-check.d.ts +16 -0
- package/dist/parsers/generators/generate-unique-items-check.js +32 -0
- package/dist/parsers/generators/generate-validation-expression.d.ts +79 -0
- package/dist/parsers/generators/generate-validation-expression.js +416 -0
- package/dist/parsers/generators/subschema-match.d.ts +48 -0
- package/dist/parsers/generators/subschema-match.js +485 -0
- package/dist/parsers/generators/unevaluated-match.d.ts +19 -0
- package/dist/parsers/generators/unevaluated-match.js +206 -0
- package/dist/parsers/helpers/apply-schema-extensions.d.ts +35 -0
- package/dist/parsers/helpers/apply-schema-extensions.js +20 -0
- package/dist/parsers/helpers/collect-helpers.d.ts +15 -0
- package/dist/parsers/helpers/collect-helpers.js +64 -0
- package/dist/parsers/helpers/collect-imports.d.ts +104 -0
- package/dist/parsers/helpers/collect-imports.js +227 -0
- package/dist/parsers/helpers/find-discriminator.d.ts +6 -0
- package/dist/parsers/helpers/find-discriminator.js +27 -0
- package/dist/parsers/helpers/get-discriminator-value.d.ts +6 -0
- package/dist/parsers/helpers/get-discriminator-value.js +18 -0
- package/dist/parsers/index.d.ts +2 -0
- package/dist/parsers/index.js +4 -0
- package/dist/parsers/types/schema-extensions.d.ts +34 -0
- package/dist/parsers/types/schema-extensions.js +0 -0
- package/dist/rehome-parser-file.d.ts +25 -0
- package/dist/rehome-parser-file.js +74 -0
- package/dist/validators/generators/assert-generatable-refs.d.ts +19 -0
- package/dist/validators/generators/assert-generatable-refs.js +12 -0
- package/dist/validators/generators/assert-unevaluated-generatable.d.ts +30 -0
- package/dist/validators/generators/assert-unevaluated-generatable.js +71 -0
- package/dist/validators/generators/build-schema.d.ts +61 -0
- package/dist/validators/generators/build-schema.js +666 -0
- package/dist/validators/generators/collect-emitted-refs.d.ts +26 -0
- package/dist/validators/generators/collect-emitted-refs.js +112 -0
- package/dist/validators/generators/collect-validator-imports.d.ts +86 -0
- package/dist/validators/generators/collect-validator-imports.js +62 -0
- package/dist/validators/generators/emit-format-checks.d.ts +50 -0
- package/dist/validators/generators/emit-format-checks.js +248 -0
- package/dist/validators/generators/enforced-keywords.d.ts +33 -0
- package/dist/validators/generators/enforced-keywords.js +70 -0
- package/dist/validators/generators/folds-to-constant.d.ts +15 -0
- package/dist/validators/generators/folds-to-constant.js +43 -0
- package/dist/validators/generators/generate-coerce-function.d.ts +22 -0
- package/dist/validators/generators/generate-coerce-function.js +208 -0
- package/dist/validators/generators/generate-files.d.ts +92 -0
- package/dist/validators/generators/generate-files.js +86 -0
- package/dist/validators/generators/generate-repair-function.d.ts +27 -0
- package/dist/validators/generators/generate-repair-function.js +102 -0
- package/dist/validators/generators/generate-validator-function.d.ts +58 -0
- package/dist/validators/generators/generate-validator-function.js +1691 -0
- package/dist/validators/generators/tuple-shape.d.ts +31 -0
- package/dist/validators/generators/tuple-shape.js +20 -0
- package/dist/validators/generators/unevaluated-match.d.ts +29 -0
- package/dist/validators/generators/unevaluated-match.js +223 -0
- package/dist/validators/index.d.ts +2 -0
- package/dist/validators/index.js +4 -0
- package/package.json +92 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { type UnknownKeysStrategy } from '@amritk/helpers/unknown-keys-strategy';
|
|
2
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
3
|
+
/**
|
|
4
|
+
* Options for controlling parser function generation behavior.
|
|
5
|
+
*/
|
|
6
|
+
type GenerateParserOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* When true, properties with $ref will call the imported parser function
|
|
9
|
+
* instead of inlining the validation logic. This is used when generating
|
|
10
|
+
* files where each ref has its own separate file with a parser.
|
|
11
|
+
*/
|
|
12
|
+
readonly useRefImports?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* When true, the generated parser emits a console.warn for every input key
|
|
15
|
+
* that is not declared in the schema's properties.
|
|
16
|
+
*/
|
|
17
|
+
readonly logWarnings?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* When true, the generated parser throws on type/shape mismatches instead
|
|
20
|
+
* of coercing invalid input to default values. Throws on the first violation
|
|
21
|
+
* with a path-aware Error. When the schema sets `additionalProperties: false`,
|
|
22
|
+
* undeclared keys throw too; otherwise they are still allowed.
|
|
23
|
+
*/
|
|
24
|
+
readonly strict?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* When true, the generated parser builds its result from the schema's declared
|
|
27
|
+
* properties only, silently dropping any undeclared input key at every nesting
|
|
28
|
+
* level (zod's `.strip()`). Unlike `additionalProperties: false`, extras are
|
|
29
|
+
* never a validation error — this composes with `strict`: `strict + stripUnknown`
|
|
30
|
+
* still throws on wrong types and missing required properties but strips extras
|
|
31
|
+
* instead of throwing on them. When the schema *also* sets
|
|
32
|
+
* `additionalProperties: false`, rejecting wins over stripping in strict mode.
|
|
33
|
+
*/
|
|
34
|
+
readonly stripUnknown?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Suffix appended to every type/parser name derived from a `$ref`. Must match
|
|
37
|
+
* the suffix used when generating the referenced files. Defaults to `''`.
|
|
38
|
+
*/
|
|
39
|
+
readonly typeSuffix?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The root schema document. When present, a top-level `oneOf`/`anyOf` whose
|
|
42
|
+
* branches are `$ref`s can be resolved to find a shared discriminator and emit a
|
|
43
|
+
* dispatch to the branch parsers, instead of falling back to a passthrough cast.
|
|
44
|
+
*/
|
|
45
|
+
readonly rootSchema?: Record<string, unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* Type names the generated file imports for its `$ref`s. Synthesized private
|
|
48
|
+
* sub-type names (nested objects, array items, the root array's `{Type}Item`)
|
|
49
|
+
* dedup against this set so they can never shadow an imported identifier —
|
|
50
|
+
* which would both fail to compile (duplicate declaration) and silently
|
|
51
|
+
* validate against the wrong schema.
|
|
52
|
+
*/
|
|
53
|
+
readonly reservedNames?: ReadonlySet<string>;
|
|
54
|
+
/**
|
|
55
|
+
* True when this schema is the root document (not a `$ref`-reached definition).
|
|
56
|
+
* The root type name is user-derived (schema `title`, filename, or `--root-type`),
|
|
57
|
+
* so the JSON Schema meta-schema special case — keyed on the literal name
|
|
58
|
+
* `Schema` — must not apply to it, or a common `schema.json` root would silently
|
|
59
|
+
* generate a validation-free pass-through parser.
|
|
60
|
+
*/
|
|
61
|
+
readonly isRoot?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* When true, a mis-cased string that matches a declared `enum`/`const` member
|
|
64
|
+
* case-insensitively is normalized to that member's exact casing (e.g. `hElLo`
|
|
65
|
+
* → `hello`) instead of coercing to the default. Coerce mode only — strict
|
|
66
|
+
* parsers still reject a casing mismatch. The normalization lives on the
|
|
67
|
+
* failure branch of the coercion ternary, so a correctly-cased value keeps the
|
|
68
|
+
* exact `===` fast path and the hot path is unaffected.
|
|
69
|
+
*/
|
|
70
|
+
readonly caseInsensitive?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* How the fast paths prove a closed object carries no undeclared key —
|
|
73
|
+
* `Object.getPrototypeOf(input) === Object.prototype && Object.keys(input).length`
|
|
74
|
+
* (the default) or a `for…in` count. See {@link UnknownKeysStrategy} for the
|
|
75
|
+
* trade-off, and {@link keyCountCheck} for what each form emits.
|
|
76
|
+
*/
|
|
77
|
+
readonly unknownKeys?: UnknownKeysStrategy;
|
|
78
|
+
/**
|
|
79
|
+
* The exact source of the `validate{TypeName}Shape` predicate emitted into
|
|
80
|
+
* the same file as this parser (generate-files produces it for the root
|
|
81
|
+
* type). When the parser can prove — by rendering the predicate it would
|
|
82
|
+
* need and comparing byte-for-byte — that this emitted predicate tests
|
|
83
|
+
* exactly its own fast-path guard, it emits `validate{TypeName}Shape(input)`
|
|
84
|
+
* instead of a second inline copy of the whole check chain, which roughly
|
|
85
|
+
* halves the emitted checks per object type. Any mismatch (composition
|
|
86
|
+
* keywords, conditional flattening, alias/union predicates, stubs) keeps
|
|
87
|
+
* the inline guard, so a wrong substitution is impossible by construction.
|
|
88
|
+
*/
|
|
89
|
+
readonly shapeValidatorSource?: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Generates a safe parser as an arrow function that never throws.
|
|
93
|
+
* The parser provides default values for all missing or invalid fields.
|
|
94
|
+
*
|
|
95
|
+
* When useRefImports is enabled, properties with $ref will call the imported
|
|
96
|
+
* parser function (e.g., parseContact(input?.contact)) instead of inlining
|
|
97
|
+
* the resolved schema's validation logic. Array properties whose items are a
|
|
98
|
+
* $ref will map each element through the imported parser.
|
|
99
|
+
*/
|
|
100
|
+
export declare const generateParserFunction: (schema: JSONSchema, typeName: string, rawOptions?: GenerateParserOptions) => string;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the predicate function name for a generated shape validator.
|
|
103
|
+
* @example shapeValidatorFunctionName('CustomerObject') // 'validateCustomerObjectShape'
|
|
104
|
+
*/
|
|
105
|
+
export declare const shapeValidatorFunctionName: (typeName: string) => string;
|
|
106
|
+
/**
|
|
107
|
+
* Generates a `validate{TypeName}Shape(input)` predicate that returns true
|
|
108
|
+
* iff `input` already matches the shape produced by the parser's fast path —
|
|
109
|
+
* i.e. the parser would return `{ ...input } as TypeName` without coercion.
|
|
110
|
+
*
|
|
111
|
+
* Parents call this predicate to fast-path through nested ref properties
|
|
112
|
+
* (and arrays of refs) without recursing into their parser.
|
|
113
|
+
*
|
|
114
|
+
* Returns a stub that always returns `false` for schemas that cannot be
|
|
115
|
+
* predicated (composition, conditionals, pattern properties, complex refs
|
|
116
|
+
* in additionalProperties). The stub is safe: parents calling it will fall
|
|
117
|
+
* through to the slow path, matching the pre-deep-fast-path behavior.
|
|
118
|
+
*/
|
|
119
|
+
export declare const generateShapeValidator: (schema: JSONSchema, typeName: string, useRefImports: boolean, suffix?: string, exported?: boolean, stripUnknown?: boolean, reservedNames?: ReadonlySet<string>, unknownKeys?: UnknownKeysStrategy) => string;
|
|
120
|
+
export {};
|