@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,127 @@
1
+ import { type AnyRecord, type Fun, type Primitive, type 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 { type ExtendedPropertyValidators, ObjectSchema } from './ObjectSchema';
8
+ import { RecordSchema } from './RecordSchema';
9
+ import { StringSchema } from './StringSchema';
10
+ import type { 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<const 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<const 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<const 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<const U extends object>(propertyValidators: NoInfer<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<const K extends string | symbol, const 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<const 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<const 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 @@
1
+ {"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../../../src/validation/validators.ts"],"names":[],"mappings":"AAKA,OAAO,EACH,KAAK,SAAS,EACd,KAAK,GAAG,EACR,KAAK,SAAS,EACd,KAAK,cAAc,EAGtB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C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1C,GAAG,UAAU,EAAE;KACV,KAAK,IAAI,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;CAC1C,GACF,WAAW,CAAC;KACV,KAAK,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;CAC/B,CAAC,CAED;AAED;;GAEG;AACH,wBAAgB,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,CAE5C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,WAAW,CAAC,SAAS,CAAC,CAEzD;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACpB,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,EACpC,cAAc,GAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MACH,GAClC,WAAW,CAAC,CAAC,CAAC,CAIhB"}
@@ -0,0 +1,168 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { stringifyForPrint, substitute, } from '@amazon/vinyl-util';
6
+ import { ArraySchema } from './ArraySchema';
7
+ import { FunctionSchema } from './FunctionSchema';
8
+ import { SetSchema } from './SetSchema';
9
+ import { ValueSchema, valueValidators } from './ValueSchema';
10
+ import { NumberSchema } from './NumberSchema';
11
+ import { ObjectSchema } from './ObjectSchema';
12
+ import { RecordSchema } from './RecordSchema';
13
+ import { StringSchema } from './StringSchema';
14
+ import { typeOfValidators } from './typeOfValidators';
15
+ import { createValidator } from './Validator';
16
+ /**
17
+ * @private
18
+ */
19
+ const locale = {
20
+ isOneOf: 'one of: {value}',
21
+ instanceOf: 'instance of {value}',
22
+ oneOfSeparator: ' | ',
23
+ };
24
+ /**
25
+ * Creates a no-op validator. The value can be any type.
26
+ */
27
+ export function any() {
28
+ return new ValueSchema(valueValidators.any());
29
+ }
30
+ export function array(elementValidator) {
31
+ if (!elementValidator)
32
+ return ArraySchema.base;
33
+ else
34
+ return ArraySchema.base.withElements(elementValidator);
35
+ }
36
+ /**
37
+ * Creates a validator that asserts that the input is a boolean.
38
+ */
39
+ export function boolean() {
40
+ return new ValueSchema(typeOfValidators.boolean);
41
+ }
42
+ /**
43
+ * Creates a validator that asserts that the input is a function.
44
+ */
45
+ export function func() {
46
+ return FunctionSchema.base;
47
+ }
48
+ /**
49
+ * Validates that the input is an instanceof the given prototype.
50
+ * @param clazz
51
+ */
52
+ export function instanceOf(clazz) {
53
+ return new ValueSchema(createValidator(substitute(locale.instanceOf, { value: clazz.name }), (input) => input instanceof clazz));
54
+ }
55
+ /**
56
+ * Creates a validator that asserts that the input is nullish or one of the given possible values.
57
+ *
58
+ * @param possibleValues
59
+ */
60
+ export function isOneOf(...possibleValues) {
61
+ return new ValueSchema(createValidator(substitute(locale.isOneOf, {
62
+ value: possibleValues.join(locale.oneOfSeparator),
63
+ }), (input) => possibleValues.includes(input)));
64
+ }
65
+ /**
66
+ * Creates a validator that asserts that the input is one of the values of the given enum.
67
+ *
68
+ * @param enumObject The enum to validate against.
69
+ */
70
+ export function enumOf(enumObject) {
71
+ return isOneOf(...Object.values(enumObject));
72
+ }
73
+ /**
74
+ * Creates a validator that asserts that the input is null.
75
+ */
76
+ export function exactlyNull() {
77
+ return valueValidators.null();
78
+ }
79
+ /**
80
+ * Creates a validator that asserts that the input is nullish.
81
+ */
82
+ export function nullish() {
83
+ return valueValidators.nullish();
84
+ }
85
+ /**
86
+ * Creates a validator that asserts that the input is a number.
87
+ */
88
+ export function number() {
89
+ return NumberSchema.base;
90
+ }
91
+ /**
92
+ * Creates a validator that validates the input has properties that pass their respective
93
+ * validators.
94
+ */
95
+ export function object(propertyValidators) {
96
+ return ObjectSchema.base.extend(propertyValidators);
97
+ }
98
+ /**
99
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
100
+ * values that match their respective validators.
101
+ *
102
+ * Prototype members are not included.
103
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
104
+ * to the key validator as '0'.
105
+ *
106
+ * To assume string keys, use {@link recordValues}
107
+ *
108
+ * @param keyValidator Validates each key.
109
+ * @param valueValidator Validates each value.
110
+ */
111
+ export function record(keyValidator, valueValidator) {
112
+ return RecordSchema.base.record(keyValidator, valueValidator);
113
+ }
114
+ /**
115
+ * Returns a validator that asserts that the input object's own enumerable properties has
116
+ * string keys and values that match the given validator.
117
+ *
118
+ * Prototype members are not included.
119
+ *
120
+ * @param valueValidator Validates each value.
121
+ */
122
+ export function recordValues(valueValidator) {
123
+ return RecordSchema.base.recordValues(valueValidator);
124
+ }
125
+ /**
126
+ * Returns a validator that asserts the input is a set with elements that match the given
127
+ * element validator.
128
+ * @param elementValidator
129
+ */
130
+ export function set(elementValidator) {
131
+ return SetSchema.base.withElements(elementValidator);
132
+ }
133
+ /**
134
+ * Creates a validator that asserts that the input is type 'string'.
135
+ */
136
+ export function string() {
137
+ return StringSchema.base;
138
+ }
139
+ /**
140
+ * Creates a validator that asserts that the input array is a tuple whose elements match
141
+ * the given validators.
142
+ *
143
+ * @param validators
144
+ */
145
+ export function tuple(...validators) {
146
+ return ArraySchema.base.tuple(...validators);
147
+ }
148
+ /**
149
+ * Creates a validator that asserts that the input is type 'symbol'.
150
+ */
151
+ export function symbol() {
152
+ return new ValueSchema(typeOfValidators.symbol);
153
+ }
154
+ /**
155
+ * Creates a validator that asserts that the input is strictly undefined.
156
+ */
157
+ export function exactlyUndefined() {
158
+ return new ValueSchema(valueValidators.undefined());
159
+ }
160
+ /**
161
+ * Returns a custom
162
+ * @param expectationMessage
163
+ * @param isValid
164
+ * @param inputStringify
165
+ */
166
+ export function custom(expectationMessage, isValid, inputStringify = (input) => stringifyForPrint(input, 500)) {
167
+ return new ValueSchema(createValidator(expectationMessage, isValid, inputStringify));
168
+ }
@@ -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 { ElementType } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import { 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 { Fun } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import { 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
@@ -0,0 +1,100 @@
1
+ import { ValueSchema } from './ValueSchema';
2
+ import { 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 { AnyRecord, IsPropertyOptional, PartialDeep } from '@amazon/vinyl-util';
2
+ import { ValidatorWithPartial } from './ValidatorWithPartial';
3
+ import { ValueSchema } from './ValueSchema';
4
+ import { PropertyValidator } from '../validation/PropertyValidator';
5
+ import { 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 { Validator } from '../validation/Validator';
2
+ import { 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 { ReadonlyRecord } from '@amazon/vinyl-util';
2
+ import { ValueSchema } from './ValueSchema';
3
+ import { 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