@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,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;
|