@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,5 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ export {};
@@ -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 @@
1
+ {"version":3,"file":"RecordSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/RecordSchema.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAG3C,OAAO,KAAK,EAA0B,SAAS,EAAE,MAAM,aAAa,CAAA;AAUpE,eAAO,MAAM,gBAAgB;IACzB;;;;;;;;;;OAUG;sBACI,CAAC,SAAS,MAAM,GAAG,MAAM,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X-License-Identifier: Apache-2.0
4
+ */
5
+ import { ownKeys, substitute } from '@amazon/vinyl-util';
6
+ import { ValueSchema } from './ValueSchema';
7
+ import { StringSchema } from './StringSchema';
8
+ import { typeOfValidators } from './typeOfValidators';
9
+ import { createDeepValidator } from './Validator';
10
+ /**
11
+ * @private
12
+ */
13
+ const locale = {
14
+ record: '{ [key: {key}]: {value} }',
15
+ };
16
+ export const recordValidators = {
17
+ /**
18
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
19
+ * values that match their respective validators.
20
+ *
21
+ * Prototype members are not included.
22
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
23
+ * to the key validator as '0'.
24
+ *
25
+ * @param keyValidator Validates each key.
26
+ * @param valueValidator Validates each value.
27
+ */
28
+ record(keyValidator, valueValidator) {
29
+ return createDeepValidator(substitute(locale.record, {
30
+ key: keyValidator.description,
31
+ value: valueValidator.description,
32
+ }), (input, options, path) => {
33
+ const errors = [];
34
+ for (const key of ownKeys(input)) {
35
+ const keyStr = String(key);
36
+ errors.push(...keyValidator.validate(key, options, path.concat(`[${keyStr}]`)));
37
+ if (!options.all && errors.length)
38
+ return errors;
39
+ const value = input[key];
40
+ errors.push(...valueValidator.validate(value, options, path.concat(keyStr)));
41
+ if (!options.all && errors.length)
42
+ return errors;
43
+ }
44
+ return errors;
45
+ });
46
+ },
47
+ };
48
+ /**
49
+ * Chainable object validators for the Record type.
50
+ */
51
+ export class RecordSchema extends ValueSchema {
52
+ static _base = null;
53
+ /**
54
+ * The base Object validator validating that the input is an object.
55
+ */
56
+ static get base() {
57
+ if (this._base == null)
58
+ this._base = new RecordSchema(typeOfValidators.object);
59
+ return this._base;
60
+ }
61
+ static get defaultKeyValidator() {
62
+ return StringSchema.base;
63
+ }
64
+ /**
65
+ * Returns a validator that asserts that the object's own enumerable properties has keys and
66
+ * values that match their respective validators.
67
+ *
68
+ * Prototype members are not included.
69
+ * Numeric keys are validated as strings. For example the object `{ [0]: 1 }` will be passed
70
+ * to the key validator as '0'.
71
+ *
72
+ * @param keyValidator Validates each key.
73
+ * @param valueValidator Validates each value.
74
+ */
75
+ record(keyValidator, valueValidator) {
76
+ return this.chain(recordValidators.record(keyValidator, valueValidator));
77
+ }
78
+ /**
79
+ * Returns a validator that asserts that the object's own enumerable properties has
80
+ * string keys and values that match the given validator.
81
+ *
82
+ * Prototype members are not included.
83
+ *
84
+ * @param valueValidator Validates each value.
85
+ */
86
+ recordValues(valueValidator) {
87
+ return this.record(RecordSchema.defaultKeyValidator, valueValidator);
88
+ }
89
+ /**
90
+ * Casts to a ReadonlyRecord
91
+ */
92
+ readonly() {
93
+ return this;
94
+ }
95
+ /**
96
+ * Casts the type parameter.
97
+ */
98
+ cast() {
99
+ return this;
100
+ }
101
+ }
@@ -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 @@
1
+ {"version":3,"file":"SchemaBase.d.ts","sourceRoot":"","sources":["../../../../src/validation/SchemaBase.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACR,sBAAsB,EACtB,iBAAiB,EACjB,SAAS,EACZ,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,EACR,iBAAiB,EAEjB,4BAA4B,EAC/B,MAAM,qBAAqB,CAAA;AAM5B;;;;;GAKG;AACH,8BAAsB,UAAU,CAAC,CAAC,CAAE,YAAW,iBAAiB,CAC5D,CAAC,EACD;IAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAA;CAAE,CAC/B;IAkCO,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAA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EAAE,GAAG,CAAC,EACrC,cAAc,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,SAAS,EAC3C,SAAS,EACH,CAAC,CACG,UAAU,EAAE,SAAS,CAAC,GAAG,CAAC,EAC1B,UAAU,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,KAC9B,SAAS,CAAC,CAAC,CAAC,CAAC,GAClB,SAAS,GAChB,GAAG;IAkBN;;;;;;;OAOG;IACH,SAAS,CAAC,KAAK,CAAC,aAAa,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,GAAG;IAIxD,QAAQ,IAAI,4BAA4B,CACpC,IAAI,EACJ,CAAC,EACD;QAAE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAA;KAAE,CAC9B;IASD,QAAQ,IAAI,4BAA4B,CACpC,IAAI,EACJ,CAAC,EACD;QAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAA;KAAE,CAC/B;CAQJ"}
@@ -0,0 +1,90 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { IllegalArgumentError, invariant } from '@amazon/vinyl-util';
6
+ import { andValidators } from './andValidators';
7
+ const requiredValidationOptions = {
8
+ optional: false,
9
+ };
10
+ /**
11
+ * A base class for validation state.
12
+ * Provides methods for chaining.
13
+ *
14
+ * Subclasses should not override constructors.
15
+ */
16
+ export class SchemaBase {
17
+ currentValidator;
18
+ previousSchema;
19
+ operation;
20
+ __type = invariant();
21
+ validator;
22
+ _options = requiredValidationOptions;
23
+ get options() {
24
+ return this._options;
25
+ }
26
+ constructor(currentValidator, previousSchema, operation) {
27
+ this.currentValidator = currentValidator;
28
+ this.previousSchema = previousSchema;
29
+ this.operation = operation;
30
+ this.validator =
31
+ operation == null
32
+ ? currentValidator
33
+ : operation(previousSchema, currentValidator);
34
+ }
35
+ get description() {
36
+ return this.validator.description;
37
+ }
38
+ assert = (input, origin, path) => {
39
+ this.validator.assert(input, origin, path);
40
+ };
41
+ isValid = (input) => this.validator.isValid(input);
42
+ validate = (input, options, path) => this.validator.validate(input, options, path);
43
+ /**
44
+ * Clones this schema, transforming all validators with the given mutator.
45
+ *
46
+ * @param transform A transformer which takes an existing validator in the chain and returns
47
+ * a new one. Validators should be considered to be immutable; transform should not change the
48
+ * input but instead return a new validator.
49
+ */
50
+ clone(transform = (v) => v) {
51
+ const cloned = this.operation
52
+ ? this.self(transform(this.currentValidator), this.previousSchema.clone(transform), this.operation)
53
+ : this.self(transform(this.currentValidator));
54
+ cloned._options = this._options;
55
+ return cloned;
56
+ }
57
+ self(current, previous, operation) {
58
+ if (this.constructor.length > 3) {
59
+ throw new IllegalArgumentError('constructor should have at most three required arguments');
60
+ }
61
+ return new this.constructor(current, previous, operation);
62
+ }
63
+ /**
64
+ * Constructs a new schema with a validator chaining the current validator with the
65
+ * provided next in an AND operation.
66
+ *
67
+ * @param nextValidator The validator to run after the current.
68
+ * @return Returns a new Schema instance the same class with a new validator chaining the
69
+ * current validator with the given next in an AND operation.
70
+ */
71
+ chain(nextValidator) {
72
+ return this.self(nextValidator, this, andValidators);
73
+ }
74
+ optional() {
75
+ const cloned = this.clone();
76
+ cloned._options = {
77
+ ...cloned.options,
78
+ optional: true,
79
+ };
80
+ return cloned;
81
+ }
82
+ required() {
83
+ const cloned = this.clone();
84
+ cloned._options = {
85
+ ...cloned.options,
86
+ optional: false,
87
+ };
88
+ return cloned;
89
+ }
90
+ }
@@ -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 @@
1
+ {"version":3,"file":"SetSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/SetSchema.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,OAAO,KAAK,EAA0B,SAAS,EAAE,MAAM,aAAa,CAAA;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAU3C,eAAO,MAAM,aAAa;IACtB;;OAEG;0BACM,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAOhC;;;;OAIG;4BACU,CAAC,oBACQ,SAAS,CAAC,CAAC,CAAC,KAC/B,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;CAuBpC,CAAA;AAEV,qBAAa,SAAS,CAAC,CAAC,SAAS,WAAW,CAAC,OAAO,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IACzE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAuC;IAE3D;;OAEG;IACH,MAAM,KAAK,IAAI,IAAI,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAIzC;IAED;;;;OAIG;IACH,YAAY,CAAC,CAAC,EACV,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC,GAC/B,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAIxC;;;OAGG;IACH,QAAQ,IAAI,SAAS,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IAIrD;;OAEG;IACH,IAAI,CAAC,CAAC,SAAS,WAAW,CAAC,OAAO,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC;CAGvD"}
@@ -0,0 +1,74 @@
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 { createDeepValidator, createValidator } from './Validator';
7
+ import { ValueSchema } from './ValueSchema';
8
+ /**
9
+ * @private
10
+ */
11
+ const locale = {
12
+ set: 'Set',
13
+ withElements: 'Set<{value}>',
14
+ };
15
+ export const setValidators = {
16
+ /**
17
+ * Creates a validator that asserts that the input is a set.
18
+ */
19
+ isSet() {
20
+ return createValidator(locale.set, (input) => input instanceof Set);
21
+ },
22
+ /**
23
+ * Creates a validator that asserts that every element passes the given validator.
24
+ *
25
+ * @param elementValidator
26
+ */
27
+ withElements(elementValidator) {
28
+ return createDeepValidator(substitute(locale.withElements, {
29
+ value: elementValidator.description,
30
+ }), (input, options, path) => {
31
+ const errors = [];
32
+ let i = 0;
33
+ for (const element of input) {
34
+ errors.push(...elementValidator.validate(element, options, path.concat(i.toString())));
35
+ if (!options.all && errors.length)
36
+ return errors;
37
+ i++;
38
+ }
39
+ return errors;
40
+ });
41
+ },
42
+ };
43
+ export class SetSchema extends ValueSchema {
44
+ static _base = null;
45
+ /**
46
+ * The base SetSchema instance validating that the input is a Set.
47
+ */
48
+ static get base() {
49
+ if (this._base == null)
50
+ this._base = new SetSchema(setValidators.isSet());
51
+ return this._base;
52
+ }
53
+ /**
54
+ * Creates a validator that asserts that every element passes the given validator.
55
+ *
56
+ * @param elementValidator
57
+ */
58
+ withElements(elementValidator) {
59
+ return this.chain(setValidators.withElements(elementValidator));
60
+ }
61
+ /**
62
+ * Casts to a readonly set.
63
+ * This is a shallow cast, elements of the array will not be changed.
64
+ */
65
+ readonly() {
66
+ return this;
67
+ }
68
+ /**
69
+ * Casts the parameter type.
70
+ */
71
+ cast() {
72
+ return this;
73
+ }
74
+ }
@@ -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 @@
1
+ {"version":3,"file":"StringSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/StringSchema.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAc5C,eAAO,MAAM,gBAAgB;IACzB;;OAEG;6BACS,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAIrC;;;;;OAKG;8BACY,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAOjD;;;;;OAKG;8BACY,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAOjD;;;OAGG;iCACa,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAOzC;;;OAGG;8BACY,MAAM,KAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;CAQ3C,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;;OAEG;IACH,QAAQ,IAAI,YAAY;IAIxB;;;;;OAKG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY;IAIpC;;;;;OAKG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY;IAIpC;;;OAGG;IACH,YAAY,IAAI,YAAY;IAI5B;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;CAGvC"}
@@ -0,0 +1,109 @@
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
+ /**
10
+ * @private
11
+ */
12
+ const locale = {
13
+ notEmpty: 'not empty',
14
+ minLength: 'at least {value} characters',
15
+ maxLength: 'at most {value} characters',
16
+ noWhitespace: 'no whitespace characters',
17
+ matches: 'matches {value}',
18
+ };
19
+ export const stringValidators = {
20
+ /**
21
+ * Creates a validator that asserts that the input is not empty.
22
+ */
23
+ notEmpty() {
24
+ return createValidator(locale.notEmpty, (input) => input !== '');
25
+ },
26
+ /**
27
+ * Creates a validator that asserts that the input string has at least the given minimum
28
+ * length.
29
+ *
30
+ * @param min The string must be at least this length.
31
+ */
32
+ minLength(min) {
33
+ return createValidator(substitute(locale.minLength, { value: min }), (input) => input.length >= min);
34
+ },
35
+ /**
36
+ * Creates a validator that asserts that the input string has at most the given maximum
37
+ * length.
38
+ *
39
+ * @param max The string must be at most this length.
40
+ */
41
+ maxLength(max) {
42
+ return createValidator(substitute(locale.maxLength, { value: max }), (input) => input.length <= max);
43
+ },
44
+ /**
45
+ * Creates a validator that asserts that the input string has no whitespace characters.
46
+ * Whitespace characters include space, new line, carriage return, and tab.
47
+ */
48
+ noWhitespace() {
49
+ return createValidator(locale.noWhitespace, (input) => input.search(/\s/g) === -1);
50
+ },
51
+ /**
52
+ * Creates a validator that asserts that the input string matches the given regex.
53
+ * @param regex
54
+ */
55
+ matches(regex) {
56
+ return createValidator(substitute(locale.matches, { value: regex.toString() }), (input) => {
57
+ return regex.test(input);
58
+ });
59
+ },
60
+ };
61
+ export class StringSchema extends ValueSchema {
62
+ static _base = null;
63
+ /**
64
+ * The base SetSchema instance validating that the input is a Set.
65
+ */
66
+ static get base() {
67
+ if (this._base == null)
68
+ this._base = new StringSchema(typeOfValidators.string);
69
+ return this._base;
70
+ }
71
+ /**
72
+ * Creates a validator that asserts that the input is not empty.
73
+ */
74
+ notEmpty() {
75
+ return this.chain(stringValidators.notEmpty());
76
+ }
77
+ /**
78
+ * Creates a validator that asserts that the input string has at least the given minimum
79
+ * length.
80
+ *
81
+ * @param min The string must be at least this length.
82
+ */
83
+ minLength(min) {
84
+ return this.chain(stringValidators.minLength(min));
85
+ }
86
+ /**
87
+ * Creates a validator that asserts that the input string has at most the given maximum
88
+ * length.
89
+ *
90
+ * @param max The string must be at most this length.
91
+ */
92
+ maxLength(max) {
93
+ return this.chain(stringValidators.maxLength(max));
94
+ }
95
+ /**
96
+ * Creates a validator that asserts that the input string has no whitespace characters.
97
+ * Whitespace characters include space, new line, carriage return, and tab.
98
+ */
99
+ noWhitespace() {
100
+ return this.chain(stringValidators.noWhitespace());
101
+ }
102
+ /**
103
+ * Creates a validator that asserts that the input string matches the given regex.
104
+ * @param regex
105
+ */
106
+ matches(regex) {
107
+ return this.chain(stringValidators.matches(regex));
108
+ }
109
+ }