@amazon/vinyl-validation 1.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.
Files changed (109) hide show
  1. package/dist/index.cjs +1325 -0
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.js +1308 -0
  4. package/dist/index.js.map +7 -0
  5. package/dist/types/current/index.d.ts +18 -0
  6. package/dist/types/current/index.d.ts.map +1 -0
  7. package/dist/types/current/index.js +17 -0
  8. package/dist/types/current/tsconfig.tsbuildinfo +1 -0
  9. package/dist/types/current/validation/ArraySchema.d.ts +82 -0
  10. package/dist/types/current/validation/ArraySchema.d.ts.map +1 -0
  11. package/dist/types/current/validation/ArraySchema.js +148 -0
  12. package/dist/types/current/validation/FunctionSchema.d.ts +21 -0
  13. package/dist/types/current/validation/FunctionSchema.d.ts.map +1 -0
  14. package/dist/types/current/validation/FunctionSchema.js +42 -0
  15. package/dist/types/current/validation/NumberSchema.d.ts +100 -0
  16. package/dist/types/current/validation/NumberSchema.d.ts.map +1 -0
  17. package/dist/types/current/validation/NumberSchema.js +146 -0
  18. package/dist/types/current/validation/ObjectSchema.d.ts +54 -0
  19. package/dist/types/current/validation/ObjectSchema.d.ts.map +1 -0
  20. package/dist/types/current/validation/ObjectSchema.js +125 -0
  21. package/dist/types/current/validation/PropertyValidator.d.ts +23 -0
  22. package/dist/types/current/validation/PropertyValidator.d.ts.map +1 -0
  23. package/dist/types/current/validation/PropertyValidator.js +5 -0
  24. package/dist/types/current/validation/RecordSchema.d.ts +58 -0
  25. package/dist/types/current/validation/RecordSchema.d.ts.map +1 -0
  26. package/dist/types/current/validation/RecordSchema.js +101 -0
  27. package/dist/types/current/validation/SchemaBase.d.ts +65 -0
  28. package/dist/types/current/validation/SchemaBase.d.ts.map +1 -0
  29. package/dist/types/current/validation/SchemaBase.js +90 -0
  30. package/dist/types/current/validation/SetSchema.d.ts +38 -0
  31. package/dist/types/current/validation/SetSchema.d.ts.map +1 -0
  32. package/dist/types/current/validation/SetSchema.js +74 -0
  33. package/dist/types/current/validation/StringSchema.d.ts +68 -0
  34. package/dist/types/current/validation/StringSchema.d.ts.map +1 -0
  35. package/dist/types/current/validation/StringSchema.js +109 -0
  36. package/dist/types/current/validation/Validator.d.ts +88 -0
  37. package/dist/types/current/validation/Validator.d.ts.map +1 -0
  38. package/dist/types/current/validation/Validator.js +95 -0
  39. package/dist/types/current/validation/ValidatorWithPartial.d.ts +26 -0
  40. package/dist/types/current/validation/ValidatorWithPartial.d.ts.map +1 -0
  41. package/dist/types/current/validation/ValidatorWithPartial.js +31 -0
  42. package/dist/types/current/validation/ValueSchema.d.ts +52 -0
  43. package/dist/types/current/validation/ValueSchema.d.ts.map +1 -0
  44. package/dist/types/current/validation/ValueSchema.js +81 -0
  45. package/dist/types/current/validation/andValidators.d.ts +19 -0
  46. package/dist/types/current/validation/andValidators.d.ts.map +1 -0
  47. package/dist/types/current/validation/andValidators.js +48 -0
  48. package/dist/types/current/validation/orValidators.d.ts +19 -0
  49. package/dist/types/current/validation/orValidators.d.ts.map +1 -0
  50. package/dist/types/current/validation/orValidators.js +74 -0
  51. package/dist/types/current/validation/typeOfValidators.d.ts +15 -0
  52. package/dist/types/current/validation/typeOfValidators.d.ts.map +1 -0
  53. package/dist/types/current/validation/typeOfValidators.js +58 -0
  54. package/dist/types/current/validation/validators.d.ts +127 -0
  55. package/dist/types/current/validation/validators.d.ts.map +1 -0
  56. package/dist/types/current/validation/validators.js +168 -0
  57. package/dist/types/ts3.7/index.d.ts +18 -0
  58. package/dist/types/ts3.7/validation/ArraySchema.d.ts +86 -0
  59. package/dist/types/ts3.7/validation/FunctionSchema.d.ts +21 -0
  60. package/dist/types/ts3.7/validation/NumberSchema.d.ts +100 -0
  61. package/dist/types/ts3.7/validation/ObjectSchema.d.ts +56 -0
  62. package/dist/types/ts3.7/validation/PropertyValidator.d.ts +23 -0
  63. package/dist/types/ts3.7/validation/RecordSchema.d.ts +58 -0
  64. package/dist/types/ts3.7/validation/SchemaBase.d.ts +65 -0
  65. package/dist/types/ts3.7/validation/SetSchema.d.ts +38 -0
  66. package/dist/types/ts3.7/validation/StringSchema.d.ts +68 -0
  67. package/dist/types/ts3.7/validation/Validator.d.ts +88 -0
  68. package/dist/types/ts3.7/validation/ValidatorWithPartial.d.ts +26 -0
  69. package/dist/types/ts3.7/validation/ValueSchema.d.ts +52 -0
  70. package/dist/types/ts3.7/validation/andValidators.d.ts +19 -0
  71. package/dist/types/ts3.7/validation/orValidators.d.ts +19 -0
  72. package/dist/types/ts3.7/validation/typeOfValidators.d.ts +15 -0
  73. package/dist/types/ts3.7/validation/validators.d.ts +127 -0
  74. package/dist/types/ts4.1/index.d.ts +18 -0
  75. package/dist/types/ts4.1/validation/ArraySchema.d.ts +86 -0
  76. package/dist/types/ts4.1/validation/FunctionSchema.d.ts +21 -0
  77. package/dist/types/ts4.1/validation/NumberSchema.d.ts +100 -0
  78. package/dist/types/ts4.1/validation/ObjectSchema.d.ts +56 -0
  79. package/dist/types/ts4.1/validation/PropertyValidator.d.ts +23 -0
  80. package/dist/types/ts4.1/validation/RecordSchema.d.ts +58 -0
  81. package/dist/types/ts4.1/validation/SchemaBase.d.ts +65 -0
  82. package/dist/types/ts4.1/validation/SetSchema.d.ts +38 -0
  83. package/dist/types/ts4.1/validation/StringSchema.d.ts +68 -0
  84. package/dist/types/ts4.1/validation/Validator.d.ts +88 -0
  85. package/dist/types/ts4.1/validation/ValidatorWithPartial.d.ts +26 -0
  86. package/dist/types/ts4.1/validation/ValueSchema.d.ts +52 -0
  87. package/dist/types/ts4.1/validation/andValidators.d.ts +19 -0
  88. package/dist/types/ts4.1/validation/orValidators.d.ts +19 -0
  89. package/dist/types/ts4.1/validation/typeOfValidators.d.ts +15 -0
  90. package/dist/types/ts4.1/validation/validators.d.ts +128 -0
  91. package/dist/types/ts4.7/index.d.ts +18 -0
  92. package/dist/types/ts4.7/validation/ArraySchema.d.ts +86 -0
  93. package/dist/types/ts4.7/validation/FunctionSchema.d.ts +21 -0
  94. package/dist/types/ts4.7/validation/NumberSchema.d.ts +100 -0
  95. package/dist/types/ts4.7/validation/ObjectSchema.d.ts +56 -0
  96. package/dist/types/ts4.7/validation/PropertyValidator.d.ts +23 -0
  97. package/dist/types/ts4.7/validation/RecordSchema.d.ts +58 -0
  98. package/dist/types/ts4.7/validation/SchemaBase.d.ts +65 -0
  99. package/dist/types/ts4.7/validation/SetSchema.d.ts +38 -0
  100. package/dist/types/ts4.7/validation/StringSchema.d.ts +68 -0
  101. package/dist/types/ts4.7/validation/Validator.d.ts +88 -0
  102. package/dist/types/ts4.7/validation/ValidatorWithPartial.d.ts +26 -0
  103. package/dist/types/ts4.7/validation/ValueSchema.d.ts +52 -0
  104. package/dist/types/ts4.7/validation/andValidators.d.ts +19 -0
  105. package/dist/types/ts4.7/validation/orValidators.d.ts +19 -0
  106. package/dist/types/ts4.7/validation/typeOfValidators.d.ts +15 -0
  107. package/dist/types/ts4.7/validation/validators.d.ts +127 -0
  108. package/docs/VALIDATION.md +344 -0
  109. package/package.json +82 -0
