@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.
- package/dist/index.cjs +1325 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +1308 -0
- package/dist/index.js.map +7 -0
- package/dist/types/current/index.d.ts +18 -0
- package/dist/types/current/index.d.ts.map +1 -0
- package/dist/types/current/index.js +17 -0
- package/dist/types/current/tsconfig.tsbuildinfo +1 -0
- package/dist/types/current/validation/ArraySchema.d.ts +82 -0
- package/dist/types/current/validation/ArraySchema.d.ts.map +1 -0
- package/dist/types/current/validation/ArraySchema.js +148 -0
- package/dist/types/current/validation/FunctionSchema.d.ts +21 -0
- package/dist/types/current/validation/FunctionSchema.d.ts.map +1 -0
- package/dist/types/current/validation/FunctionSchema.js +42 -0
- package/dist/types/current/validation/NumberSchema.d.ts +100 -0
- package/dist/types/current/validation/NumberSchema.d.ts.map +1 -0
- package/dist/types/current/validation/NumberSchema.js +146 -0
- package/dist/types/current/validation/ObjectSchema.d.ts +54 -0
- package/dist/types/current/validation/ObjectSchema.d.ts.map +1 -0
- package/dist/types/current/validation/ObjectSchema.js +125 -0
- package/dist/types/current/validation/PropertyValidator.d.ts +23 -0
- package/dist/types/current/validation/PropertyValidator.d.ts.map +1 -0
- package/dist/types/current/validation/PropertyValidator.js +5 -0
- package/dist/types/current/validation/RecordSchema.d.ts +58 -0
- package/dist/types/current/validation/RecordSchema.d.ts.map +1 -0
- package/dist/types/current/validation/RecordSchema.js +101 -0
- package/dist/types/current/validation/SchemaBase.d.ts +65 -0
- package/dist/types/current/validation/SchemaBase.d.ts.map +1 -0
- package/dist/types/current/validation/SchemaBase.js +90 -0
- package/dist/types/current/validation/SetSchema.d.ts +38 -0
- package/dist/types/current/validation/SetSchema.d.ts.map +1 -0
- package/dist/types/current/validation/SetSchema.js +74 -0
- package/dist/types/current/validation/StringSchema.d.ts +68 -0
- package/dist/types/current/validation/StringSchema.d.ts.map +1 -0
- package/dist/types/current/validation/StringSchema.js +109 -0
- package/dist/types/current/validation/Validator.d.ts +88 -0
- package/dist/types/current/validation/Validator.d.ts.map +1 -0
- package/dist/types/current/validation/Validator.js +95 -0
- package/dist/types/current/validation/ValidatorWithPartial.d.ts +26 -0
- package/dist/types/current/validation/ValidatorWithPartial.d.ts.map +1 -0
- package/dist/types/current/validation/ValidatorWithPartial.js +31 -0
- package/dist/types/current/validation/ValueSchema.d.ts +52 -0
- package/dist/types/current/validation/ValueSchema.d.ts.map +1 -0
- package/dist/types/current/validation/ValueSchema.js +81 -0
- package/dist/types/current/validation/andValidators.d.ts +19 -0
- package/dist/types/current/validation/andValidators.d.ts.map +1 -0
- package/dist/types/current/validation/andValidators.js +48 -0
- package/dist/types/current/validation/orValidators.d.ts +19 -0
- package/dist/types/current/validation/orValidators.d.ts.map +1 -0
- package/dist/types/current/validation/orValidators.js +74 -0
- package/dist/types/current/validation/typeOfValidators.d.ts +15 -0
- package/dist/types/current/validation/typeOfValidators.d.ts.map +1 -0
- package/dist/types/current/validation/typeOfValidators.js +58 -0
- package/dist/types/current/validation/validators.d.ts +127 -0
- package/dist/types/current/validation/validators.d.ts.map +1 -0
- package/dist/types/current/validation/validators.js +168 -0
- package/dist/types/ts3.7/index.d.ts +18 -0
- package/dist/types/ts3.7/validation/ArraySchema.d.ts +86 -0
- package/dist/types/ts3.7/validation/FunctionSchema.d.ts +21 -0
- package/dist/types/ts3.7/validation/NumberSchema.d.ts +100 -0
- package/dist/types/ts3.7/validation/ObjectSchema.d.ts +56 -0
- package/dist/types/ts3.7/validation/PropertyValidator.d.ts +23 -0
- package/dist/types/ts3.7/validation/RecordSchema.d.ts +58 -0
- package/dist/types/ts3.7/validation/SchemaBase.d.ts +65 -0
- package/dist/types/ts3.7/validation/SetSchema.d.ts +38 -0
- package/dist/types/ts3.7/validation/StringSchema.d.ts +68 -0
- package/dist/types/ts3.7/validation/Validator.d.ts +88 -0
- package/dist/types/ts3.7/validation/ValidatorWithPartial.d.ts +26 -0
- package/dist/types/ts3.7/validation/ValueSchema.d.ts +52 -0
- package/dist/types/ts3.7/validation/andValidators.d.ts +19 -0
- package/dist/types/ts3.7/validation/orValidators.d.ts +19 -0
- package/dist/types/ts3.7/validation/typeOfValidators.d.ts +15 -0
- package/dist/types/ts3.7/validation/validators.d.ts +127 -0
- package/dist/types/ts4.1/index.d.ts +18 -0
- package/dist/types/ts4.1/validation/ArraySchema.d.ts +86 -0
- package/dist/types/ts4.1/validation/FunctionSchema.d.ts +21 -0
- package/dist/types/ts4.1/validation/NumberSchema.d.ts +100 -0
- package/dist/types/ts4.1/validation/ObjectSchema.d.ts +56 -0
- package/dist/types/ts4.1/validation/PropertyValidator.d.ts +23 -0
- package/dist/types/ts4.1/validation/RecordSchema.d.ts +58 -0
- package/dist/types/ts4.1/validation/SchemaBase.d.ts +65 -0
- package/dist/types/ts4.1/validation/SetSchema.d.ts +38 -0
- package/dist/types/ts4.1/validation/StringSchema.d.ts +68 -0
- package/dist/types/ts4.1/validation/Validator.d.ts +88 -0
- package/dist/types/ts4.1/validation/ValidatorWithPartial.d.ts +26 -0
- package/dist/types/ts4.1/validation/ValueSchema.d.ts +52 -0
- package/dist/types/ts4.1/validation/andValidators.d.ts +19 -0
- package/dist/types/ts4.1/validation/orValidators.d.ts +19 -0
- package/dist/types/ts4.1/validation/typeOfValidators.d.ts +15 -0
- package/dist/types/ts4.1/validation/validators.d.ts +128 -0
- package/dist/types/ts4.7/index.d.ts +18 -0
- package/dist/types/ts4.7/validation/ArraySchema.d.ts +86 -0
- package/dist/types/ts4.7/validation/FunctionSchema.d.ts +21 -0
- package/dist/types/ts4.7/validation/NumberSchema.d.ts +100 -0
- package/dist/types/ts4.7/validation/ObjectSchema.d.ts +56 -0
- package/dist/types/ts4.7/validation/PropertyValidator.d.ts +23 -0
- package/dist/types/ts4.7/validation/RecordSchema.d.ts +58 -0
- package/dist/types/ts4.7/validation/SchemaBase.d.ts +65 -0
- package/dist/types/ts4.7/validation/SetSchema.d.ts +38 -0
- package/dist/types/ts4.7/validation/StringSchema.d.ts +68 -0
- package/dist/types/ts4.7/validation/Validator.d.ts +88 -0
- package/dist/types/ts4.7/validation/ValidatorWithPartial.d.ts +26 -0
- package/dist/types/ts4.7/validation/ValueSchema.d.ts +52 -0
- package/dist/types/ts4.7/validation/andValidators.d.ts +19 -0
- package/dist/types/ts4.7/validation/orValidators.d.ts +19 -0
- package/dist/types/ts4.7/validation/typeOfValidators.d.ts +15 -0
- package/dist/types/ts4.7/validation/validators.d.ts +127 -0
- package/docs/VALIDATION.md +344 -0
- package/package.json +82 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type Invariant } from '@amazon/vinyl-util';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a validation failure.
|
|
4
|
+
*/
|
|
5
|
+
export interface ValidationErrorMessage {
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly path: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
export interface ValidationOptions {
|
|
10
|
+
/**
|
|
11
|
+
* If true, collect all validation errors.
|
|
12
|
+
* Default: false
|
|
13
|
+
*/
|
|
14
|
+
readonly all?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A Validator can assert that an input matches this validator's schema.
|
|
18
|
+
*/
|
|
19
|
+
export interface Validator<Output extends Input, in Input = unknown> {
|
|
20
|
+
/**
|
|
21
|
+
* Enforces T is in an invariant position.
|
|
22
|
+
* This prevents a validator with more strict rules is assigned to a less strict type, which
|
|
23
|
+
* can cause unexpected validation errors.
|
|
24
|
+
*/
|
|
25
|
+
__type?: Invariant<Output>;
|
|
26
|
+
/**
|
|
27
|
+
* A short description of the validator.
|
|
28
|
+
*/
|
|
29
|
+
readonly description: string;
|
|
30
|
+
/**
|
|
31
|
+
* Returns true if the input is valid according to this validator.
|
|
32
|
+
*
|
|
33
|
+
* @param input
|
|
34
|
+
*/
|
|
35
|
+
readonly isValid: (input: Input) => input is Output;
|
|
36
|
+
/**
|
|
37
|
+
* Validates the provided input, returning a list of error messages, or an empty array if the
|
|
38
|
+
* input passes this validator.
|
|
39
|
+
*
|
|
40
|
+
* @param input The input to validate.
|
|
41
|
+
* @param options Validation options.
|
|
42
|
+
* @param path The current field path to this validator.
|
|
43
|
+
* @return Returns an array of validation failures. If `options.all` is not true, only the
|
|
44
|
+
* first error will be returned.
|
|
45
|
+
*/
|
|
46
|
+
readonly validate: (input: Input, options?: ValidationOptions, path?: readonly string[]) => readonly ValidationErrorMessage[];
|
|
47
|
+
/**
|
|
48
|
+
* Validates the given input, asserting that the input is type `T`.
|
|
49
|
+
* If the input is not valid, a {@link ValidationError} will be thrown.
|
|
50
|
+
*
|
|
51
|
+
* @param input The input to validate.
|
|
52
|
+
* @param origin The error origin. `ErrorOrigin.API` by default.
|
|
53
|
+
* @param path The current field path to this validator.
|
|
54
|
+
* @return Returns the input, cast as the validated type T
|
|
55
|
+
*/
|
|
56
|
+
readonly assert: (input: Input, origin?: string, path?: readonly string[]) => asserts input is Output;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a message string for a ValidationErrorMessage based on the input and its expectation.
|
|
60
|
+
*
|
|
61
|
+
* @param expectationMessage
|
|
62
|
+
* @param input
|
|
63
|
+
* @param path
|
|
64
|
+
* @param inputStringify
|
|
65
|
+
*/
|
|
66
|
+
export declare function createValidationExpectationMessage<Input>(expectationMessage: string, input: Input, path: readonly string[], inputStringify?: (input: Input) => string): string;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new basic validator.
|
|
69
|
+
*
|
|
70
|
+
* For a deep validator which has sub-fields, use {@link createDeepValidator}.
|
|
71
|
+
*
|
|
72
|
+
* @param expectationMessage The message to use to describe what is expected. This will be used in
|
|
73
|
+
* the error message template.
|
|
74
|
+
* @param isValid Returns true if the given input is valid.
|
|
75
|
+
* @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
|
|
76
|
+
* expectation template. Default is `stringifyForPrint` with a 500 character max length.
|
|
77
|
+
*/
|
|
78
|
+
export declare function createValidator<Output extends Input, Input = unknown>(expectationMessage: string, isValid: (input: Input) => boolean, inputStringify?: (input: Input) => string): Validator<Output, Input>;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new deep validator.
|
|
81
|
+
*
|
|
82
|
+
* For a shallow validator which validates one value, use {@link createValidator}.
|
|
83
|
+
*
|
|
84
|
+
* @param description Describes the validator.
|
|
85
|
+
* @param validate Returns an array of validation failures.
|
|
86
|
+
*/
|
|
87
|
+
export declare function createDeepValidator<Output extends Input, Input = unknown>(description: string, validate: (input: Input, options: ValidationOptions, path: readonly string[]) => readonly ValidationErrorMessage[]): Validator<Output, Input>;
|
|
88
|
+
//# sourceMappingURL=Validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Validator.d.ts","sourceRoot":"","sources":["../../../../src/validation/Validator.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,SAAS,EAAmB,MAAM,oBAAoB,CAAA;AAGpE;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;CACnC;AAED,MAAM,WAAW,iBAAiB;IAC9B;;;OAGG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAA;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS,CAAC,MAAM,SAAS,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,OAAO;IAC/D;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAA;IAE1B;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAE5B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,IAAI,MAAM,CAAA;IAEnD;;;;;;;;;OASG;IACH,QAAQ,CAAC,QAAQ,EAAE,CACf,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,iBAAiB,EAC3B,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,KACvB,SAAS,sBAAsB,EAAE,CAAA;IAEtC;;;;;;;;OAQG;IACH,QAAQ,CAAC,MAAM,EAAE,CACb,KAAK,EAAE,KAAK,EACZ,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,KACvB,OAAO,CAAC,KAAK,IAAI,MAAM,CAAA;CAC/B;AAgBD;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EACpD,kBAAkB,EAAE,MAAM,EAC1B,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,cAAc,GAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MACD,GAClC,MAAM,CAMR;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,MAAM,SAAS,KAAK,EAAE,KAAK,GAAG,OAAO,EACjE,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,EAClC,cAAc,GAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MACD,GAClC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAgD1B;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,SAAS,KAAK,EAAE,KAAK,GAAG,OAAO,EACrE,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,CACN,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,iBAAiB,EAC1B,IAAI,EAAE,SAAS,MAAM,EAAE,KACtB,SAAS,sBAAsB,EAAE,GACvC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CA4B1B"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { ValidationError } from '@amazon/vinyl-util';
|
|
6
|
+
import { stringifyForPrint, substitute } from '@amazon/vinyl-util';
|
|
7
|
+
/**
|
|
8
|
+
* @private
|
|
9
|
+
*/
|
|
10
|
+
const validatorLocale = {
|
|
11
|
+
/**
|
|
12
|
+
* The expectation template string with the following replacement tokens:
|
|
13
|
+
*
|
|
14
|
+
* `{expected}` - The expected description.
|
|
15
|
+
* `{actual}` - The actual value.
|
|
16
|
+
* `{path}` - The path, dot separated.
|
|
17
|
+
*/
|
|
18
|
+
template: 'Expected: {expected}, but was: {actual}. At: {path}',
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Creates a message string for a ValidationErrorMessage based on the input and its expectation.
|
|
22
|
+
*
|
|
23
|
+
* @param expectationMessage
|
|
24
|
+
* @param input
|
|
25
|
+
* @param path
|
|
26
|
+
* @param inputStringify
|
|
27
|
+
*/
|
|
28
|
+
export function createValidationExpectationMessage(expectationMessage, input, path, inputStringify = (input) => stringifyForPrint(input, 500)) {
|
|
29
|
+
return substitute(validatorLocale.template, {
|
|
30
|
+
expected: expectationMessage,
|
|
31
|
+
actual: inputStringify(input),
|
|
32
|
+
path: path.join('.'),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new basic validator.
|
|
37
|
+
*
|
|
38
|
+
* For a deep validator which has sub-fields, use {@link createDeepValidator}.
|
|
39
|
+
*
|
|
40
|
+
* @param expectationMessage The message to use to describe what is expected. This will be used in
|
|
41
|
+
* the error message template.
|
|
42
|
+
* @param isValid Returns true if the given input is valid.
|
|
43
|
+
* @param inputStringify Stringifies the input for use as the actual `{actual}` token in the validation
|
|
44
|
+
* expectation template. Default is `stringifyForPrint` with a 500 character max length.
|
|
45
|
+
*/
|
|
46
|
+
export function createValidator(expectationMessage, isValid, inputStringify = (input) => stringifyForPrint(input, 500)) {
|
|
47
|
+
return {
|
|
48
|
+
description: expectationMessage,
|
|
49
|
+
isValid(input) {
|
|
50
|
+
return isValid(input);
|
|
51
|
+
},
|
|
52
|
+
validate(input, _, path = []) {
|
|
53
|
+
if (!isValid(input)) {
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
message: createValidationExpectationMessage(expectationMessage, input, path, inputStringify),
|
|
57
|
+
path,
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
return [];
|
|
62
|
+
},
|
|
63
|
+
assert: (input, origin, path = []) => {
|
|
64
|
+
if (!isValid(input)) {
|
|
65
|
+
throw new ValidationError(createValidationExpectationMessage(expectationMessage, input, path, inputStringify), origin, path);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new deep validator.
|
|
72
|
+
*
|
|
73
|
+
* For a shallow validator which validates one value, use {@link createValidator}.
|
|
74
|
+
*
|
|
75
|
+
* @param description Describes the validator.
|
|
76
|
+
* @param validate Returns an array of validation failures.
|
|
77
|
+
*/
|
|
78
|
+
export function createDeepValidator(description, validate) {
|
|
79
|
+
return {
|
|
80
|
+
description,
|
|
81
|
+
isValid(input) {
|
|
82
|
+
return validate(input, { all: false }, []).length === 0;
|
|
83
|
+
},
|
|
84
|
+
validate(input, options, path = []) {
|
|
85
|
+
return validate(input, options ?? { all: false }, path);
|
|
86
|
+
},
|
|
87
|
+
assert: (input, origin, path = []) => {
|
|
88
|
+
const errors = validate(input, { all: false }, path);
|
|
89
|
+
if (errors.length) {
|
|
90
|
+
const error = errors[0];
|
|
91
|
+
throw new ValidationError(error.message, origin, error.path);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PartialDeep } from '@amazon/vinyl-util';
|
|
2
|
+
import type { Validator } from './Validator';
|
|
3
|
+
export interface ValidatorWithPartial<Output extends Input, Input> extends Validator<Output, Input> {
|
|
4
|
+
partial(): ValidatorWithPartial<Partial<Output> & Input, Input>;
|
|
5
|
+
partialDeep(): ValidatorWithPartial<PartialDeep<Output> & Input, Input>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the given validator is an object validator.
|
|
9
|
+
* @param validator
|
|
10
|
+
*/
|
|
11
|
+
export declare function isValidatorWithPartial<Output extends Input, Input>(validator: Validator<Output, Input>): validator is ValidatorWithPartial<Output, Input>;
|
|
12
|
+
/**
|
|
13
|
+
* If the given validator is an object validator, it will be cloned to a deep partial validator,
|
|
14
|
+
* otherwise returns the validator as is.
|
|
15
|
+
*
|
|
16
|
+
* @param validator
|
|
17
|
+
*/
|
|
18
|
+
export declare function maybeToDeepPartial(validator: Validator<any>): Validator<any>;
|
|
19
|
+
/**
|
|
20
|
+
* If the given validator is an object validator, it will be cloned to a partial validator,
|
|
21
|
+
* otherwise returns the validator as is.
|
|
22
|
+
*
|
|
23
|
+
* @param validator
|
|
24
|
+
*/
|
|
25
|
+
export declare function maybeToPartial(validator: Validator<any>): Validator<any>;
|
|
26
|
+
//# sourceMappingURL=ValidatorWithPartial.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidatorWithPartial.d.ts","sourceRoot":"","sources":["../../../../src/validation/ValidatorWithPartial.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE5C,MAAM,WAAW,oBAAoB,CACjC,MAAM,SAAS,KAAK,EACpB,KAAK,CACP,SAAQ,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC;IAC9B,OAAO,IAAI,oBAAoB,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,CAAA;IAC/D,WAAW,IAAI,oBAAoB,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,CAAA;CAC1E;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,SAAS,KAAK,EAAE,KAAK,EAC9D,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,GACpC,SAAS,IAAI,oBAAoB,CAAC,MAAM,EAAE,KAAK,CAAC,CAElD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAI5E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAExE"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if the given validator is an object validator.
|
|
7
|
+
* @param validator
|
|
8
|
+
*/
|
|
9
|
+
export function isValidatorWithPartial(validator) {
|
|
10
|
+
return 'partial' in validator && 'partialDeep' in validator;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* If the given validator is an object validator, it will be cloned to a deep partial validator,
|
|
14
|
+
* otherwise returns the validator as is.
|
|
15
|
+
*
|
|
16
|
+
* @param validator
|
|
17
|
+
*/
|
|
18
|
+
export function maybeToDeepPartial(validator) {
|
|
19
|
+
return isValidatorWithPartial(validator)
|
|
20
|
+
? validator.partialDeep()
|
|
21
|
+
: validator;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* If the given validator is an object validator, it will be cloned to a partial validator,
|
|
25
|
+
* otherwise returns the validator as is.
|
|
26
|
+
*
|
|
27
|
+
* @param validator
|
|
28
|
+
*/
|
|
29
|
+
export function maybeToPartial(validator) {
|
|
30
|
+
return isValidatorWithPartial(validator) ? validator.partial() : validator;
|
|
31
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { SchemaBase } from './SchemaBase';
|
|
2
|
+
import type { Validator } from './Validator';
|
|
3
|
+
export declare const valueValidators: {
|
|
4
|
+
readonly any: () => Validator<any>;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a validator that asserts that the input is nullish.
|
|
7
|
+
*/
|
|
8
|
+
readonly nullish: () => Validator<null | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a validator that asserts that the input is strictly null.
|
|
11
|
+
*/
|
|
12
|
+
readonly null: () => Validator<null>;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a validator that asserts that the input is strictly undefined.
|
|
15
|
+
*/
|
|
16
|
+
readonly undefined: () => Validator<undefined>;
|
|
17
|
+
};
|
|
18
|
+
export declare class ValueSchema<T> extends SchemaBase<T> {
|
|
19
|
+
/**
|
|
20
|
+
* Prepends this validator allowing for null or undefined.
|
|
21
|
+
*/
|
|
22
|
+
maybe(): ValueSchema<T | undefined | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Prepends this validator allowing for null.
|
|
25
|
+
*/
|
|
26
|
+
orNull(): ValueSchema<T | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Prepends this validator allowing for undefined.
|
|
29
|
+
*/
|
|
30
|
+
orUndefined(): ValueSchema<T | undefined>;
|
|
31
|
+
/**
|
|
32
|
+
* Returns a validator that asserts if this validator and the next passes.
|
|
33
|
+
*
|
|
34
|
+
* @see andValidators
|
|
35
|
+
*
|
|
36
|
+
* @param nextValidator The validator to run after the current.
|
|
37
|
+
* @return Returns a new ValueSchema intersecting this validator and the next.
|
|
38
|
+
*/
|
|
39
|
+
and<U>(nextValidator: Validator<U>): ValueSchema<T & U>;
|
|
40
|
+
/**
|
|
41
|
+
* Returns a validator that asserts if this validator or the next passes.
|
|
42
|
+
*
|
|
43
|
+
* Implementation note:
|
|
44
|
+
* subclasses should not change the schema instance returned unless all chainable validators
|
|
45
|
+
* accept input type 'unknown'.
|
|
46
|
+
*
|
|
47
|
+
* @param nextValidator The validator to run after the current.
|
|
48
|
+
* @return Returns a new ValueSchema providing the union of this validator and the next.
|
|
49
|
+
*/
|
|
50
|
+
or<U>(nextValidator: Validator<U, any>): ValueSchema<T | U>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=ValueSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValueSchema.d.ts","sourceRoot":"","sources":["../../../../src/validation/ValueSchema.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAS5C,eAAO,MAAM,eAAe;wBACjB,SAAS,CAAC,GAAG,CAAC;IAKrB;;OAEG;4BACQ,SAAS,CAAC,IAAI,GAAG,SAAS,CAAC;IAItC;;OAEG;yBACK,SAAS,CAAC,IAAI,CAAC;IAIvB;;OAEG;8BACU,SAAS,CAAC,SAAS,CAAC;CAG3B,CAAA;AAEV,qBAAa,WAAW,CAAC,CAAC,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IAC7C;;OAEG;IACH,KAAK,IAAI,WAAW,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,CAAC;IAI1C;;OAEG;IACH,MAAM,IAAI,WAAW,CAAC,CAAC,GAAG,IAAI,CAAC;IAI/B;;OAEG;IACH,WAAW,IAAI,WAAW,CAAC,CAAC,GAAG,SAAS,CAAC;IAIzC;;;;;;;OAOG;IACH,GAAG,CAAC,CAAC,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC;IAIvD;;;;;;;;;OASG;IACH,EAAE,CAAC,CAAC,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC;CAG9D"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { andValidators } from './andValidators';
|
|
6
|
+
import { orValidators } from './orValidators';
|
|
7
|
+
import { SchemaBase } from './SchemaBase';
|
|
8
|
+
import { createValidator } from './Validator';
|
|
9
|
+
const locale = {
|
|
10
|
+
null: 'null',
|
|
11
|
+
nullish: 'nullish',
|
|
12
|
+
undefined: 'undefined',
|
|
13
|
+
};
|
|
14
|
+
export const valueValidators = {
|
|
15
|
+
any() {
|
|
16
|
+
// no need for localization, validator cannot fail.
|
|
17
|
+
return createValidator('', () => true);
|
|
18
|
+
},
|
|
19
|
+
/**
|
|
20
|
+
* Creates a validator that asserts that the input is nullish.
|
|
21
|
+
*/
|
|
22
|
+
nullish() {
|
|
23
|
+
return createValidator(locale.nullish, (input) => input == null);
|
|
24
|
+
},
|
|
25
|
+
/**
|
|
26
|
+
* Creates a validator that asserts that the input is strictly null.
|
|
27
|
+
*/
|
|
28
|
+
null() {
|
|
29
|
+
return createValidator(locale.null, (input) => input === null);
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* Creates a validator that asserts that the input is strictly undefined.
|
|
33
|
+
*/
|
|
34
|
+
undefined() {
|
|
35
|
+
return createValidator(locale.undefined, (input) => input === undefined);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
export class ValueSchema extends SchemaBase {
|
|
39
|
+
/**
|
|
40
|
+
* Prepends this validator allowing for null or undefined.
|
|
41
|
+
*/
|
|
42
|
+
maybe() {
|
|
43
|
+
return this.or(valueValidators.nullish());
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Prepends this validator allowing for null.
|
|
47
|
+
*/
|
|
48
|
+
orNull() {
|
|
49
|
+
return this.or(valueValidators.null());
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Prepends this validator allowing for undefined.
|
|
53
|
+
*/
|
|
54
|
+
orUndefined() {
|
|
55
|
+
return this.or(valueValidators.undefined());
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns a validator that asserts if this validator and the next passes.
|
|
59
|
+
*
|
|
60
|
+
* @see andValidators
|
|
61
|
+
*
|
|
62
|
+
* @param nextValidator The validator to run after the current.
|
|
63
|
+
* @return Returns a new ValueSchema intersecting this validator and the next.
|
|
64
|
+
*/
|
|
65
|
+
and(nextValidator) {
|
|
66
|
+
return new ValueSchema(nextValidator, this, andValidators);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns a validator that asserts if this validator or the next passes.
|
|
70
|
+
*
|
|
71
|
+
* Implementation note:
|
|
72
|
+
* subclasses should not change the schema instance returned unless all chainable validators
|
|
73
|
+
* accept input type 'unknown'.
|
|
74
|
+
*
|
|
75
|
+
* @param nextValidator The validator to run after the current.
|
|
76
|
+
* @return Returns a new ValueSchema providing the union of this validator and the next.
|
|
77
|
+
*/
|
|
78
|
+
or(nextValidator) {
|
|
79
|
+
return new ValueSchema(nextValidator, this, orValidators);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* Validates that all the given validators pass.
|
|
4
|
+
*/
|
|
5
|
+
export declare function andValidators<A extends Input, B, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>): Validator<A & B, Input>;
|
|
6
|
+
/**
|
|
7
|
+
* Validates that all the given validators pass.
|
|
8
|
+
*/
|
|
9
|
+
export declare function andValidators<A extends Input, B, C, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>): Validator<A & B & C, Input>;
|
|
10
|
+
/**
|
|
11
|
+
* Validates that all the given validators pass.
|
|
12
|
+
*/
|
|
13
|
+
export declare function andValidators<A extends Input, B, C, D, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>): Validator<A & B & C & D, any>;
|
|
14
|
+
/**
|
|
15
|
+
* Validates that all the given validators pass.
|
|
16
|
+
*/
|
|
17
|
+
export declare function andValidators<A extends Input, B, C, D, E, Input>(validatorA: Validator<A, Input>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>, validatorE: Validator<E, any>): Validator<A & B & C & D & E, any>;
|
|
18
|
+
export declare function andValidators(...validators: Validator<any, any>[]): Validator<any, any>;
|
|
19
|
+
//# sourceMappingURL=andValidators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"andValidators.d.ts","sourceRoot":"","sources":["../../../../src/validation/andValidators.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAoC5C;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,EAAE,KAAK,EACnD,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,EAC/B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;AAE1B;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EACtD,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,EAC/B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;AAE9B;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EACzD,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,EAC/B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAEhC;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAC5D,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,EAC/B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAEpC,wBAAgB,aAAa,CACzB,GAAG,UAAU,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GACrC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
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 } from './Validator';
|
|
7
|
+
/*
|
|
8
|
+
* @brief
|
|
9
|
+
* andValidators will intersect an arbitrary number of validators.
|
|
10
|
+
*
|
|
11
|
+
* While in most cases validating that an input passes two validators, the asserted output is the
|
|
12
|
+
* intersection of those two validator outputs, this is not always the case.
|
|
13
|
+
* For example, take two validators with outputs `A[]` and `B[]`, their combined validated output
|
|
14
|
+
* type would be `(A & B)[]`, not `A[] & B[]`. Use casting when necessary.
|
|
15
|
+
*
|
|
16
|
+
* Also note that there is no contravariance safety with the chained validator inputs. That is,
|
|
17
|
+
* there's no compile-time checking that validatorB accepts an input that is the super of
|
|
18
|
+
* validatorA's output.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
const locale = {
|
|
24
|
+
/**
|
|
25
|
+
* {value} The child validators' descriptions, joined with `separator`.
|
|
26
|
+
*
|
|
27
|
+
* groupings of and validators is unimportant,
|
|
28
|
+
* and(and(a, b), c) is the same as and(a, and(b, c)), so `a & b & c` is an accurate
|
|
29
|
+
* representation for either.
|
|
30
|
+
*/
|
|
31
|
+
template: '{value}',
|
|
32
|
+
/**
|
|
33
|
+
* The separator with which to join child validator descriptions.
|
|
34
|
+
*/
|
|
35
|
+
separator: ' & ',
|
|
36
|
+
};
|
|
37
|
+
export function andValidators(...validators) {
|
|
38
|
+
return createDeepValidator(substitute(locale.template, {
|
|
39
|
+
value: validators.map((v) => v.description).join(locale.separator),
|
|
40
|
+
}), (input, options, path) => {
|
|
41
|
+
for (const validator of validators) {
|
|
42
|
+
const errors = validator.validate(input, options, path);
|
|
43
|
+
if (errors.length)
|
|
44
|
+
return errors;
|
|
45
|
+
}
|
|
46
|
+
return [];
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* Validates that one of the given validators pass.
|
|
4
|
+
*/
|
|
5
|
+
export declare function orValidators<A, B>(validatorA: Validator<A, any>, validatorB: Validator<B, any>): Validator<A | B, any>;
|
|
6
|
+
/**
|
|
7
|
+
* Validates that one of the given validators pass.
|
|
8
|
+
*/
|
|
9
|
+
export declare function orValidators<A, B, C>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>): Validator<A | B | C, any>;
|
|
10
|
+
/**
|
|
11
|
+
* Validates that one of the given validators pass.
|
|
12
|
+
*/
|
|
13
|
+
export declare function orValidators<A, B, C, D>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>): Validator<A | B | C | D, any>;
|
|
14
|
+
/**
|
|
15
|
+
* Validates that one of the given validators pass.
|
|
16
|
+
*/
|
|
17
|
+
export declare function orValidators<A, B, C, D, E>(validatorA: Validator<A, any>, validatorB: Validator<B, any>, validatorC: Validator<C, any>, validatorD: Validator<D, any>, validatorE: Validator<E, any>): Validator<A | B | C | D | E, any>;
|
|
18
|
+
export declare function orValidators(...validators: Validator<any, any>[]): Validator<any, any>;
|
|
19
|
+
//# sourceMappingURL=orValidators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orValidators.d.ts","sourceRoot":"","sources":["../../../../src/validation/orValidators.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAA0B,SAAS,EAAE,MAAM,aAAa,CAAA;AA+BpE;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAExB;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAChC,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAE5B;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACnC,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAEhC;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACtC,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAC7B,UAAU,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,GAC9B,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;AAEpC,wBAAgB,YAAY,CACxB,GAAG,UAAU,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GACrC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA"}
|
|
@@ -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, createValidationExpectationMessage, } from './Validator';
|
|
7
|
+
/*
|
|
8
|
+
* @brief
|
|
9
|
+
* orValidators will union an arbitrary number of validators.
|
|
10
|
+
* input type is always type any; it's not strictly necessary for all validators to accept the same
|
|
11
|
+
* input type. For example, if validatorA asserts that a value is not null, validatorB can safely
|
|
12
|
+
* assume its input is not null.
|
|
13
|
+
* output type will be the union of the validator outputs up to 5 validators, 6 or more will be
|
|
14
|
+
* type any.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
const locale = {
|
|
20
|
+
/**
|
|
21
|
+
* {value} - The child validators' descriptions, joined with `separator`.
|
|
22
|
+
*/
|
|
23
|
+
template: '({value})',
|
|
24
|
+
/**
|
|
25
|
+
* The separator with which to join child validator descriptions.
|
|
26
|
+
*/
|
|
27
|
+
separator: ' | ',
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Validates that one of the given validators pass.
|
|
31
|
+
*
|
|
32
|
+
* If all validators fail, the validator that produced an error at the deepest path will have its
|
|
33
|
+
* error message bubbled. If all validators failed at the current depth, a concatenation of
|
|
34
|
+
* validator descriptions will be used.
|
|
35
|
+
*
|
|
36
|
+
* For example:
|
|
37
|
+
* `orValidators(number(), string()).validate(false)` will produce the message:
|
|
38
|
+
* 'Expected (number | string), but was false, At: '
|
|
39
|
+
*
|
|
40
|
+
* And:
|
|
41
|
+
* `orValidators(number(), object({ childProp: number() })).validate({ childProp: 'not-a-number' })`
|
|
42
|
+
* will produce:
|
|
43
|
+
* 'Expected number, but was "not-a-number", At: childProp'
|
|
44
|
+
*
|
|
45
|
+
* @param validators The validators to test.
|
|
46
|
+
*/
|
|
47
|
+
export function orValidators(...validators) {
|
|
48
|
+
const description = substitute(locale.template, {
|
|
49
|
+
value: validators.map((v) => v.description).join(locale.separator),
|
|
50
|
+
});
|
|
51
|
+
return createDeepValidator(description, (input, options, path) => {
|
|
52
|
+
let longestErrors = [];
|
|
53
|
+
let longestPath = 0;
|
|
54
|
+
for (const validator of validators) {
|
|
55
|
+
const errors = validator.validate(input, options, path);
|
|
56
|
+
if (!errors.length)
|
|
57
|
+
return errors;
|
|
58
|
+
// Take the deepest error to bubble.
|
|
59
|
+
const pathLength = errors.reduce((max, m) => Math.max(m.path.length, max), 0);
|
|
60
|
+
if (pathLength >= longestPath) {
|
|
61
|
+
longestErrors = errors;
|
|
62
|
+
longestPath = pathLength;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return longestPath > path.length
|
|
66
|
+
? longestErrors
|
|
67
|
+
: [
|
|
68
|
+
{
|
|
69
|
+
message: createValidationExpectationMessage(description, input, path),
|
|
70
|
+
path,
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Validator } from './Validator';
|
|
2
|
+
/**
|
|
3
|
+
* Validators asserting typeof input matches the expected value.
|
|
4
|
+
*
|
|
5
|
+
* Note: bigint is omitted; not all supported platforms can use BigInt.
|
|
6
|
+
*/
|
|
7
|
+
export declare const typeOfValidators: {
|
|
8
|
+
readonly boolean: Validator<boolean, unknown>;
|
|
9
|
+
readonly func: Validator<import("@amazon/vinyl-util").Fun, unknown>;
|
|
10
|
+
readonly number: Validator<number, unknown>;
|
|
11
|
+
readonly object: Validator<object, unknown>;
|
|
12
|
+
readonly string: Validator<string, unknown>;
|
|
13
|
+
readonly symbol: Validator<symbol, unknown>;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=typeOfValidators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeOfValidators.d.ts","sourceRoot":"","sources":["../../../../src/validation/typeOfValidators.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAyC5C;;;;GAIG;AACH,eAAO,MAAM,gBAAgB;;;;;;;CAmBnB,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { createValidator } from './Validator';
|
|
7
|
+
/**
|
|
8
|
+
* @private
|
|
9
|
+
*/
|
|
10
|
+
const locale = {
|
|
11
|
+
template: 'type {value}',
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a validator that validates that the input is not nullish and its `typeof` matches the
|
|
15
|
+
* expected value.
|
|
16
|
+
*
|
|
17
|
+
* Note: while `typeof null === 'object'`, `typeOfValidator('object')` will assert that its
|
|
18
|
+
* input is not nullish.
|
|
19
|
+
*
|
|
20
|
+
* @param expected
|
|
21
|
+
*/
|
|
22
|
+
function typeOfValidator(expected) {
|
|
23
|
+
return createValidator(substitute(locale.template, { value: expected }), (input) => {
|
|
24
|
+
return input != null && typeof input === expected;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const typeOfValidatorCache = {};
|
|
28
|
+
function cachedTypeOfValidator(type) {
|
|
29
|
+
if (!(type in typeOfValidatorCache)) {
|
|
30
|
+
typeOfValidatorCache[type] = typeOfValidator(type);
|
|
31
|
+
}
|
|
32
|
+
return typeOfValidatorCache[type];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Validators asserting typeof input matches the expected value.
|
|
36
|
+
*
|
|
37
|
+
* Note: bigint is omitted; not all supported platforms can use BigInt.
|
|
38
|
+
*/
|
|
39
|
+
export const typeOfValidators = {
|
|
40
|
+
get boolean() {
|
|
41
|
+
return cachedTypeOfValidator('boolean');
|
|
42
|
+
},
|
|
43
|
+
get func() {
|
|
44
|
+
return cachedTypeOfValidator('function');
|
|
45
|
+
},
|
|
46
|
+
get number() {
|
|
47
|
+
return cachedTypeOfValidator('number');
|
|
48
|
+
},
|
|
49
|
+
get object() {
|
|
50
|
+
return cachedTypeOfValidator('object');
|
|
51
|
+
},
|
|
52
|
+
get string() {
|
|
53
|
+
return cachedTypeOfValidator('string');
|
|
54
|
+
},
|
|
55
|
+
get symbol() {
|
|
56
|
+
return cachedTypeOfValidator('symbol');
|
|
57
|
+
},
|
|
58
|
+
};
|