@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,148 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { substitute } from '@amazon/vinyl-util';
6
+ import { ValueSchema } from './ValueSchema';
7
+ import { createDeepValidator, createValidator } from './Validator';
8
+ /**
9
+ * @private
10
+ */
11
+ const locale = {
12
+ array: 'array',
13
+ minLength: 'at least {value} elements',
14
+ maxLength: 'at most {value} elements',
15
+ exactlyLength: 'exactly {value} elements',
16
+ actualLength: 'length {value}',
17
+ withElements: '({value})[]',
18
+ tuple: '[{value}]',
19
+ };
20
+ export const arrayValidators = {
21
+ /**
22
+ * Creates a validator that asserts that the input is an array.
23
+ *
24
+ * Note: arrays can only be readonly by type system, there is no native readonly
25
+ * js array. This validator assumes mutability, use the ArraySchema readonly() method
26
+ * to cast to a readonly array.
27
+ */
28
+ isArray() {
29
+ return createValidator(locale.array, (input) => Array.isArray(input));
30
+ },
31
+ /**
32
+ * Creates a validator that asserts that every element passes the given validator.
33
+ *
34
+ * @param elementValidator
35
+ */
36
+ withElements(elementValidator) {
37
+ return createDeepValidator(substitute(locale.withElements, {
38
+ value: elementValidator.description,
39
+ }), (input, options, path) => {
40
+ const errors = [];
41
+ for (let i = 0; i < input.length; i++) {
42
+ const element = input[i];
43
+ errors.push(...elementValidator.validate(element, options, path.concat(i.toString())));
44
+ if (!options.all && errors.length)
45
+ return errors;
46
+ }
47
+ return errors;
48
+ });
49
+ },
50
+ /**
51
+ * Creates a validator that asserts that the input array is a tuple whose elements match
52
+ * the given validators.
53
+ *
54
+ * @param validators
55
+ */
56
+ tuple(...validators) {
57
+ const lengthValidator = createValidator(substitute(locale.exactlyLength, {
58
+ value: validators.length,
59
+ }), (length) => length === validators.length);
60
+ return createDeepValidator(substitute(locale.tuple, { value: validators.join(', ') }), (input, options, path) => {
61
+ const errors = [];
62
+ errors.push(...lengthValidator.validate(input.length, options, path));
63
+ if (!options.all && errors.length)
64
+ return errors;
65
+ const n = Math.min(validators.length, input.length);
66
+ for (let i = 0; i < n; i++) {
67
+ const element = input[i];
68
+ const validator = validators[i];
69
+ errors.push(...validator.validate(element, options, path.concat(i.toString())));
70
+ if (!options.all && errors.length)
71
+ return errors;
72
+ }
73
+ return errors;
74
+ });
75
+ },
76
+ /**
77
+ * Creates a validator that validates the input array has the given minimum length.
78
+ * @param min The minimum number of elements (inclusive)
79
+ */
80
+ minLength(min) {
81
+ return createValidator(substitute(locale.minLength, { value: min }), (input) => input.length >= min, (input) => substitute(locale.actualLength, { value: input.length }));
82
+ },
83
+ /**
84
+ *
85
+ * Creates a validator that validates the input array has the given maximum length.
86
+ * @param max The maximum number of elements (inclusive)
87
+ */
88
+ maxLength(max) {
89
+ return createValidator(substitute(locale.maxLength, { value: max }), (input) => input.length <= max, (input) => substitute(locale.actualLength, { value: input.length }));
90
+ },
91
+ };
92
+ export class ArraySchema extends ValueSchema {
93
+ static _base = null;
94
+ /**
95
+ * The base ArraySchema instance validating that the input is an Array.
96
+ */
97
+ static get base() {
98
+ if (this._base == null)
99
+ this._base = new ArraySchema(arrayValidators.isArray());
100
+ return this._base;
101
+ }
102
+ /**
103
+ * Creates a validator that asserts that every element passes the given validator.
104
+ *
105
+ * @param elementValidator
106
+ */
107
+ withElements(elementValidator) {
108
+ return this.chain(arrayValidators.withElements(elementValidator));
109
+ }
110
+ /**
111
+ * Creates a validator that asserts that the input array is a tuple whose elements match
112
+ * the given validators.
113
+ *
114
+ * @param validators
115
+ */
116
+ tuple(...validators) {
117
+ return this.chain(arrayValidators.tuple(...validators));
118
+ }
119
+ /**
120
+ * Creates a validator that validates the input array has the given minimum length.
121
+ * @param min The minimum number of elements (inclusive)
122
+ */
123
+ minLength(min) {
124
+ return this.chain(arrayValidators.minLength(min));
125
+ }
126
+ /**
127
+ *
128
+ * Creates a validator that validates the input array has the given maximum length.
129
+ * @param max The maximum number of elements (inclusive)
130
+ */
131
+ maxLength(max) {
132
+ return this.chain(arrayValidators.maxLength(max));
133
+ }
134
+ /**
135
+ * Casts to a readonly array.
136
+ * This is a shallow cast, elements of the array will not be changed.
137
+ */
138
+ readonly() {
139
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
140
+ return this;
141
+ }
142
+ /**
143
+ * Casts the type parameter.
144
+ */
145
+ cast() {
146
+ return this;
147
+ }
148
+ }
@@ -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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/FunctionSchema.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAA;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAY5C,eAAO,MAAM,kBAAkB;2BACf,MAAM,KAAG,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;CAOlC,CAAA;AAEV,qBAAa,cAAc,CAAC,CAAC,SAAS,GAAG,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IAC7D,OAAO,CAAC,MAAM,CAAC,KAAK,CAAmC;IAEvD;;OAEG;IACH,MAAM,KAAK,IAAI,IAAI,cAAc,CAAC,GAAG,CAAC,CAIrC;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC;IAItC;;;;OAIG;IACH,IAAI,CAAC,CAAC,SAAS,GAAG,KAAK,cAAc,CAAC,CAAC,CAAC;CAG3C"}
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { substitute } from '@amazon/vinyl-util';
6
+ import { ValueSchema } from './ValueSchema';
7
+ import { createValidator } from './Validator';
8
+ import { typeOfValidators } from '../validation/typeOfValidators';
9
+ /**
10
+ * @private
11
+ */
12
+ const locale = {
13
+ length: 'at most {value} arguments',
14
+ actualLength: '{value} arguments',
15
+ };
16
+ export const functionValidators = {
17
+ length(max) {
18
+ return createValidator(substitute(locale.length, { value: max }), (input) => input.length <= max, (input) => substitute(locale.actualLength, { value: input.length }));
19
+ },
20
+ };
21
+ export class FunctionSchema extends ValueSchema {
22
+ static _base = null;
23
+ /**
24
+ * The base Function validator validating that the input is a function.
25
+ */
26
+ static get base() {
27
+ if (this._base == null)
28
+ this._base = new FunctionSchema(typeOfValidators.func);
29
+ return this._base;
30
+ }
31
+ length(max) {
32
+ return this.chain(functionValidators.length(max));
33
+ }
34
+ /**
35
+ * Casts the function signature.
36
+ * Function signatures, except for their required number of parameters, cannot be
37
+ * meaningfully validated.
38
+ */
39
+ cast() {
40
+ return this;
41
+ }
42
+ }
@@ -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 @@
1
+ {"version":3,"file":"NumberSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/NumberSchema.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAa5C,eAAO,MAAM,gBAAgB;IACzB;;;;;OAKG;wBACM,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAO3C;;;;;OAKG;uBACK,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAO1C;;;;;OAKG;wBACM,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAO3C;;;;;OAKG;uBACK,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAO1C;;;;;;OAMG;2BACS,MAAM,OAAO,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAO3D;;OAEG;2BACO,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAInC;;OAEG;gCACY,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;CAKlC,CAAA;AAEV,qBAAa,YAAa,SAAQ,WAAW,CAAC,MAAM,CAAC;IACjD,OAAO,CAAC,MAAM,CAAC,KAAK,CAA4B;IAEhD;;OAEG;IACH,MAAM,KAAK,IAAI,IAAI,YAAY,CAI9B;IAED;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAItB;;;;;OAKG;IACH,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIrB;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAItB;;;;;OAKG;IACH,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIrB;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAItC;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,WAAW,IAAI,IAAI;CAGtB"}
@@ -0,0 +1,146 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { substitute } from '@amazon/vinyl-util';
6
+ import { ValueSchema } from './ValueSchema';
7
+ import { typeOfValidators } from './typeOfValidators';
8
+ import { createValidator } from './Validator';
9
+ const locale = {
10
+ gte: 'at least {value}',
11
+ gt: 'greater than {value}',
12
+ lte: 'at most {value}',
13
+ lt: 'less than {value}',
14
+ within: 'within {min} and {max}',
15
+ safeInteger: 'safe integer',
16
+ finite: 'finite',
17
+ };
18
+ export const numberValidators = {
19
+ /**
20
+ * Creates a validator that asserts that the input number is at least the provided minimum
21
+ * value.
22
+ *
23
+ * @param min The minimum (inclusive) the value may be.
24
+ */
25
+ gte(min) {
26
+ return createValidator(substitute(locale.gte, { value: min }), (input) => input >= min);
27
+ },
28
+ /**
29
+ * Creates a validator that asserts that the input number is greater than the provided minimum
30
+ * value.
31
+ *
32
+ * @param min The minimum (exclusive) the value may be.
33
+ */
34
+ gt(min) {
35
+ return createValidator(substitute(locale.gt, { value: min }), (input) => input > min);
36
+ },
37
+ /**
38
+ * Creates a validator that asserts that the input number is at most the provided maximum
39
+ * value.
40
+ *
41
+ * @param max The maximum (inclusive) the value may be.
42
+ */
43
+ lte(max) {
44
+ return createValidator(substitute(locale.lte, { value: max }), (input) => input <= max);
45
+ },
46
+ /**
47
+ * Creates a validator that asserts that the input number is less than the provided maximum
48
+ * value.
49
+ *
50
+ * @param max The maximum (exclusive) the value may be.
51
+ */
52
+ lt(max) {
53
+ return createValidator(substitute(locale.lt, { value: max }), (input) => input < max);
54
+ },
55
+ /**
56
+ * Creates a validator that asserts that the input number is at least the provided minimum
57
+ * and at most the provided maximum values.
58
+ *
59
+ * @param min The minimum (inclusive) value the input may be.
60
+ * @param max The maximum (inclusive) value the input may be.
61
+ */
62
+ within(min, max) {
63
+ return createValidator(substitute(locale.within, { min, max }), (input) => input >= min && input <= max);
64
+ },
65
+ /**
66
+ * Creates a validator that asserts that the input number is finite.
67
+ */
68
+ finite() {
69
+ return createValidator(locale.finite, (input) => Number.isFinite(input));
70
+ },
71
+ /**
72
+ * Creates a validator that asserts that the input number is a safe integer.
73
+ */
74
+ safeInteger() {
75
+ return createValidator(locale.safeInteger, (input) => Number.isSafeInteger(input));
76
+ },
77
+ };
78
+ export class NumberSchema extends ValueSchema {
79
+ static _base = null;
80
+ /**
81
+ * The base Object validator validating that the input is an object.
82
+ */
83
+ static get base() {
84
+ if (this._base == null)
85
+ this._base = new NumberSchema(typeOfValidators.number);
86
+ return this._base;
87
+ }
88
+ /**
89
+ * Creates a validator that asserts that the input number is at least the provided minimum
90
+ * value.
91
+ *
92
+ * @param min The minimum (inclusive) the value may be.
93
+ */
94
+ gte(min) {
95
+ return this.chain(numberValidators.gte(min));
96
+ }
97
+ /**
98
+ * Creates a validator that asserts that the input number is greater than the provided minimum
99
+ * value.
100
+ *
101
+ * @param min The minimum (exclusive) the value may be.
102
+ */
103
+ gt(min) {
104
+ return this.chain(numberValidators.gt(min));
105
+ }
106
+ /**
107
+ * Creates a validator that asserts that the input number is at most the provided maximum
108
+ * value.
109
+ *
110
+ * @param max The maximum (inclusive) the value may be.
111
+ */
112
+ lte(max) {
113
+ return this.chain(numberValidators.lte(max));
114
+ }
115
+ /**
116
+ * Creates a validator that asserts that the input number is less than the provided maximum
117
+ * value.
118
+ *
119
+ * @param max The maximum (exclusive) the value may be.
120
+ */
121
+ lt(max) {
122
+ return this.chain(numberValidators.lt(max));
123
+ }
124
+ /**
125
+ * Creates a validator that asserts that the input number is at least the provided minimum
126
+ * and at most the provided maximum values.
127
+ *
128
+ * @param min The minimum (inclusive) value the input may be.
129
+ * @param max The maximum (inclusive) value the input may be.
130
+ */
131
+ within(min, max) {
132
+ return this.chain(numberValidators.within(min, max));
133
+ }
134
+ /**
135
+ * Creates a validator that asserts that the input number is finite.
136
+ */
137
+ finite() {
138
+ return this.chain(numberValidators.finite());
139
+ }
140
+ /**
141
+ * Creates a validator that asserts that the input number is a safe integer.
142
+ */
143
+ safeInteger() {
144
+ return this.chain(numberValidators.safeInteger());
145
+ }
146
+ }
@@ -0,0 +1,54 @@
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: { readonly [P in keyof U]-?: PropertyValidator<Required<U>[P], {
14
+ optional: IsPropertyOptional<U, P>;
15
+ }>; }) => ValidatorWithPartial<U, object>;
16
+ };
17
+ /**
18
+ * Chainable object validators.
19
+ */
20
+ export declare class ObjectSchema<T extends object> extends ValueSchema<T> implements ValidatorWithPartial<T, unknown> {
21
+ private static _base;
22
+ /**
23
+ * The base Object validator validating that the input is an object.
24
+ */
25
+ static get base(): ObjectSchema<AnyRecord>;
26
+ /**
27
+ * Creates a validator that validates the input has properties that pass their respective
28
+ * validators.
29
+ *
30
+ * @param propertyValidators
31
+ */
32
+ extend<U extends object>(propertyValidators: NoInfer<ExtendedPropertyValidators<T, U>>): ObjectSchema<U>;
33
+ /**
34
+ * Allows all properties to be undefined.
35
+ */
36
+ partial(): ObjectSchema<Partial<T>>;
37
+ /**
38
+ * Allows all recursive properties to be undefined.
39
+ */
40
+ partialDeep(): ObjectSchema<PartialDeep<T>>;
41
+ /**
42
+ * Casts the type parameter.
43
+ */
44
+ cast<T extends object>(): ObjectSchema<T>;
45
+ }
46
+ export type ExtendedPropertyValidators<T extends object, U extends object> = {
47
+ [K in keyof U as K extends keyof T ? Equal<U[K], T[K]> extends true ? never : K : K]-?: ExtendedPropertyValidator<U & T, K>;
48
+ } & {
49
+ [K in keyof U as K extends keyof T ? Equal<U[K], T[K]> extends true ? K : never : never]?: ExtendedPropertyValidator<U & T, K>;
50
+ };
51
+ export type ExtendedPropertyValidator<U extends object, P extends keyof U> = PropertyValidator<Required<U>[P], {
52
+ optional: IsPropertyOptional<U, P>;
53
+ }>;
54
+ //# sourceMappingURL=ObjectSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ObjectSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/ObjectSchema.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACR,SAAS,EACT,kBAAkB,EAClB,WAAW,EACd,MAAM,oBAAoB,CAAA;AAE3B,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAElE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAI3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oCAAoC,CAAA;AAa/D,eAAO,MAAM,gBAAgB;IACzB;;;;;OAKG;0BACQ,CAAC,SAAS,MAAM,sBAAsB,EAC7C,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,iBAAiB,CACxC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EACd;QAAE,QAAQ,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KAAE,CACzC,GACJ,KAAG,oBAAoB,CAAC,CAAC,EAAE,MAAM,CAAC;CAyE7B,CAAA;AAEV;;GAEG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,MAAM,CACtC,SAAQ,WAAW,CAAC,CAAC,CACrB,YAAW,oBAAoB,CAAC,CAAC,EAAE,OAAO,CAAC;IAE3C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAuC;IAE3D;;OAEG;IACH,MAAM,KAAK,IAAI,IAAI,YAAY,CAAC,SAAS,CAAC,CAIzC;IAED;;;;;OAKG;IACH,MAAM,CAAC,CAAC,SAAS,MAAM,EACnB,kBAAkB,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAC9D,YAAY,CAAC,CAAC,CAAC;IAIlB;;OAEG;IACH,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAInC;;OAEG;IACH,WAAW,IAAI,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAI3C;;OAEG;IACH,IAAI,CAAC,CAAC,SAAS,MAAM,KAAK,YAAY,CAAC,CAAC,CAAC;CAG5C;AAED,MAAM,MAAM,0BAA0B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI;KAIxE,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAC5B,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAC1B,KAAK,GACL,CAAC,GACL,CAAC,CAAC,CAAC,GAAG,yBAAyB,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;CAClD,GAAG;KAGC,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAC5B,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAC1B,CAAC,GACD,KAAK,GACT,KAAK,CAAC,CAAC,EAAE,yBAAyB,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;CACrD,CAAA;AAED,MAAM,MAAM,yBAAyB,CACjC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,IACjB,iBAAiB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;IAAE,QAAQ,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAAE,CAAC,CAAA"}
@@ -0,0 +1,125 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { merge, ownKeys, substitute } from '@amazon/vinyl-util';
6
+ import { maybeToDeepPartial, maybeToPartial } from './ValidatorWithPartial';
7
+ import { ValueSchema } from './ValueSchema';
8
+ import { typeOfValidators } from './typeOfValidators';
9
+ import { createDeepValidator } from './Validator';
10
+ /**
11
+ * @private
12
+ */
13
+ const locale = {
14
+ extend: '{\n{body}\n}',
15
+ prop: '{key}: {value}',
16
+ propSeparator: ',\n',
17
+ indent: ' ',
18
+ required: `property '{key}' is required`,
19
+ };
20
+ export const objectValidators = {
21
+ /**
22
+ * Creates a validator that validates the input has properties that pass their respective
23
+ * validators.
24
+ *
25
+ * @param propertyValidators
26
+ */
27
+ properties(propertyValidators) {
28
+ const propertiesDescriptions = [];
29
+ const keys = ownKeys(propertyValidators);
30
+ for (const key of keys) {
31
+ const validator = propertyValidators[key];
32
+ propertiesDescriptions.push(substitute(locale.prop, {
33
+ key: String(key) + (validator.options.optional ? '?' : ''),
34
+ value: validator.description,
35
+ }));
36
+ }
37
+ const description = substitute(locale.extend, {
38
+ body: propertiesDescriptions
39
+ .join(locale.propSeparator)
40
+ .split('\n')
41
+ .map((line) => locale.indent + line)
42
+ .join('\n'),
43
+ });
44
+ const validator = createDeepValidator(description, (input, options, path) => {
45
+ const errors = [];
46
+ for (const key of keys) {
47
+ const validator = propertyValidators[key];
48
+ if (key in input) {
49
+ const value = input[key];
50
+ errors.push(...validator.validate(value, options, path.concat(String(key))));
51
+ }
52
+ else {
53
+ // Missing property
54
+ if (!validator.options.optional) {
55
+ errors.push({
56
+ message: substitute(locale.required, { key }),
57
+ path: path.concat(String(key)),
58
+ });
59
+ }
60
+ }
61
+ if (!options.all && errors.length)
62
+ return errors;
63
+ }
64
+ return errors;
65
+ });
66
+ return merge(validator, {
67
+ partial() {
68
+ const optionalPropertyValidators = {};
69
+ for (const key of keys) {
70
+ optionalPropertyValidators[key] =
71
+ propertyValidators[key].optional();
72
+ }
73
+ return objectValidators.properties(optionalPropertyValidators);
74
+ },
75
+ partialDeep() {
76
+ const optionalPropertyValidators = {};
77
+ for (const key of keys) {
78
+ optionalPropertyValidators[key] = maybeToDeepPartial(propertyValidators[key].optional());
79
+ }
80
+ return objectValidators.properties(optionalPropertyValidators);
81
+ },
82
+ });
83
+ },
84
+ };
85
+ /**
86
+ * Chainable object validators.
87
+ */
88
+ export class ObjectSchema extends ValueSchema {
89
+ static _base = null;
90
+ /**
91
+ * The base Object validator validating that the input is an object.
92
+ */
93
+ static get base() {
94
+ if (this._base == null)
95
+ this._base = new ObjectSchema(typeOfValidators.object);
96
+ return this._base;
97
+ }
98
+ /**
99
+ * Creates a validator that validates the input has properties that pass their respective
100
+ * validators.
101
+ *
102
+ * @param propertyValidators
103
+ */
104
+ extend(propertyValidators) {
105
+ return this.chain(objectValidators.properties(propertyValidators));
106
+ }
107
+ /**
108
+ * Allows all properties to be undefined.
109
+ */
110
+ partial() {
111
+ return this.clone(maybeToPartial);
112
+ }
113
+ /**
114
+ * Allows all recursive properties to be undefined.
115
+ */
116
+ partialDeep() {
117
+ return this.clone(maybeToDeepPartial);
118
+ }
119
+ /**
120
+ * Casts the type parameter.
121
+ */
122
+ cast() {
123
+ return this;
124
+ }
125
+ }
@@ -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 @@
1
+ {"version":3,"file":"PropertyValidator.d.ts","sourceRoot":"","sources":["../../../../src/validation/PropertyValidator.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AACvD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAE/C,MAAM,WAAW,wBAAwB;IACrC;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;CAC7B;AAED;;;GAGG;AACH,MAAM,MAAM,4BAA4B,CACpC,aAAa,SAAS,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,EACjD,CAAC,EACD,OAAO,SAAS,wBAAwB,IACxC,IAAI,CAAC,aAAa,EAAE,MAAM,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACtD,iBAAiB,CAAC,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,CAAC,CAAA;AAElE,MAAM,WAAW,iBAAiB,CAC9B,CAAC,EACD,mBAAmB,SAAS,wBAAwB,CACtD,SAAQ,SAAS,CAAC,CAAC,CAAC;IAClB,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAA;IAErC,QAAQ,IAAI,4BAA4B,CACpC,IAAI,EACJ,CAAC,EACD;QAAE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAA;KAAE,CAC9B,CAAA;IAED,QAAQ,IAAI,4BAA4B,CACpC,IAAI,EACJ,CAAC,EACD;QAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAA;KAAE,CAC/B,CAAA;CACJ"}