@@ -0,0 +1,65 @@
1
+ import { ValidationErrorMessage, ValidationOptions, Validator } from './Validator';
2
+ import { PropertyValidator, PropertyValidatorWithOptions } from './PropertyValidator';
3
+ /**
4
+ * A base class for validation state.
5
+ * Provides methods for chaining.
6
+ *
7
+ * Subclasses should not override constructors.
8
+ */
9
+ export declare abstract class SchemaBase<T> implements PropertyValidator<T, {
10
+ readonly optional: false;
11
+ }> {
12
+ private readonly currentValidator;
13
+ private readonly previousSchema?;
14
+ private readonly operation?;
15
+ __type: import("@amazon/vinyl-util").Invariant<T>;
16
+ private readonly validator;
17
+ private _options;
18
+ get options(): {
19
+ readonly optional: false;
20
+ };
21
+ /**
22
+ * Constructs the first link in a schema chain.
23
+ *
24
+ * @param currentValidator
25
+ */
26
+ constructor(currentValidator: Validator<any, any>);
27
+ /**
28
+ * Constructs linking to the given previous schema.
29
+ *
30
+ * @param currentValidator
31
+ * @param previousSchema
32
+ * @param operation
33
+ */
34
+ constructor(currentValidator: Validator<any, any>, previousSchema: SchemaBase<any>, operation: (validatorA: Validator<any>, validatorB: Validator<any, any>) => Validator<T>);
35
+ get description(): string;
36
+ readonly assert: (input: unknown, origin?: string, path?: readonly string[]) => asserts input is T;
37
+ readonly isValid: (input: unknown) => input is T;
38
+ readonly validate: (input: unknown, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
39
+ /**
40
+ * Clones this schema, transforming all validators with the given mutator.
41
+ *
42
+ * @param transform A transformer which takes an existing validator in the chain and returns
43
+ * a new one. Validators should be considered to be immutable; transform should not change the
44
+ * input but instead return a new validator.
45
+ */
46
+ protected clone(transform?: (validator: Validator<any>) => Validator<any>): any;
47
+ protected self(current: Validator<any, any>): any;
48
+ protected self(currentValidator: Validator<any, any>, previousSchema: SchemaBase<any> | undefined, operation: ((validatorA: Validator<any>, validatorB: Validator<any, any>) => Validator<T>) | undefined): any;
49
+ /**
50
+ * Constructs a new schema with a validator chaining the current validator with the
51
+ * provided next in an AND operation.
52
+ *
53
+ * @param nextValidator The validator to run after the current.
54
+ * @return Returns a new Schema instance the same class with a new validator chaining the
55
+ * current validator with the given next in an AND operation.
56
+ */
57
+ protected chain(nextValidator: Validator<any, any>): any;
58
+ optional(): PropertyValidatorWithOptions<this, T, {
59
+ readonly optional: true;
60
+ }>;
61
+ required(): PropertyValidatorWithOptions<this, T, {
62
+ readonly optional: false;
63
+ }>;
64
+ }
65
+ //# sourceMappingURL=SchemaBase.d.ts.map
@@ -0,0 +1,38 @@
1
+ import { SetElementType } from '@amazon/vinyl-util';
2
+ import { Validator } from './Validator';
3
+ import { ValueSchema } from './ValueSchema';
4
+ export declare const setValidators: {
5
+ /**
6
+ * Creates a validator that asserts that the input is a set.
7
+ */
8
+ readonly isSet: () => Validator<Set<unknown>>;
9
+ /**
10
+ * Creates a validator that asserts that every element passes the given validator.
11
+ *
12
+ * @param elementValidator
13
+ */
14
+ readonly withElements: <U>(elementValidator: Validator<U>) => Validator<Set<U>, ReadonlySet<unknown>>;
15
+ };
16
+ export declare class SetSchema<T extends ReadonlySet<unknown>> extends ValueSchema<T> {
17
+ private static _base;
18
+ /**
19
+ * The base SetSchema instance validating that the input is a Set.
20
+ */
21
+ static get base(): SetSchema<Set<unknown>>;
22
+ /**
23
+ * Creates a validator that asserts that every element passes the given validator.
24
+ *
25
+ * @param elementValidator
26
+ */
27
+ withElements<U>(elementValidator: Validator<U>): SetSchema<Set<SetElementType<T> & U>>;
28
+ /**
29
+ * Casts to a readonly set.
30
+ * This is a shallow cast, elements of the array will not be changed.
31
+ */
32
+ readonly(): SetSchema<ReadonlySet<SetElementType<T>>>;
33
+ /**
34
+ * Casts the parameter type.
35
+ */
36
+ cast<T extends ReadonlySet<unknown>>(): SetSchema<T>;
37
+ }
38
+ //# sourceMappingURL=SetSchema.d.ts.map
@@ -0,0 +1,68 @@
1
+ import { ValueSchema } from './ValueSchema';
2
+ import { Validator } from './Validator';
3
+ export declare const stringValidators: {
4
+ /**
5
+ * Creates a validator that asserts that the input is not empty.
6
+ */
7
+ readonly notEmpty: () => Validator<string, string>;
8
+ /**
9
+ * Creates a validator that asserts that the input string has at least the given minimum
10
+ * length.
11
+ *
12
+ * @param min The string must be at least this length.
13
+ */
14
+ readonly minLength: (min: number) => Validator<string, string>;
15
+ /**
16
+ * Creates a validator that asserts that the input string has at most the given maximum
17
+ * length.
18
+ *
19
+ * @param max The string must be at most this length.
20
+ */
21
+ readonly maxLength: (max: number) => Validator<string, string>;
22
+ /**
23
+ * Creates a validator that asserts that the input string has no whitespace characters.
24
+ * Whitespace characters include space, new line, carriage return, and tab.
25
+ */
26
+ readonly noWhitespace: () => Validator<string, string>;
27
+ /**
28
+ * Creates a validator that asserts that the input string matches the given regex.
29
+ * @param regex
30
+ */
31
+ readonly matches: (regex: RegExp) => Validator<string, string>;
32
+ };
33
+ export declare class StringSchema extends ValueSchema<string> {
34
+ private static _base;
35
+ /**
36
+ * The base SetSchema instance validating that the input is a Set.
37
+ */
38
+ static get base(): StringSchema;
39
+ /**
40
+ * Creates a validator that asserts that the input is not empty.
41
+ */
42
+ notEmpty(): StringSchema;
43
+ /**
44
+ * Creates a validator that asserts that the input string has at least the given minimum
45
+ * length.
46
+ *
47
+ * @param min The string must be at least this length.
48
+ */
49
+ minLength(min: number): StringSchema;
50
+ /**
51
+ * Creates a validator that asserts that the input string has at most the given maximum
52
+ * length.
53
+ *
54
+ * @param max The string must be at most this length.
55
+ */
56
+ maxLength(max: number): StringSchema;
57
+ /**
58
+ * Creates a validator that asserts that the input string has no whitespace characters.
59
+ * Whitespace characters include space, new line, carriage return, and tab.
60
+ */
61
+ noWhitespace(): StringSchema;
62
+ /**
63
+ * Creates a validator that asserts that the input string matches the given regex.
64
+ * @param regex
65
+ */
66
+ matches(regex: RegExp): StringSchema;
67
+ }
68
+ //# sourceMappingURL=StringSchema.d.ts.map
@@ -0,0 +1,88 @@
1
+ import { Invariant } from '@amazon/vinyl-util';
2
+ /**
3
+ * Represents a validation failure.
4
+ */
5
+ export interface ValidationErrorMessage {
6
+ readonly message: string;
7
+ readonly path: readonly string[];
8
+ }
9
+ export interface ValidationOptions {
10
+ /**
11
+ * If true, collect all validation errors.
12
+ * Default: false
13
+ */
14
+ readonly all?: boolean;
15
+ }
16
+ /**
17
+ * A Validator can assert that an input matches this validator's schema.
18
+ */
19
+ export interface Validator<Output extends Input, Input = unknown> {
20
+ /**
21
+ * Enforces T is in an invariant position.
22
+ * This prevents a validator with more strict rules is assigned to a less strict type, which
23
+ * can cause unexpected validation errors.
24
+ */
25
+ __type?: Invariant<Output>;
26
+ /**
27
+ * A short description of the validator.
28
+ */
29
+ readonly description: string;
30
+ /**
31
+ * Returns true if the input is valid according to this validator.
32
+ *
33
+ * @param input
34
+ */
35
+ readonly isValid: (input: Input) => input is Output;
36
+ /**
37
+ * Validates the provided input, returning a list of error messages, or an empty array if the
38
+ * input passes this validator.
39
+ *
40
+ * @param input The input to validate.
41
+ * @param options Validation options.
42
+ * @param path The current field path to this validator.
43
+ * @return Returns an array of validation failures. If `options.all` is not true, only the
44
+ * first error will be returned.
45
+ */
46
+ readonly validate: (input: Input, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
47
+ /**
48
+ * Validates the given input, asserting that the input is type `T`.
49
+ * If the input is not valid, a {@link ValidationError} will be thrown.
50
+ *
51
+ * @param input The input to validate.
52
+ * @param origin The error origin. `ErrorOrigin.API` by default.
53
+ * @param path The current field path to this validator.
54
+ * @return Returns the input, cast as the validated type T
55
+ */
56
+ readonly assert: (input: Input, origin?: string, path?: readonly string[]) => asserts input is Output;
57
+ }
58
+ /**
59
+ * Creates a message string for a ValidationErrorMessage based on the input and its expectation.
60
+ *
61
+ * @param expectationMessage
62
+ * @param input
63
+ * @param path
64
+ * @param inputStringify
65
+ */
66
+ export declare function createValidationExpectationMessage<Input>(expectationMessage: string, input: Input, path: readonly string[], inputStringify?: (input: Input) => string): string;
67
+ /**
68
+ * Creates a new basic validator.
69
+ *
70
+ * For a deep validator which has sub-fields, use {@link createDeepValidator}.
71
+ *
72
+ * @param expectationMessage The message to use to describe what is expected. This will be used in
73
+ * the error message template.
74
+ * @param isValid Returns true if the given input is valid.
75
+ * @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
76
+ * expectation template. Default is `stringifyForPrint` with a 500 character max length.
77
+ */
78
+ export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string): Validator<Output, Input>;
79
+ /**
80
+ * Creates a new deep validator.
81
+ *
82
+ * For a shallow validator which validates one value, use {@link createValidator}.
83
+ *
84
+ * @param description Describes the validator.
85
+ * @param validate Returns an array of validation failures.
86
+ */
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>;
88
+ //# sourceMappingURL=Validator.d.ts.map
@@ -0,0 +1,26 @@
1
+ import { PartialDeep } from '@amazon/vinyl-util';
2
+ import { Validator } from './Validator';
3
+ export interface ValidatorWithPartial<Output extends Input, Input> extends Validator<Output, Input> {
4
+ partial(): ValidatorWithPartial<Partial<Output> & Input, Input>;
5
+ partialDeep(): ValidatorWithPartial<PartialDeep<Output> & Input, Input>;
6
+ }
7
+ /**
8
+ * Returns true if the given validator is an object validator.
9
+ * @param validator
10
+ */
11
+ export declare function isValidatorWithPartial<Output extends Input, Input>(validator: Validator<Output, Input>): validator is ValidatorWithPartial<Output, Input>;
12
+ /**
13
+ * If the given validator is an object validator, it will be cloned to a deep partial validator,
14
+ * otherwise returns the validator as is.
15
+ *
16
+ * @param validator
17
+ */
18
+ export declare function maybeToDeepPartial(validator: Validator<any>): Validator<any>;
19
+ /**
20
+ * If the given validator is an object validator, it will be cloned to a partial validator,
21
+ * otherwise returns the validator as is.
22
+ *
23
+ * @param validator
24
+ */
25
+ export declare function maybeToPartial(validator: Validator<any>): Validator<any>;
26
+ //# sourceMappingURL=ValidatorWithPartial.d.ts.map
@@ -0,0 +1,52 @@
1
+ import { SchemaBase } from './SchemaBase';
2
+ import { Validator } from './Validator';
3
+ export declare const valueValidators: {
4
+ readonly any: () => Validator<any>;
5
+ /**
6
+ * Creates a validator that asserts that the input is nullish.
7
+ */
8
+ readonly nullish: () => Validator<null | undefined>;
9
+ /**
10
+ * Creates a validator that asserts that the input is strictly null.
11
+ */
12
+ readonly null: () => Validator<null>;
13
+ /**
14
+ * Creates a validator that asserts that the input is strictly undefined.
15
+ */
16
+ readonly undefined: () => Validator<undefined>;
17
+ };
18
+ export declare class ValueSchema<T> extends SchemaBase<T> {
19
+ /**
20
+ * Prepends this validator allowing for null or undefined.
21
+ */
22
+ maybe(): ValueSchema<T | undefined | null>;
23
+ /**
24
+ * Prepends this validator allowing for null.
25
+ */
26
+ orNull(): ValueSchema<T | null>;
27
+ /**
28
+ * Prepends this validator allowing for undefined.
29
+ */
30
+ orUndefined(): ValueSchema<T | undefined>;
31
+ /**
32
+ * Returns a validator that asserts if this validator and the next passes.
33
+ *
34
+ * @see andValidators
35
+ *
36
+ * @param nextValidator The validator to run after the current.
37
+ * @return Returns a new ValueSchema intersecting this validator and the next.
38
+ */
39
+ and<U>(nextValidator: Validator<U>): ValueSchema<T & U>;
40
+ /**
41
+ * Returns a validator that asserts if this validator or the next passes.
42
+ *
43
+ * Implementation note:
44
+ * subclasses should not change the schema instance returned unless all chainable validators
45
+ * accept input type 'unknown'.
46
+ *
47
+ * @param nextValidator The validator to run after the current.
48
+ * @return Returns a new ValueSchema providing the union of this validator and the next.
49
+ */
50
+ or<U>(nextValidator: Validator<U, any>): ValueSchema<T | U>;
51
+ }
52
+ //# sourceMappingURL=ValueSchema.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { Validator } from './Validator';
2
+ /**
3
+ * Validates that all the given validators pass.
4
+ */
5
+ export declare function andValidators<A extends Input, B, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>): Validator<A & B, Input>;
6
+ /**
7
+ * Validates that all the given validators pass.
8
+ */
9
+ export declare function andValidators<A extends Input, B, C, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>): Validator<A & B & C, Input>;
10
+ /**
11
+ * Validates that all the given validators pass.
12
+ */
13
+ export declare function andValidators<A extends Input, B, C, D, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>): Validator<A & B & C & D, any>;
14
+ /**
15
+ * Validates that all the given validators pass.
16
+ */
17
+ export declare function andValidators<A extends Input, B, C, D, E, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>, validatorE: Validator<E, any>): Validator<A & B & C & D & E, any>;
18
+ export declare function andValidators(...validators: Validator<any, any>[]): Validator<any, any>;
19
+ //# sourceMappingURL=andValidators.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { Validator } from './Validator';
2
+ /**
3
+ * Validates that one of the given validators pass.
4
+ */
5
+ export declare function orValidators<A, B>(validatorA: Validator<A, any>, validatorB: Validator<B, any>): Validator<A | B, any>;
6
+ /**
7
+ * Validates that one of the given validators pass.
8
+ */
9
+ export declare function orValidators<A, B, C>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>): Validator<A | B | C, any>;
10
+ /**
11
+ * Validates that one of the given validators pass.
12
+ */
13
+ export declare function orValidators<A, B, C, D>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>): Validator<A | B | C | D, any>;
14
+ /**
15
+ * Validates that one of the given validators pass.
16
+ */
17
+ export declare function orValidators<A, B, C, D, E>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>, validatorE: Validator<E, any>): Validator<A | B | C | D | E, any>;
18
+ export declare function orValidators(...validators: Validator<any, any>[]): Validator<any, any>;
19
+ //# sourceMappingURL=orValidators.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { Validator } from './Validator';
2
+ /**
3
+ * Validators asserting typeof input matches the expected value.
4
+ *
5
+ * Note: bigint is omitted; not all supported platforms can use BigInt.
6
+ */
7
+ export declare const typeOfValidators: {
8
+ readonly boolean: Validator<boolean, unknown>;
9
+ readonly func: Validator<import("@amazon/vinyl-util").Fun, unknown>;
10
+ readonly number: Validator<number, unknown>;
11
+ readonly object: Validator<object, unknown>;
12
+ readonly string: Validator<string, unknown>;
13
+ readonly symbol: Validator<symbol, unknown>;
14
+ };
15
+ //# sourceMappingURL=typeOfValidators.d.ts.map
@@ -0,0 +1,127 @@
1
+ import { AnyRecord, Fun, Primitive, ReadonlyRecord } from '@amazon/vinyl-util';
2
+ import { ArraySchema } from './ArraySchema';
3
+ import { FunctionSchema } from './FunctionSchema';
4
+ import { SetSchema } from './SetSchema';
5
+ import { ValueSchema } from './ValueSchema';
6
+ import { NumberSchema } from './NumberSchema';
7
+ import { ExtendedPropertyValidators, ObjectSchema } from './ObjectSchema';
8
+ import { RecordSchema } from './RecordSchema';
9
+ import { StringSchema } from './StringSchema';
10
+ import { Validator } from './Validator';
11
+ /**
12
+ * Creates a no-op validator. The value can be any type.
13
+ */
14
+ export declare function any(): ValueSchema<any>;
15
+ /**
16
+ * Creates a validator that asserts that the input is an array.
17
+ */
18
+ export declare function array(): ArraySchema<unknown[]>;
19
+ /**
20
+ * Creates a validator that asserts that the input is an array and every element passes the
21
+ * given validator.
22
+ *
23
+ * @param elementValidator
24
+ */
25
+ export declare function array<U>(elementValidator: Validator<U>): ArraySchema<U[]>;
26
+ /**
27
+ * Creates a validator that asserts that the input is a boolean.
28
+ */
29
+ export declare function boolean(): ValueSchema<boolean>;
30
+ /**
31
+ * Creates a validator that asserts that the input is a function.
32
+ */
33
+ export declare function func(): FunctionSchema<Fun>;
34
+ /**
35
+ * Validates that the input is an instanceof the given prototype.
36
+ * @param clazz
37
+ */
38
+ export declare function instanceOf<T>(clazz: new (...args: any[]) => T): ValueSchema<T>;
39
+ /**
40
+ * Creates a validator that asserts that the input is nullish or one of the given possible values.
41
+ *
42
+ * @param possibleValues
43
+ */
44
+ export declare function isOneOf<U extends readonly Primitive[]>(...possibleValues: U): ValueSchema<U[number]>;
45
+ /**
46
+ * Creates a validator that asserts that the input is one of the values of the given enum.
47
+ *
48
+ * @param enumObject The enum to validate against.
49
+ */
50
+ export declare function enumOf<T extends Record<string, Primitive>>(enumObject: T): ValueSchema<T[keyof T]>;
51
+ /**
52
+ * Creates a validator that asserts that the input is null.
53
+ */
54
+ export declare function exactlyNull(): Validator<null>;
55
+ /**
56
+ * Creates a validator that asserts that the input is nullish.
57
+ */
58
+ export declare function nullish(): Validator<null | undefined>;
59
+ /**
60
+ * Creates a validator that asserts that the input is a number.
61
+ */
62
+ export declare function number(): NumberSchema;
63
+ /**
64
+ * Creates a validator that validates the input has properties that pass their respective
65
+ * validators.
66
+ */
67
+ export declare function object<U extends object>(propertyValidators: ExtendedPropertyValidators<AnyRecord, U>): ObjectSchema<U>;
68
+ /**
69
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
70
+ * values that match their respective validators.
71
+ *
72
+ * Prototype members are not included.
73
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
74
+ * to the key validator as '0'.
75
+ *
76
+ * To assume string keys, use {@link recordValues}
77
+ *
78
+ * @param keyValidator Validates each key.
79
+ * @param valueValidator Validates each value.
80
+ */
81
+ export declare function record<K extends string | symbol, V>(keyValidator: Validator<K>, valueValidator: Validator<V>): RecordSchema<ReadonlyRecord<K, V>>;
82
+ /**
83
+ * Returns a validator that asserts that the input object's own enumerable properties has
84
+ * string keys and values that match the given validator.
85
+ *
86
+ * Prototype members are not included.
87
+ *
88
+ * @param valueValidator Validates each value.
89
+ */
90
+ export declare function recordValues<V>(valueValidator: Validator<V>): RecordSchema<Record<string, V>>;
91
+ /**
92
+ * Returns a validator that asserts the input is a set with elements that match the given
93
+ * element validator.
94
+ * @param elementValidator
95
+ */
96
+ export declare function set<U>(elementValidator: Validator<U>): SetSchema<Set<U>>;
97
+ /**
98
+ * Creates a validator that asserts that the input is type 'string'.
99
+ */
100
+ export declare function string(): StringSchema;
101
+ /**
102
+ * Creates a validator that asserts that the input array is a tuple whose elements match
103
+ * the given validators.
104
+ *
105
+ * @param validators
106
+ */
107
+ export declare function tuple<U extends readonly any[]>(...validators: {
108
+ [Index in keyof U]: Validator<U[Index]>;
109
+ }): ArraySchema<{
110
+ [Index in keyof U]: U[Index];
111
+ }>;
112
+ /**
113
+ * Creates a validator that asserts that the input is type 'symbol'.
114
+ */
115
+ export declare function symbol(): ValueSchema<symbol>;
116
+ /**
117
+ * Creates a validator that asserts that the input is strictly undefined.
118
+ */
119
+ export declare function exactlyUndefined(): ValueSchema<undefined>;
120
+ /**
121
+ * Returns a custom
122
+ * @param expectationMessage
123
+ * @param isValid
124
+ * @param inputStringify
125
+ */
126
+ export declare function custom<T>(expectationMessage: string, isValid: (input: unknown) => boolean, inputStringify?: (input: unknown) => string): ValueSchema<T>;
127
+ //# sourceMappingURL=validators.d.ts.map
@@ -0,0 +1,18 @@
1
+ /** Auto-generated from build scripts */
2
+ export * from './validation/validators';
3
+ export * from './validation/typeOfValidators';
4
+ export * from './validation/orValidators';
5
+ 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
+ export * from './validation/ArraySchema';
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,86 @@
1
+ import type { ElementType } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import type { Validator } from './Validator';
4
+ export declare const arrayValidators: {
5
+ /**
6
+ * Creates a validator that asserts that the input is an array.
7
+ *
8
+ * Note: arrays can only be readonly by type system, there is no native readonly
9
+ * js array. This validator assumes mutability, use the ArraySchema readonly() method
10
+ * to cast to a readonly array.
11
+ */
12
+ readonly isArray: () => Validator<unknown[]>;
13
+ /**
14
+ * Creates a validator that asserts that every element passes the given validator.
15
+ *
16
+ * @param elementValidator
17
+ */
18
+ readonly withElements: <U>(elementValidator: Validator<U>) => Validator<U[], readonly unknown[]>;
19
+ /**
20
+ * Creates a validator that asserts that the input array is a tuple whose elements match
21
+ * the given validators.
22
+ *
23
+ * @param validators
24
+ */
25
+ readonly tuple: <U extends readonly any[]>(...validators: {
26
+ [Index in keyof U]: Validator<U[Index]>;
27
+ }) => Validator<{
28
+ [Index in keyof U]: U[Index];
29
+ }, readonly unknown[]>;
30
+ /**
31
+ * Creates a validator that validates the input array has the given minimum length.
32
+ * @param min The minimum number of elements (inclusive)
33
+ */
34
+ readonly minLength: (min: number) => Validator<readonly unknown[], readonly unknown[]>;
35
+ /**
36
+ *
37
+ * Creates a validator that validates the input array has the given maximum length.
38
+ * @param max The maximum number of elements (inclusive)
39
+ */
40
+ readonly maxLength: (max: number) => Validator<readonly unknown[], readonly unknown[]>;
41
+ };
42
+ export declare class ArraySchema<T extends readonly unknown[]> extends ValueSchema<T> {
43
+ private static _base;
44
+ /**
45
+ * The base ArraySchema instance validating that the input is an Array.
46
+ */
47
+ static get base(): ArraySchema<unknown[]>;
48
+ /**
49
+ * Creates a validator that asserts that every element passes the given validator.
50
+ *
51
+ * @param elementValidator
52
+ */
53
+ withElements<U>(elementValidator: Validator<U>): ArraySchema<(ElementType<T> & U)[]>;
54
+ /**
55
+ * Creates a validator that asserts that the input array is a tuple whose elements match
56
+ * the given validators.
57
+ *
58
+ * @param validators
59
+ */
60
+ tuple<U extends readonly any[]>(...validators: {
61
+ [Index in keyof U]: Validator<U[Index]>;
62
+ }): ArraySchema<{
63
+ [Index in keyof U]: U[Index];
64
+ }>;
65
+ /**
66
+ * Creates a validator that validates the input array has the given minimum length.
67
+ * @param min The minimum number of elements (inclusive)
68
+ */
69
+ minLength(min: number): ArraySchema<T>;
70
+ /**
71
+ *
72
+ * Creates a validator that validates the input array has the given maximum length.
73
+ * @param max The maximum number of elements (inclusive)
74
+ */
75
+ maxLength(max: number): ArraySchema<T>;
76
+ /**
77
+ * Casts to a readonly array.
78
+ * This is a shallow cast, elements of the array will not be changed.
79
+ */
80
+ readonly(): ArraySchema<Readonly<T>>;
81
+ /**
82
+ * Casts the type parameter.
83
+ */
84
+ cast<T extends readonly unknown[]>(): ArraySchema<T>;
85
+ }
86
+ //# sourceMappingURL=ArraySchema.d.ts.map
@@ -0,0 +1,21 @@
1
+ import type { Fun } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import type { Validator } from './Validator';
4
+ export declare const functionValidators: {
5
+ readonly length: (max: number) => Validator<Fun, Fun>;
6
+ };
7
+ export declare class FunctionSchema<T extends Fun> extends ValueSchema<T> {
8
+ private static _base;
9
+ /**
10
+ * The base Function validator validating that the input is a function.
11
+ */
12
+ static get base(): FunctionSchema<Fun>;
13
+ length(max: number): FunctionSchema<T>;
14
+ /**
15
+ * Casts the function signature.
16
+ * Function signatures, except for their required number of parameters, cannot be
17
+ * meaningfully validated.
18
+ */
19
+ cast<T extends Fun>(): FunctionSchema<T>;
20
+ }
21
+ //# sourceMappingURL=FunctionSchema.d.ts.map