@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,100 @@
1
+ import { ValueSchema } from './ValueSchema';
2
+ import type { Validator } from './Validator';
3
+ export declare const numberValidators: {
4
+ /**
5
+ * Creates a validator that asserts that the input number is at least the provided minimum
6
+ * value.
7
+ *
8
+ * @param min The minimum (inclusive) the value may be.
9
+ */
10
+ readonly gte: (min: number) => Validator<number, number>;
11
+ /**
12
+ * Creates a validator that asserts that the input number is greater than the provided minimum
13
+ * value.
14
+ *
15
+ * @param min The minimum (exclusive) the value may be.
16
+ */
17
+ readonly gt: (min: number) => Validator<number, number>;
18
+ /**
19
+ * Creates a validator that asserts that the input number is at most the provided maximum
20
+ * value.
21
+ *
22
+ * @param max The maximum (inclusive) the value may be.
23
+ */
24
+ readonly lte: (max: number) => Validator<number, number>;
25
+ /**
26
+ * Creates a validator that asserts that the input number is less than the provided maximum
27
+ * value.
28
+ *
29
+ * @param max The maximum (exclusive) the value may be.
30
+ */
31
+ readonly lt: (max: number) => Validator<number, number>;
32
+ /**
33
+ * Creates a validator that asserts that the input number is at least the provided minimum
34
+ * and at most the provided maximum values.
35
+ *
36
+ * @param min The minimum (inclusive) value the input may be.
37
+ * @param max The maximum (inclusive) value the input may be.
38
+ */
39
+ readonly within: (min: number, max: number) => Validator<number, number>;
40
+ /**
41
+ * Creates a validator that asserts that the input number is finite.
42
+ */
43
+ readonly finite: () => Validator<number, number>;
44
+ /**
45
+ * Creates a validator that asserts that the input number is a safe integer.
46
+ */
47
+ readonly safeInteger: () => Validator<number, number>;
48
+ };
49
+ export declare class NumberSchema extends ValueSchema<number> {
50
+ private static _base;
51
+ /**
52
+ * The base Object validator validating that the input is an object.
53
+ */
54
+ static get base(): NumberSchema;
55
+ /**
56
+ * Creates a validator that asserts that the input number is at least the provided minimum
57
+ * value.
58
+ *
59
+ * @param min The minimum (inclusive) the value may be.
60
+ */
61
+ gte(min: number): this;
62
+ /**
63
+ * Creates a validator that asserts that the input number is greater than the provided minimum
64
+ * value.
65
+ *
66
+ * @param min The minimum (exclusive) the value may be.
67
+ */
68
+ gt(min: number): this;
69
+ /**
70
+ * Creates a validator that asserts that the input number is at most the provided maximum
71
+ * value.
72
+ *
73
+ * @param max The maximum (inclusive) the value may be.
74
+ */
75
+ lte(max: number): this;
76
+ /**
77
+ * Creates a validator that asserts that the input number is less than the provided maximum
78
+ * value.
79
+ *
80
+ * @param max The maximum (exclusive) the value may be.
81
+ */
82
+ lt(max: number): this;
83
+ /**
84
+ * Creates a validator that asserts that the input number is at least the provided minimum
85
+ * and at most the provided maximum values.
86
+ *
87
+ * @param min The minimum (inclusive) value the input may be.
88
+ * @param max The maximum (inclusive) value the input may be.
89
+ */
90
+ within(min: number, max: number): this;
91
+ /**
92
+ * Creates a validator that asserts that the input number is finite.
93
+ */
94
+ finite(): this;
95
+ /**
96
+ * Creates a validator that asserts that the input number is a safe integer.
97
+ */
98
+ safeInteger(): this;
99
+ }
100
+ //# sourceMappingURL=NumberSchema.d.ts.map
@@ -0,0 +1,56 @@
1
+ import type { AnyRecord, IsPropertyOptional, PartialDeep } from '@amazon/vinyl-util';
2
+ import type { ValidatorWithPartial } from './ValidatorWithPartial';
3
+ import { ValueSchema } from './ValueSchema';
4
+ import type { PropertyValidator } from '../validation/PropertyValidator';
5
+ import type { Equal } from '@amazon/vinyl-util/browserTestUtil';
6
+ export declare const objectValidators: {
7
+ /**
8
+ * Creates a validator that validates the input has properties that pass their respective
9
+ * validators.
10
+ *
11
+ * @param propertyValidators
12
+ */
13
+ readonly properties: <U extends object>(propertyValidators: {
14
+ readonly [P in keyof U]-?: PropertyValidator<Required<U>[P], {
15
+ optional: IsPropertyOptional<U, P>;
16
+ }>;
17
+ }) => ValidatorWithPartial<U, object>;
18
+ };
19
+ /**
20
+ * Chainable object validators.
21
+ */
22
+ export declare class ObjectSchema<T extends object> extends ValueSchema<T> implements ValidatorWithPartial<T, unknown> {
23
+ private static _base;
24
+ /**
25
+ * The base Object validator validating that the input is an object.
26
+ */
27
+ static get base(): ObjectSchema<AnyRecord>;
28
+ /**
29
+ * Creates a validator that validates the input has properties that pass their respective
30
+ * validators.
31
+ *
32
+ * @param propertyValidators
33
+ */
34
+ extend<U extends object>(propertyValidators: ExtendedPropertyValidators<T, U>): ObjectSchema<U>;
35
+ /**
36
+ * Allows all properties to be undefined.
37
+ */
38
+ partial(): ObjectSchema<Partial<T>>;
39
+ /**
40
+ * Allows all recursive properties to be undefined.
41
+ */
42
+ partialDeep(): ObjectSchema<PartialDeep<T>>;
43
+ /**
44
+ * Casts the type parameter.
45
+ */
46
+ cast<T extends object>(): ObjectSchema<T>;
47
+ }
48
+ export type ExtendedPropertyValidators<T extends object, U extends object> = {
49
+ [K in keyof U as K extends keyof T ? Equal<U[K], T[K]> extends true ? never : K : K]-?: ExtendedPropertyValidator<U & T, K>;
50
+ } & {
51
+ [K in keyof U as K extends keyof T ? Equal<U[K], T[K]> extends true ? K : never : never]?: ExtendedPropertyValidator<U & T, K>;
52
+ };
53
+ export type ExtendedPropertyValidator<U extends object, P extends keyof U> = PropertyValidator<Required<U>[P], {
54
+ optional: IsPropertyOptional<U, P>;
55
+ }>;
56
+ //# sourceMappingURL=ObjectSchema.d.ts.map
@@ -0,0 +1,23 @@
1
+ import type { Validator } from '../validation/Validator';
2
+ import type { Merge } from '@amazon/vinyl-util';
3
+ export interface PropertyValidatorOptions {
4
+ /**
5
+ * If true, when the property is omitted, the object schema should not fail validation.
6
+ */
7
+ readonly optional: boolean;
8
+ }
9
+ /**
10
+ * A utility type for overriding the options type on a validator.
11
+ * This will strip the types of the overridden options and set to the new types.
12
+ */
13
+ export type PropertyValidatorWithOptions<ValidatorType extends PropertyValidator<any, any>, T, Options extends PropertyValidatorOptions> = Omit<ValidatorType, keyof PropertyValidator<any, any>> & PropertyValidator<T, Merge<ValidatorType['options'], Options>>;
14
+ export interface PropertyValidator<T, PropertyOptionsType extends PropertyValidatorOptions> extends Validator<T> {
15
+ readonly options: PropertyOptionsType;
16
+ optional(): PropertyValidatorWithOptions<this, T, {
17
+ readonly optional: true;
18
+ }>;
19
+ required(): PropertyValidatorWithOptions<this, T, {
20
+ readonly optional: false;
21
+ }>;
22
+ }
23
+ //# sourceMappingURL=PropertyValidator.d.ts.map
@@ -0,0 +1,58 @@
1
+ import type { ReadonlyRecord } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import type { Validator } from './Validator';
4
+ export declare const recordValidators: {
5
+ /**
6
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
7
+ * values that match their respective validators.
8
+ *
9
+ * Prototype members are not included.
10
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
11
+ * to the key validator as '0'.
12
+ *
13
+ * @param keyValidator Validates each key.
14
+ * @param valueValidator Validates each value.
15
+ */
16
+ readonly record: <K extends string | symbol, V>(keyValidator: Validator<K>, valueValidator: Validator<V>) => Validator<Record<K, V>, object>;
17
+ };
18
+ /**
19
+ * Chainable object validators for the Record type.
20
+ */
21
+ export declare class RecordSchema<T extends ReadonlyRecord<keyof any, any>> extends ValueSchema<T> {
22
+ private static _base;
23
+ /**
24
+ * The base Object validator validating that the input is an object.
25
+ */
26
+ static get base(): RecordSchema<Record<keyof any, unknown>>;
27
+ private static get defaultKeyValidator();
28
+ /**
29
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
30
+ * values that match their respective validators.
31
+ *
32
+ * Prototype members are not included.
33
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
34
+ * to the key validator as '0'.
35
+ *
36
+ * @param keyValidator Validates each key.
37
+ * @param valueValidator Validates each value.
38
+ */
39
+ record<K extends string | symbol, V>(keyValidator: Validator<K>, valueValidator: Validator<V>): RecordSchema<ReadonlyRecord<K, V>>;
40
+ /**
41
+ * Returns a validator that asserts that the object's own enumerable properties has
42
+ * string keys and values that match the given validator.
43
+ *
44
+ * Prototype members are not included.
45
+ *
46
+ * @param valueValidator Validates each value.
47
+ */
48
+ recordValues<V>(valueValidator: Validator<V>): RecordSchema<Record<string, V>>;
49
+ /**
50
+ * Casts to a ReadonlyRecord
51
+ */
52
+ readonly(): RecordSchema<T extends Record<infer K, infer V> ? ReadonlyRecord<K, V> : never>;
53
+ /**
54
+ * Casts the type parameter.
55
+ */
56
+ cast<T extends ReadonlyRecord<keyof any, unknown>>(): RecordSchema<T>;
57
+ }
58
+ //# sourceMappingURL=RecordSchema.d.ts.map
@@ -0,0 +1,65 @@
1
+ import type { ValidationErrorMessage, ValidationOptions, Validator } from './Validator';
2
+ import type { 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 type { SetElementType } from '@amazon/vinyl-util';
2
+ import type { 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 type { 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 type { 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 type { PartialDeep } from '@amazon/vinyl-util';
2
+ import type { 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 type { 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 type { 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 type { 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 type { 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