@amazon/vinyl-validation 1.2.0 → 2.0.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/README.md +42 -0
- package/dist/index.cjs +334 -171
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +322 -159
- package/dist/index.js.map +4 -4
- package/dist/types/current/index.d.ts +15 -14
- package/dist/types/current/index.d.ts.map +1 -1
- package/dist/types/current/index.js +15 -14
- package/dist/types/current/tsconfig.tsbuildinfo +1 -1
- package/dist/types/current/validation/ArraySchema.d.ts.map +1 -1
- package/dist/types/current/validation/ArraySchema.js +11 -4
- package/dist/types/current/validation/JsonSchema.d.ts +63 -0
- package/dist/types/current/validation/JsonSchema.d.ts.map +1 -0
- package/dist/types/current/validation/JsonSchema.js +59 -0
- package/dist/types/current/validation/NumberSchema.d.ts.map +1 -1
- package/dist/types/current/validation/NumberSchema.js +6 -6
- package/dist/types/current/validation/ObjectSchema.d.ts.map +1 -1
- package/dist/types/current/validation/ObjectSchema.js +12 -1
- package/dist/types/current/validation/RecordSchema.d.ts.map +1 -1
- package/dist/types/current/validation/RecordSchema.js +4 -0
- package/dist/types/current/validation/SchemaBase.d.ts +23 -1
- package/dist/types/current/validation/SchemaBase.d.ts.map +1 -1
- package/dist/types/current/validation/SchemaBase.js +36 -0
- package/dist/types/current/validation/SetSchema.d.ts.map +1 -1
- package/dist/types/current/validation/SetSchema.js +6 -1
- package/dist/types/current/validation/StringSchema.d.ts.map +1 -1
- package/dist/types/current/validation/StringSchema.js +4 -4
- package/dist/types/current/validation/Validator.d.ts +14 -2
- package/dist/types/current/validation/Validator.d.ts.map +1 -1
- package/dist/types/current/validation/Validator.js +6 -2
- package/dist/types/current/validation/ValueSchema.d.ts +1 -0
- package/dist/types/current/validation/ValueSchema.d.ts.map +1 -1
- package/dist/types/current/validation/ValueSchema.js +7 -2
- package/dist/types/current/validation/andValidators.d.ts.map +1 -1
- package/dist/types/current/validation/andValidators.js +2 -1
- package/dist/types/current/validation/orValidators.d.ts.map +1 -1
- package/dist/types/current/validation/orValidators.js +18 -1
- package/dist/types/current/validation/typeOfValidators.d.ts.map +1 -1
- package/dist/types/current/validation/typeOfValidators.js +13 -1
- package/dist/types/current/validation/validators.d.ts.map +1 -1
- package/dist/types/current/validation/validators.js +1 -1
- package/dist/types/ts3.7/index.d.ts +15 -14
- package/dist/types/ts3.7/validation/JsonSchema.d.ts +63 -0
- package/dist/types/ts3.7/validation/SchemaBase.d.ts +18 -1
- package/dist/types/ts3.7/validation/Validator.d.ts +14 -2
- package/dist/types/ts3.7/validation/ValueSchema.d.ts +1 -0
- package/dist/types/ts4.1/index.d.ts +15 -14
- package/dist/types/ts4.1/validation/JsonSchema.d.ts +63 -0
- package/dist/types/ts4.1/validation/SchemaBase.d.ts +18 -1
- package/dist/types/ts4.1/validation/Validator.d.ts +14 -2
- package/dist/types/ts4.1/validation/ValueSchema.d.ts +1 -0
- package/dist/types/ts4.7/index.d.ts +15 -14
- package/dist/types/ts4.7/validation/JsonSchema.d.ts +63 -0
- package/dist/types/ts4.7/validation/SchemaBase.d.ts +23 -1
- package/dist/types/ts4.7/validation/Validator.d.ts +14 -2
- package/dist/types/ts4.7/validation/ValueSchema.d.ts +1 -0
- package/dist/unit/types/current/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -11
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* The `type` keyword value of a JSON Schema node.
|
|
4
|
+
*/
|
|
5
|
+
export type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
6
|
+
/**
|
|
7
|
+
* A JSON Schema definition (a draft-07 compatible subset) describing the shape a value must take.
|
|
8
|
+
*
|
|
9
|
+
* This is the serializable representation produced by {@link toJsonSchema}, suitable for handing to
|
|
10
|
+
* an LLM as the shape it must produce. It intentionally mirrors zod-to-json-schema output for the
|
|
11
|
+
* node types this library supports.
|
|
12
|
+
*/
|
|
13
|
+
export interface JsonSchema {
|
|
14
|
+
readonly type?: JsonSchemaType | readonly JsonSchemaType[];
|
|
15
|
+
/**
|
|
16
|
+
* A human-readable description, attached to a schema node via its `describe` method.
|
|
17
|
+
*/
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly properties?: Readonly<Record<string, JsonSchema>>;
|
|
20
|
+
readonly required?: readonly string[];
|
|
21
|
+
readonly additionalProperties?: boolean | JsonSchema;
|
|
22
|
+
readonly items?: JsonSchema | readonly JsonSchema[];
|
|
23
|
+
readonly additionalItems?: boolean;
|
|
24
|
+
readonly minItems?: number;
|
|
25
|
+
readonly maxItems?: number;
|
|
26
|
+
readonly uniqueItems?: boolean;
|
|
27
|
+
readonly minLength?: number;
|
|
28
|
+
readonly maxLength?: number;
|
|
29
|
+
readonly pattern?: string;
|
|
30
|
+
readonly minimum?: number;
|
|
31
|
+
readonly maximum?: number;
|
|
32
|
+
readonly exclusiveMinimum?: number;
|
|
33
|
+
readonly exclusiveMaximum?: number;
|
|
34
|
+
readonly enum?: readonly unknown[];
|
|
35
|
+
readonly anyOf?: readonly JsonSchema[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A source that can produce its own JSON Schema definition. Schema nodes implement this so that
|
|
39
|
+
* per-node metadata (such as a `describe` description) is honored during serialization.
|
|
40
|
+
*/
|
|
41
|
+
export interface JsonSchemaProvider {
|
|
42
|
+
toJsonSchema(): JsonSchema;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Merges JSON Schema fragments into a single definition, later fragments taking precedence.
|
|
46
|
+
*
|
|
47
|
+
* This mirrors an intersection (`and`) of constraints: `properties` are combined and `required`
|
|
48
|
+
* keys are unioned, while all other keywords are overridden by the last fragment that defines them.
|
|
49
|
+
*
|
|
50
|
+
* @param fragments The fragments to merge. `undefined` fragments are ignored.
|
|
51
|
+
*/
|
|
52
|
+
export declare function mergeJsonSchema(...fragments: readonly (JsonSchema | undefined)[]): JsonSchema;
|
|
53
|
+
/**
|
|
54
|
+
* Serializes a validator to a JSON Schema definition.
|
|
55
|
+
*
|
|
56
|
+
* Schema nodes ({@link JsonSchemaProvider}s) serialize themselves so that per-node metadata is
|
|
57
|
+
* honored; other validators contribute their attached JSON Schema fragment, defaulting to the empty
|
|
58
|
+
* schema (which matches any value) when none is attached.
|
|
59
|
+
*
|
|
60
|
+
* @param source The validator or schema node to serialize.
|
|
61
|
+
*/
|
|
62
|
+
export declare function toJsonSchema(source: Validator<any, any>): JsonSchema;
|
|
63
|
+
//# sourceMappingURL=JsonSchema.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ValidationErrorMessage, ValidationOptions, Validator } from './Validator';
|
|
2
2
|
import { PropertyValidator, PropertyValidatorWithOptions } from './PropertyValidator';
|
|
3
|
+
import { JsonSchema, JsonSchemaProvider } from './JsonSchema';
|
|
3
4
|
/**
|
|
4
5
|
* A base class for validation state.
|
|
5
6
|
* Provides methods for chaining.
|
|
@@ -8,13 +9,14 @@ import { PropertyValidator, PropertyValidatorWithOptions } from './PropertyValid
|
|
|
8
9
|
*/
|
|
9
10
|
export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
10
11
|
readonly optional: false;
|
|
11
|
-
}
|
|
12
|
+
}>, JsonSchemaProvider {
|
|
12
13
|
private readonly currentValidator;
|
|
13
14
|
private readonly previousSchema?;
|
|
14
15
|
private readonly operation?;
|
|
15
16
|
__type: import("@amazon/vinyl-util").Invariant<T>;
|
|
16
17
|
private readonly validator;
|
|
17
18
|
private _options;
|
|
19
|
+
private _jsonSchemaDescription?;
|
|
18
20
|
get options(): {
|
|
19
21
|
readonly optional: false;
|
|
20
22
|
};
|
|
@@ -33,6 +35,21 @@ export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
|
33
35
|
*/
|
|
34
36
|
constructor(currentValidator: Validator<any, any>, previousSchema: SchemaBase<any>, operation: (validatorA: Validator<any>, validatorB: Validator<any, any>) => Validator<T>);
|
|
35
37
|
get description(): string;
|
|
38
|
+
get jsonSchema(): (JsonSchema | undefined);
|
|
39
|
+
/**
|
|
40
|
+
* Attaches a human-readable description to this schema, akin to zod's `describe`.
|
|
41
|
+
* The description is metadata only; it does not affect validation, and surfaces in the output
|
|
42
|
+
* of {@link toJsonSchema}.
|
|
43
|
+
*
|
|
44
|
+
* @param description The description to attach.
|
|
45
|
+
* @return A clone of this schema carrying the description.
|
|
46
|
+
*/
|
|
47
|
+
describe(description: string): this;
|
|
48
|
+
/**
|
|
49
|
+
* Serializes this schema to a JSON Schema definition, suitable for describing the shape to an
|
|
50
|
+
* LLM or other consumer. Works similarly to zod-to-json-schema.
|
|
51
|
+
*/
|
|
52
|
+
toJsonSchema(): JsonSchema;
|
|
36
53
|
readonly assert: (input: unknown, origin?: string, path?: readonly string[]) => asserts input is T;
|
|
37
54
|
readonly isValid: (input: unknown) => input is T;
|
|
38
55
|
readonly validate: (input: unknown, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Invariant } from '@amazon/vinyl-util';
|
|
2
|
+
import { JsonSchema } from './JsonSchema';
|
|
2
3
|
/**
|
|
3
4
|
* Represents a validation failure.
|
|
4
5
|
*/
|
|
@@ -27,6 +28,15 @@ export interface Validator<Output extends Input, Input = unknown> {
|
|
|
27
28
|
* A short description of the validator.
|
|
28
29
|
*/
|
|
29
30
|
readonly description: string;
|
|
31
|
+
/**
|
|
32
|
+
* The JSON Schema fragment this validator contributes, if any.
|
|
33
|
+
*
|
|
34
|
+
* Fragments compose the same way the {@link description} does: chaining validators with `and`
|
|
35
|
+
* merges their fragments, and `or` unions them into an `anyOf`. Serialize a full schema with
|
|
36
|
+
* {@link toJsonSchema}. A validator with no meaningful shape (for example a bare type check)
|
|
37
|
+
* may omit this.
|
|
38
|
+
*/
|
|
39
|
+
readonly jsonSchema?: JsonSchema | undefined;
|
|
30
40
|
/**
|
|
31
41
|
* Returns true if the input is valid according to this validator.
|
|
32
42
|
*
|
|
@@ -74,8 +84,9 @@ export declare function createValidationExpectationMessage<Input>(expectationMes
|
|
|
74
84
|
* @param isValid Returns true if the given input is valid.
|
|
75
85
|
* @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
|
|
76
86
|
* expectation template. Default is `stringifyForPrint` with a 500 character max length.
|
|
87
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
77
88
|
*/
|
|
78
|
-
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string): Validator<Output, Input>;
|
|
89
|
+
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string, jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
79
90
|
/**
|
|
80
91
|
* Creates a new deep validator.
|
|
81
92
|
*
|
|
@@ -83,6 +94,7 @@ export declare function createValidator<Output extends Input, Input = unknown>(e
|
|
|
83
94
|
*
|
|
84
95
|
* @param description Describes the validator.
|
|
85
96
|
* @param validate Returns an array of validation failures.
|
|
97
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
86
98
|
*/
|
|
87
|
-
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[]): Validator<Output, Input>;
|
|
99
|
+
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[], jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
88
100
|
//# sourceMappingURL=Validator.d.ts.map
|
|
@@ -4,6 +4,7 @@ export declare const valueValidators: {
|
|
|
4
4
|
readonly any: () => Validator<any>;
|
|
5
5
|
/**
|
|
6
6
|
* Creates a validator that asserts that the input is nullish.
|
|
7
|
+
* JSON Schema has no representation for `undefined`, so only `null` is expressed.
|
|
7
8
|
*/
|
|
8
9
|
readonly nullish: () => Validator<null | undefined>;
|
|
9
10
|
/**
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
/** Auto-generated from build scripts */
|
|
2
|
-
export * from './validation/validators';
|
|
3
|
-
export * from './validation/typeOfValidators';
|
|
4
|
-
export * from './validation/orValidators';
|
|
5
2
|
export * from './validation/andValidators';
|
|
6
|
-
export * from './validation/ValueSchema';
|
|
7
|
-
export * from './validation/ValidatorWithPartial';
|
|
8
|
-
export * from './validation/Validator';
|
|
9
|
-
export * from './validation/StringSchema';
|
|
10
|
-
export * from './validation/SetSchema';
|
|
11
|
-
export * from './validation/SchemaBase';
|
|
12
|
-
export * from './validation/RecordSchema';
|
|
13
|
-
export * from './validation/PropertyValidator';
|
|
14
|
-
export * from './validation/ObjectSchema';
|
|
15
|
-
export * from './validation/NumberSchema';
|
|
16
|
-
export * from './validation/FunctionSchema';
|
|
17
3
|
export * from './validation/ArraySchema';
|
|
4
|
+
export * from './validation/FunctionSchema';
|
|
5
|
+
export * from './validation/JsonSchema';
|
|
6
|
+
export * from './validation/NumberSchema';
|
|
7
|
+
export * from './validation/ObjectSchema';
|
|
8
|
+
export * from './validation/orValidators';
|
|
9
|
+
export * from './validation/PropertyValidator';
|
|
10
|
+
export * from './validation/RecordSchema';
|
|
11
|
+
export * from './validation/SchemaBase';
|
|
12
|
+
export * from './validation/SetSchema';
|
|
13
|
+
export * from './validation/StringSchema';
|
|
14
|
+
export * from './validation/typeOfValidators';
|
|
15
|
+
export * from './validation/Validator';
|
|
16
|
+
export * from './validation/validators';
|
|
17
|
+
export * from './validation/ValidatorWithPartial';
|
|
18
|
+
export * from './validation/ValueSchema';
|
|
18
19
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* The `type` keyword value of a JSON Schema node.
|
|
4
|
+
*/
|
|
5
|
+
export type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
6
|
+
/**
|
|
7
|
+
* A JSON Schema definition (a draft-07 compatible subset) describing the shape a value must take.
|
|
8
|
+
*
|
|
9
|
+
* This is the serializable representation produced by {@link toJsonSchema}, suitable for handing to
|
|
10
|
+
* an LLM as the shape it must produce. It intentionally mirrors zod-to-json-schema output for the
|
|
11
|
+
* node types this library supports.
|
|
12
|
+
*/
|
|
13
|
+
export interface JsonSchema {
|
|
14
|
+
readonly type?: JsonSchemaType | readonly JsonSchemaType[];
|
|
15
|
+
/**
|
|
16
|
+
* A human-readable description, attached to a schema node via its `describe` method.
|
|
17
|
+
*/
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly properties?: Readonly<Record<string, JsonSchema>>;
|
|
20
|
+
readonly required?: readonly string[];
|
|
21
|
+
readonly additionalProperties?: boolean | JsonSchema;
|
|
22
|
+
readonly items?: JsonSchema | readonly JsonSchema[];
|
|
23
|
+
readonly additionalItems?: boolean;
|
|
24
|
+
readonly minItems?: number;
|
|
25
|
+
readonly maxItems?: number;
|
|
26
|
+
readonly uniqueItems?: boolean;
|
|
27
|
+
readonly minLength?: number;
|
|
28
|
+
readonly maxLength?: number;
|
|
29
|
+
readonly pattern?: string;
|
|
30
|
+
readonly minimum?: number;
|
|
31
|
+
readonly maximum?: number;
|
|
32
|
+
readonly exclusiveMinimum?: number;
|
|
33
|
+
readonly exclusiveMaximum?: number;
|
|
34
|
+
readonly enum?: readonly unknown[];
|
|
35
|
+
readonly anyOf?: readonly JsonSchema[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A source that can produce its own JSON Schema definition. Schema nodes implement this so that
|
|
39
|
+
* per-node metadata (such as a `describe` description) is honored during serialization.
|
|
40
|
+
*/
|
|
41
|
+
export interface JsonSchemaProvider {
|
|
42
|
+
toJsonSchema(): JsonSchema;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Merges JSON Schema fragments into a single definition, later fragments taking precedence.
|
|
46
|
+
*
|
|
47
|
+
* This mirrors an intersection (`and`) of constraints: `properties` are combined and `required`
|
|
48
|
+
* keys are unioned, while all other keywords are overridden by the last fragment that defines them.
|
|
49
|
+
*
|
|
50
|
+
* @param fragments The fragments to merge. `undefined` fragments are ignored.
|
|
51
|
+
*/
|
|
52
|
+
export declare function mergeJsonSchema(...fragments: readonly (JsonSchema | undefined)[]): JsonSchema;
|
|
53
|
+
/**
|
|
54
|
+
* Serializes a validator to a JSON Schema definition.
|
|
55
|
+
*
|
|
56
|
+
* Schema nodes ({@link JsonSchemaProvider}s) serialize themselves so that per-node metadata is
|
|
57
|
+
* honored; other validators contribute their attached JSON Schema fragment, defaulting to the empty
|
|
58
|
+
* schema (which matches any value) when none is attached.
|
|
59
|
+
*
|
|
60
|
+
* @param source The validator or schema node to serialize.
|
|
61
|
+
*/
|
|
62
|
+
export declare function toJsonSchema(source: Validator<any, any>): JsonSchema;
|
|
63
|
+
//# sourceMappingURL=JsonSchema.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ValidationErrorMessage, ValidationOptions, Validator } from './Validator';
|
|
2
2
|
import type { PropertyValidator, PropertyValidatorWithOptions } from './PropertyValidator';
|
|
3
|
+
import type { JsonSchema, JsonSchemaProvider } from './JsonSchema';
|
|
3
4
|
/**
|
|
4
5
|
* A base class for validation state.
|
|
5
6
|
* Provides methods for chaining.
|
|
@@ -8,13 +9,14 @@ import type { PropertyValidator, PropertyValidatorWithOptions } from './Property
|
|
|
8
9
|
*/
|
|
9
10
|
export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
10
11
|
readonly optional: false;
|
|
11
|
-
}
|
|
12
|
+
}>, JsonSchemaProvider {
|
|
12
13
|
private readonly currentValidator;
|
|
13
14
|
private readonly previousSchema?;
|
|
14
15
|
private readonly operation?;
|
|
15
16
|
__type: import("@amazon/vinyl-util").Invariant<T>;
|
|
16
17
|
private readonly validator;
|
|
17
18
|
private _options;
|
|
19
|
+
private _jsonSchemaDescription?;
|
|
18
20
|
get options(): {
|
|
19
21
|
readonly optional: false;
|
|
20
22
|
};
|
|
@@ -33,6 +35,21 @@ export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
|
33
35
|
*/
|
|
34
36
|
constructor(currentValidator: Validator<any, any>, previousSchema: SchemaBase<any>, operation: (validatorA: Validator<any>, validatorB: Validator<any, any>) => Validator<T>);
|
|
35
37
|
get description(): string;
|
|
38
|
+
get jsonSchema(): (JsonSchema | undefined);
|
|
39
|
+
/**
|
|
40
|
+
* Attaches a human-readable description to this schema, akin to zod's `describe`.
|
|
41
|
+
* The description is metadata only; it does not affect validation, and surfaces in the output
|
|
42
|
+
* of {@link toJsonSchema}.
|
|
43
|
+
*
|
|
44
|
+
* @param description The description to attach.
|
|
45
|
+
* @return A clone of this schema carrying the description.
|
|
46
|
+
*/
|
|
47
|
+
describe(description: string): this;
|
|
48
|
+
/**
|
|
49
|
+
* Serializes this schema to a JSON Schema definition, suitable for describing the shape to an
|
|
50
|
+
* LLM or other consumer. Works similarly to zod-to-json-schema.
|
|
51
|
+
*/
|
|
52
|
+
toJsonSchema(): JsonSchema;
|
|
36
53
|
readonly assert: (input: unknown, origin?: string, path?: readonly string[]) => asserts input is T;
|
|
37
54
|
readonly isValid: (input: unknown) => input is T;
|
|
38
55
|
readonly validate: (input: unknown, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Invariant } from '@amazon/vinyl-util';
|
|
2
|
+
import type { JsonSchema } from './JsonSchema';
|
|
2
3
|
/**
|
|
3
4
|
* Represents a validation failure.
|
|
4
5
|
*/
|
|
@@ -27,6 +28,15 @@ export interface Validator<Output extends Input, Input = unknown> {
|
|
|
27
28
|
* A short description of the validator.
|
|
28
29
|
*/
|
|
29
30
|
readonly description: string;
|
|
31
|
+
/**
|
|
32
|
+
* The JSON Schema fragment this validator contributes, if any.
|
|
33
|
+
*
|
|
34
|
+
* Fragments compose the same way the {@link description} does: chaining validators with `and`
|
|
35
|
+
* merges their fragments, and `or` unions them into an `anyOf`. Serialize a full schema with
|
|
36
|
+
* {@link toJsonSchema}. A validator with no meaningful shape (for example a bare type check)
|
|
37
|
+
* may omit this.
|
|
38
|
+
*/
|
|
39
|
+
readonly jsonSchema?: JsonSchema | undefined;
|
|
30
40
|
/**
|
|
31
41
|
* Returns true if the input is valid according to this validator.
|
|
32
42
|
*
|
|
@@ -74,8 +84,9 @@ export declare function createValidationExpectationMessage<Input>(expectationMes
|
|
|
74
84
|
* @param isValid Returns true if the given input is valid.
|
|
75
85
|
* @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
|
|
76
86
|
* expectation template. Default is `stringifyForPrint` with a 500 character max length.
|
|
87
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
77
88
|
*/
|
|
78
|
-
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string): Validator<Output, Input>;
|
|
89
|
+
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string, jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
79
90
|
/**
|
|
80
91
|
* Creates a new deep validator.
|
|
81
92
|
*
|
|
@@ -83,6 +94,7 @@ export declare function createValidator<Output extends Input, Input = unknown>(e
|
|
|
83
94
|
*
|
|
84
95
|
* @param description Describes the validator.
|
|
85
96
|
* @param validate Returns an array of validation failures.
|
|
97
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
86
98
|
*/
|
|
87
|
-
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[]): Validator<Output, Input>;
|
|
99
|
+
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[], jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
88
100
|
//# sourceMappingURL=Validator.d.ts.map
|
|
@@ -4,6 +4,7 @@ export declare const valueValidators: {
|
|
|
4
4
|
readonly any: () => Validator<any>;
|
|
5
5
|
/**
|
|
6
6
|
* Creates a validator that asserts that the input is nullish.
|
|
7
|
+
* JSON Schema has no representation for `undefined`, so only `null` is expressed.
|
|
7
8
|
*/
|
|
8
9
|
readonly nullish: () => Validator<null | undefined>;
|
|
9
10
|
/**
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
/** Auto-generated from build scripts */
|
|
2
|
-
export * from './validation/validators';
|
|
3
|
-
export * from './validation/typeOfValidators';
|
|
4
|
-
export * from './validation/orValidators';
|
|
5
2
|
export * from './validation/andValidators';
|
|
6
|
-
export * from './validation/ValueSchema';
|
|
7
|
-
export * from './validation/ValidatorWithPartial';
|
|
8
|
-
export * from './validation/Validator';
|
|
9
|
-
export * from './validation/StringSchema';
|
|
10
|
-
export * from './validation/SetSchema';
|
|
11
|
-
export * from './validation/SchemaBase';
|
|
12
|
-
export * from './validation/RecordSchema';
|
|
13
|
-
export * from './validation/PropertyValidator';
|
|
14
|
-
export * from './validation/ObjectSchema';
|
|
15
|
-
export * from './validation/NumberSchema';
|
|
16
|
-
export * from './validation/FunctionSchema';
|
|
17
3
|
export * from './validation/ArraySchema';
|
|
4
|
+
export * from './validation/FunctionSchema';
|
|
5
|
+
export * from './validation/JsonSchema';
|
|
6
|
+
export * from './validation/NumberSchema';
|
|
7
|
+
export * from './validation/ObjectSchema';
|
|
8
|
+
export * from './validation/orValidators';
|
|
9
|
+
export * from './validation/PropertyValidator';
|
|
10
|
+
export * from './validation/RecordSchema';
|
|
11
|
+
export * from './validation/SchemaBase';
|
|
12
|
+
export * from './validation/SetSchema';
|
|
13
|
+
export * from './validation/StringSchema';
|
|
14
|
+
export * from './validation/typeOfValidators';
|
|
15
|
+
export * from './validation/Validator';
|
|
16
|
+
export * from './validation/validators';
|
|
17
|
+
export * from './validation/ValidatorWithPartial';
|
|
18
|
+
export * from './validation/ValueSchema';
|
|
18
19
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* The `type` keyword value of a JSON Schema node.
|
|
4
|
+
*/
|
|
5
|
+
export type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
6
|
+
/**
|
|
7
|
+
* A JSON Schema definition (a draft-07 compatible subset) describing the shape a value must take.
|
|
8
|
+
*
|
|
9
|
+
* This is the serializable representation produced by {@link toJsonSchema}, suitable for handing to
|
|
10
|
+
* an LLM as the shape it must produce. It intentionally mirrors zod-to-json-schema output for the
|
|
11
|
+
* node types this library supports.
|
|
12
|
+
*/
|
|
13
|
+
export interface JsonSchema {
|
|
14
|
+
readonly type?: JsonSchemaType | readonly JsonSchemaType[];
|
|
15
|
+
/**
|
|
16
|
+
* A human-readable description, attached to a schema node via its `describe` method.
|
|
17
|
+
*/
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly properties?: Readonly<Record<string, JsonSchema>>;
|
|
20
|
+
readonly required?: readonly string[];
|
|
21
|
+
readonly additionalProperties?: boolean | JsonSchema;
|
|
22
|
+
readonly items?: JsonSchema | readonly JsonSchema[];
|
|
23
|
+
readonly additionalItems?: boolean;
|
|
24
|
+
readonly minItems?: number;
|
|
25
|
+
readonly maxItems?: number;
|
|
26
|
+
readonly uniqueItems?: boolean;
|
|
27
|
+
readonly minLength?: number;
|
|
28
|
+
readonly maxLength?: number;
|
|
29
|
+
readonly pattern?: string;
|
|
30
|
+
readonly minimum?: number;
|
|
31
|
+
readonly maximum?: number;
|
|
32
|
+
readonly exclusiveMinimum?: number;
|
|
33
|
+
readonly exclusiveMaximum?: number;
|
|
34
|
+
readonly enum?: readonly unknown[];
|
|
35
|
+
readonly anyOf?: readonly JsonSchema[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A source that can produce its own JSON Schema definition. Schema nodes implement this so that
|
|
39
|
+
* per-node metadata (such as a `describe` description) is honored during serialization.
|
|
40
|
+
*/
|
|
41
|
+
export interface JsonSchemaProvider {
|
|
42
|
+
toJsonSchema(): JsonSchema;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Merges JSON Schema fragments into a single definition, later fragments taking precedence.
|
|
46
|
+
*
|
|
47
|
+
* This mirrors an intersection (`and`) of constraints: `properties` are combined and `required`
|
|
48
|
+
* keys are unioned, while all other keywords are overridden by the last fragment that defines them.
|
|
49
|
+
*
|
|
50
|
+
* @param fragments The fragments to merge. `undefined` fragments are ignored.
|
|
51
|
+
*/
|
|
52
|
+
export declare function mergeJsonSchema(...fragments: readonly (JsonSchema | undefined)[]): JsonSchema;
|
|
53
|
+
/**
|
|
54
|
+
* Serializes a validator to a JSON Schema definition.
|
|
55
|
+
*
|
|
56
|
+
* Schema nodes ({@link JsonSchemaProvider}s) serialize themselves so that per-node metadata is
|
|
57
|
+
* honored; other validators contribute their attached JSON Schema fragment, defaulting to the empty
|
|
58
|
+
* schema (which matches any value) when none is attached.
|
|
59
|
+
*
|
|
60
|
+
* @param source The validator or schema node to serialize.
|
|
61
|
+
*/
|
|
62
|
+
export declare function toJsonSchema(source: Validator<any, any>): JsonSchema;
|
|
63
|
+
//# sourceMappingURL=JsonSchema.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ValidationErrorMessage, ValidationOptions, Validator } from './Validator';
|
|
2
2
|
import type { PropertyValidator, PropertyValidatorWithOptions } from './PropertyValidator';
|
|
3
|
+
import type { JsonSchema, JsonSchemaProvider } from './JsonSchema';
|
|
3
4
|
/**
|
|
4
5
|
* A base class for validation state.
|
|
5
6
|
* Provides methods for chaining.
|
|
@@ -8,13 +9,14 @@ import type { PropertyValidator, PropertyValidatorWithOptions } from './Property
|
|
|
8
9
|
*/
|
|
9
10
|
export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
10
11
|
readonly optional: false;
|
|
11
|
-
}
|
|
12
|
+
}>, JsonSchemaProvider {
|
|
12
13
|
private readonly currentValidator;
|
|
13
14
|
private readonly previousSchema?;
|
|
14
15
|
private readonly operation?;
|
|
15
16
|
__type: import("@amazon/vinyl-util").Invariant<T>;
|
|
16
17
|
private readonly validator;
|
|
17
18
|
private _options;
|
|
19
|
+
private _jsonSchemaDescription?;
|
|
18
20
|
get options(): {
|
|
19
21
|
readonly optional: false;
|
|
20
22
|
};
|
|
@@ -33,6 +35,26 @@ export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
|
|
|
33
35
|
*/
|
|
34
36
|
constructor(currentValidator: Validator<any, any>, previousSchema: SchemaBase<any>, operation: (validatorA: Validator<any>, validatorB: Validator<any, any>) => Validator<T>);
|
|
35
37
|
get description(): string;
|
|
38
|
+
/**
|
|
39
|
+
* The composed JSON Schema fragment for this schema, including any {@link describe} description.
|
|
40
|
+
* Composes through chaining the same way {@link description} does; serialize a complete
|
|
41
|
+
* definition with {@link toJsonSchema}.
|
|
42
|
+
*/
|
|
43
|
+
get jsonSchema(): JsonSchema | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Attaches a human-readable description to this schema, akin to zod's `describe`.
|
|
46
|
+
* The description is metadata only; it does not affect validation, and surfaces in the output
|
|
47
|
+
* of {@link toJsonSchema}.
|
|
48
|
+
*
|
|
49
|
+
* @param description The description to attach.
|
|
50
|
+
* @return A clone of this schema carrying the description.
|
|
51
|
+
*/
|
|
52
|
+
describe(description: string): this;
|
|
53
|
+
/**
|
|
54
|
+
* Serializes this schema to a JSON Schema definition, suitable for describing the shape to an
|
|
55
|
+
* LLM or other consumer. Works similarly to zod-to-json-schema.
|
|
56
|
+
*/
|
|
57
|
+
toJsonSchema(): JsonSchema;
|
|
36
58
|
readonly assert: (input: unknown, origin?: string, path?: readonly string[]) => asserts input is T;
|
|
37
59
|
readonly isValid: (input: unknown) => input is T;
|
|
38
60
|
readonly validate: (input: unknown, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Invariant } from '@amazon/vinyl-util';
|
|
2
|
+
import type { JsonSchema } from './JsonSchema';
|
|
2
3
|
/**
|
|
3
4
|
* Represents a validation failure.
|
|
4
5
|
*/
|
|
@@ -27,6 +28,15 @@ export interface Validator<Output extends Input, in Input = unknown> {
|
|
|
27
28
|
* A short description of the validator.
|
|
28
29
|
*/
|
|
29
30
|
readonly description: string;
|
|
31
|
+
/**
|
|
32
|
+
* The JSON Schema fragment this validator contributes, if any.
|
|
33
|
+
*
|
|
34
|
+
* Fragments compose the same way the {@link description} does: chaining validators with `and`
|
|
35
|
+
* merges their fragments, and `or` unions them into an `anyOf`. Serialize a full schema with
|
|
36
|
+
* {@link toJsonSchema}. A validator with no meaningful shape (for example a bare type check)
|
|
37
|
+
* may omit this.
|
|
38
|
+
*/
|
|
39
|
+
readonly jsonSchema?: JsonSchema | undefined;
|
|
30
40
|
/**
|
|
31
41
|
* Returns true if the input is valid according to this validator.
|
|
32
42
|
*
|
|
@@ -74,8 +84,9 @@ export declare function createValidationExpectationMessage<Input>(expectationMes
|
|
|
74
84
|
* @param isValid Returns true if the given input is valid.
|
|
75
85
|
* @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
|
|
76
86
|
* expectation template. Default is `stringifyForPrint` with a 500 character max length.
|
|
87
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
77
88
|
*/
|
|
78
|
-
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string): Validator<Output, Input>;
|
|
89
|
+
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string, jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
79
90
|
/**
|
|
80
91
|
* Creates a new deep validator.
|
|
81
92
|
*
|
|
@@ -83,6 +94,7 @@ export declare function createValidator<Output extends Input, Input = unknown>(e
|
|
|
83
94
|
*
|
|
84
95
|
* @param description Describes the validator.
|
|
85
96
|
* @param validate Returns an array of validation failures.
|
|
97
|
+
* @param jsonSchema The JSON Schema fragment this validator contributes, if any.
|
|
86
98
|
*/
|
|
87
|
-
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[]): Validator<Output, Input>;
|
|
99
|
+
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[], jsonSchema?: JsonSchema): Validator<Output, Input>;
|
|
88
100
|
//# sourceMappingURL=Validator.d.ts.map
|
|
@@ -4,6 +4,7 @@ export declare const valueValidators: {
|
|
|
4
4
|
readonly any: () => Validator<any>;
|
|
5
5
|
/**
|
|
6
6
|
* Creates a validator that asserts that the input is nullish.
|
|
7
|
+
* JSON Schema has no representation for `undefined`, so only `null` is expressed.
|
|
7
8
|
*/
|
|
8
9
|
readonly nullish: () => Validator<null | undefined>;
|
|
9
10
|
/**
|