@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,58 @@
1
+ import { type UnknownKeysStrategy } from '@amritk/helpers/unknown-keys-strategy';
2
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
3
+ /**
4
+ * Generates the exported boolean guard `isTypeName`.
5
+ *
6
+ * Unlike `validateTypeName` (which returns rich `ValidationResult` errors), this
7
+ * is a single flat boolean predicate — no error array, no cold-path call — so V8
8
+ * inlines it like a hand-written `check`, matching the shape of TypeBox's
9
+ * compiled checker. It returns the *same verdict* as the validator. When the
10
+ * schema carries anything the flat form can't mirror exactly, it falls back to
11
+ * `validateTypeName(input) === true`, which is always correct.
12
+ *
13
+ * The signature is `input is TypeName` whenever that narrowing is sound, and a
14
+ * plain `boolean` when it is not — see {@link typeDescribesEveryAcceptedValue}.
15
+ *
16
+ * `unknownKeys` picks how a closed object's no-extras test counts keys — see
17
+ * {@link UnknownKeysStrategy}.
18
+ */
19
+ export declare const generateBooleanGuard: (schema: JSONSchema, typeName: string, _suffix?: string, unknownKeys?: UnknownKeysStrategy, formats?: ReadonlySet<string>) => string;
20
+ /**
21
+ * Generates a TypeScript validator function from a JSON Schema.
22
+ *
23
+ * The generated function accepts `unknown` input and returns `true` if valid,
24
+ * or `{ valid: false, errors }` with a list of errors if not.
25
+ *
26
+ * Object schemas check that required properties are present and that all
27
+ * provided properties match their declared types. Non-object schemas (strings,
28
+ * numbers, enums, $refs) emit an inline type check.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * generateValidatorFunction({ type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }, 'Info')
33
+ * // export const validateInfo = (input: unknown, _path = ''): ValidationResult => {
34
+ * // if (typeof input !== 'object' || ...) return { valid: false, ... }
35
+ * // const errors: ValidationError[] = []
36
+ * // const obj = input as Record<string, unknown>
37
+ * // if (!('name' in obj)) { errors.push(...) } else if (typeof obj['name'] !== 'string') { errors.push(...) }
38
+ * // return errors.length > 0 ? { valid: false, errors } : true
39
+ * // }
40
+ * ```
41
+ */
42
+ export declare const generateValidatorFunction: (schema: JSONSchema, typeName: string, suffix?: string, rootSchema?: Record<string, unknown>, unknownKeys?: UnknownKeysStrategy, formats?: ReadonlySet<string>, branchErrors?: boolean) => string;
43
+ /**
44
+ * Generates the fail-fast half: a `checkX(input, _path?)` that returns the same
45
+ * `ValidationResult` as `validateX` and stops at the first violation, so the
46
+ * `errors` array it hands back holds exactly one error — the one `validateX`
47
+ * would have reported first.
48
+ *
49
+ * Same return type on purpose. A caller that already renders a `ValidationResult`
50
+ * renders this one with the code it has; the only difference is how much of the
51
+ * document was looked at before the answer came back.
52
+ *
53
+ * A schema whose emitted body would report with no runtime condition in front of
54
+ * it cannot take this shape — see {@link FailFast} — so for those `checkX` runs
55
+ * `validateX` and hands back its first error. The verdict is identical either
56
+ * way; only the short circuit is lost.
57
+ */
58
+ export declare const generateCheckFunction: (schema: JSONSchema, typeName: string, suffix?: string, rootSchema?: Record<string, unknown>, unknownKeys?: UnknownKeysStrategy, formats?: ReadonlySet<string>) => string;