@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.
Files changed (84) hide show
  1. package/AI.md +92 -0
  2. package/LICENSE +21 -0
  3. package/README.md +184 -0
  4. package/dist/generate.d.ts +135 -0
  5. package/dist/generate.js +74 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +5 -0
  8. package/dist/parsers/generators/assert-supported-keywords.d.ts +30 -0
  9. package/dist/parsers/generators/assert-supported-keywords.js +87 -0
  10. package/dist/parsers/generators/build-schema.d.ts +111 -0
  11. package/dist/parsers/generators/build-schema.js +74 -0
  12. package/dist/parsers/generators/generate-deep-equal-check.d.ts +24 -0
  13. package/dist/parsers/generators/generate-deep-equal-check.js +35 -0
  14. package/dist/parsers/generators/generate-discriminated-union-validation.d.ts +15 -0
  15. package/dist/parsers/generators/generate-discriminated-union-validation.js +28 -0
  16. package/dist/parsers/generators/generate-enum-check.d.ts +35 -0
  17. package/dist/parsers/generators/generate-enum-check.js +31 -0
  18. package/dist/parsers/generators/generate-files.d.ts +138 -0
  19. package/dist/parsers/generators/generate-files.js +89 -0
  20. package/dist/parsers/generators/generate-non-discriminated-union-validation.d.ts +6 -0
  21. package/dist/parsers/generators/generate-non-discriminated-union-validation.js +26 -0
  22. package/dist/parsers/generators/generate-parser-function.d.ts +120 -0
  23. package/dist/parsers/generators/generate-parser-function.js +1973 -0
  24. package/dist/parsers/generators/generate-schema-checks.d.ts +5 -0
  25. package/dist/parsers/generators/generate-schema-checks.js +260 -0
  26. package/dist/parsers/generators/generate-strict-assertion.d.ts +155 -0
  27. package/dist/parsers/generators/generate-strict-assertion.js +759 -0
  28. package/dist/parsers/generators/generate-type-checks.d.ts +148 -0
  29. package/dist/parsers/generators/generate-type-checks.js +454 -0
  30. package/dist/parsers/generators/generate-unique-items-check.d.ts +16 -0
  31. package/dist/parsers/generators/generate-unique-items-check.js +32 -0
  32. package/dist/parsers/generators/generate-validation-expression.d.ts +79 -0
  33. package/dist/parsers/generators/generate-validation-expression.js +416 -0
  34. package/dist/parsers/generators/subschema-match.d.ts +48 -0
  35. package/dist/parsers/generators/subschema-match.js +485 -0
  36. package/dist/parsers/generators/unevaluated-match.d.ts +19 -0
  37. package/dist/parsers/generators/unevaluated-match.js +206 -0
  38. package/dist/parsers/helpers/apply-schema-extensions.d.ts +35 -0
  39. package/dist/parsers/helpers/apply-schema-extensions.js +20 -0
  40. package/dist/parsers/helpers/collect-helpers.d.ts +15 -0
  41. package/dist/parsers/helpers/collect-helpers.js +64 -0
  42. package/dist/parsers/helpers/collect-imports.d.ts +104 -0
  43. package/dist/parsers/helpers/collect-imports.js +227 -0
  44. package/dist/parsers/helpers/find-discriminator.d.ts +6 -0
  45. package/dist/parsers/helpers/find-discriminator.js +27 -0
  46. package/dist/parsers/helpers/get-discriminator-value.d.ts +6 -0
  47. package/dist/parsers/helpers/get-discriminator-value.js +18 -0
  48. package/dist/parsers/index.d.ts +2 -0
  49. package/dist/parsers/index.js +4 -0
  50. package/dist/parsers/types/schema-extensions.d.ts +34 -0
  51. package/dist/parsers/types/schema-extensions.js +0 -0
  52. package/dist/rehome-parser-file.d.ts +25 -0
  53. package/dist/rehome-parser-file.js +74 -0
  54. package/dist/validators/generators/assert-generatable-refs.d.ts +19 -0
  55. package/dist/validators/generators/assert-generatable-refs.js +12 -0
  56. package/dist/validators/generators/assert-unevaluated-generatable.d.ts +30 -0
  57. package/dist/validators/generators/assert-unevaluated-generatable.js +71 -0
  58. package/dist/validators/generators/build-schema.d.ts +61 -0
  59. package/dist/validators/generators/build-schema.js +666 -0
  60. package/dist/validators/generators/collect-emitted-refs.d.ts +26 -0
  61. package/dist/validators/generators/collect-emitted-refs.js +112 -0
  62. package/dist/validators/generators/collect-validator-imports.d.ts +86 -0
  63. package/dist/validators/generators/collect-validator-imports.js +62 -0
  64. package/dist/validators/generators/emit-format-checks.d.ts +50 -0
  65. package/dist/validators/generators/emit-format-checks.js +248 -0
  66. package/dist/validators/generators/enforced-keywords.d.ts +33 -0
  67. package/dist/validators/generators/enforced-keywords.js +70 -0
  68. package/dist/validators/generators/folds-to-constant.d.ts +15 -0
  69. package/dist/validators/generators/folds-to-constant.js +43 -0
  70. package/dist/validators/generators/generate-coerce-function.d.ts +22 -0
  71. package/dist/validators/generators/generate-coerce-function.js +208 -0
  72. package/dist/validators/generators/generate-files.d.ts +92 -0
  73. package/dist/validators/generators/generate-files.js +86 -0
  74. package/dist/validators/generators/generate-repair-function.d.ts +27 -0
  75. package/dist/validators/generators/generate-repair-function.js +102 -0
  76. package/dist/validators/generators/generate-validator-function.d.ts +58 -0
  77. package/dist/validators/generators/generate-validator-function.js +1691 -0
  78. package/dist/validators/generators/tuple-shape.d.ts +31 -0
  79. package/dist/validators/generators/tuple-shape.js +20 -0
  80. package/dist/validators/generators/unevaluated-match.d.ts +29 -0
  81. package/dist/validators/generators/unevaluated-match.js +223 -0
  82. package/dist/validators/index.d.ts +2 -0
  83. package/dist/validators/index.js +4 -0
  84. 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 {};