@fgv/ts-utils 5.0.0-12 → 5.0.0-14
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/.vscode/launch.json +16 -0
- package/.vscode/settings.json +36 -0
- package/config/api-extractor.json +343 -0
- package/config/rig.json +16 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +52 -0
- package/lib/test/helpers/jest/helpers/fsHelpers.js +180 -0
- package/lib/test/helpers/jest/helpers/index.d.ts +2 -0
- package/lib/test/helpers/jest/helpers/index.js +18 -0
- package/lib/test/helpers/jest/index.d.ts +3 -0
- package/lib/test/helpers/jest/index.js +27 -0
- package/lib/test/helpers/jest/matchers/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/index.js +19 -0
- package/lib/test/helpers/jest/matchers/toFail/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toFail/index.js +25 -0
- package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFail/predicate.js +9 -0
- package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toFailTest/index.js +30 -0
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +3 -0
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +12 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +15 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +31 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +30 -0
- package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toFailWith/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +20 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +19 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toSucceed/index.js +25 -0
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +9 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +19 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +55 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +21 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +16 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +21 -0
- package/lib/test/helpers/jest/resolvers/cli.d.ts +5 -0
- package/lib/test/helpers/jest/resolvers/cli.js +48 -0
- package/lib/test/helpers/jest/resolvers/ide.d.ts +5 -0
- package/lib/test/helpers/jest/resolvers/ide.js +48 -0
- package/lib/test/helpers/jest/ts-utils.d.ts +2 -0
- package/lib/test/helpers/jest/ts-utils.js +18 -0
- package/lib/test/helpers/jest/types/index.d.ts +79 -0
- package/lib/test/helpers/jest/types/index.js +4 -0
- package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +6 -0
- package/lib/test/helpers/jest/utils/matcherHelpers.js +53 -0
- package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +4 -0
- package/lib/test/helpers/jest/utils/snapshotResolver.js +19 -0
- package/lib/test/unit/collections/collectible.test.d.ts +2 -0
- package/lib/test/unit/collections/collector.test.d.ts +2 -0
- package/lib/test/unit/collections/collectors.test.d.ts +2 -0
- package/lib/test/unit/collections/convertingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/helpers.d.ts +44 -0
- package/lib/test/unit/collections/helpers.js +115 -0
- package/lib/test/unit/collections/resultMap.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingResultMap.test.d.ts +2 -0
- package/lib/test/unit/converter.test.d.ts +2 -0
- package/lib/test/unit/converters.test.d.ts +2 -0
- package/lib/test/unit/defaultingConverter.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fileItem.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fileTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fsTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/sample/rootFile.json +1 -0
- package/lib/test/unit/file-tree/sample/some/other/path/file2.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/below/file4.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/file1.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/file3.json +1 -0
- package/lib/test/unit/hash.test.d.ts +2 -0
- package/lib/test/unit/logger.test.d.ts +2 -0
- package/lib/test/unit/mapResults.test.d.ts +3 -0
- package/lib/test/unit/messageAggregator.test.d.ts +3 -0
- package/lib/test/unit/normalize.test.d.ts +2 -0
- package/lib/test/unit/result.test.d.ts +3 -0
- package/lib/test/unit/utils.test.d.ts +3 -0
- package/lib/test/unit/validation/array.test.d.ts +2 -0
- package/lib/test/unit/validation/boolean.test.d.ts +2 -0
- package/lib/test/unit/validation/genericValidator.test.d.ts +2 -0
- package/lib/test/unit/validation/number.test.d.ts +2 -0
- package/lib/test/unit/validation/object.test.d.ts +2 -0
- package/lib/test/unit/validation/oneOf.test.d.ts +2 -0
- package/lib/test/unit/validation/recordOf.test.d.ts +2 -0
- package/lib/test/unit/validation/string.test.d.ts +2 -0
- package/lib/test/unit/validation/traits.test.d.ts +2 -0
- package/lib/test/unit/validation/typeGuard.test.d.ts +2 -0
- package/lib/test/unit/validation/validators.test.d.ts +2 -0
- package/package.json +9 -9
- package/src/index.ts +63 -0
- package/src/packlets/base/brand.ts +28 -0
- package/src/packlets/base/index.ts +31 -0
- package/src/packlets/base/logger.ts +156 -0
- package/src/packlets/base/mapResults.ts +302 -0
- package/src/packlets/base/messageAggregator.ts +120 -0
- package/src/packlets/base/normalize.ts +144 -0
- package/src/packlets/base/result.ts +961 -0
- package/src/packlets/base/utils.ts +236 -0
- package/src/packlets/collections/collectible.ts +238 -0
- package/src/packlets/collections/collector.ts +273 -0
- package/src/packlets/collections/collectorValidator.ts +178 -0
- package/src/packlets/collections/common.ts +27 -0
- package/src/packlets/collections/convertingCollector.ts +223 -0
- package/src/packlets/collections/convertingCollectorValidator.ts +164 -0
- package/src/packlets/collections/index.ts +39 -0
- package/src/packlets/collections/keyValueConverters.ts +142 -0
- package/src/packlets/collections/readonlyResultMap.ts +99 -0
- package/src/packlets/collections/resultMap.ts +322 -0
- package/src/packlets/collections/resultMapValidator.ts +188 -0
- package/src/packlets/collections/utils.ts +33 -0
- package/src/packlets/collections/validatingCollector.ts +123 -0
- package/src/packlets/collections/validatingConvertingCollector.ts +113 -0
- package/src/packlets/collections/validatingResultMap.ts +94 -0
- package/src/packlets/conversion/baseConverter.ts +330 -0
- package/src/packlets/conversion/converter.ts +260 -0
- package/src/packlets/conversion/converters.ts +1039 -0
- package/src/packlets/conversion/defaultingConverter.ts +188 -0
- package/src/packlets/conversion/index.ts +30 -0
- package/src/packlets/conversion/objectConverter.ts +259 -0
- package/src/packlets/conversion/stringConverter.ts +165 -0
- package/src/packlets/file-tree/directoryItem.ts +84 -0
- package/src/packlets/file-tree/fileItem.ts +119 -0
- package/src/packlets/file-tree/fileTree.ts +152 -0
- package/src/packlets/file-tree/fileTreeAccessors.ts +176 -0
- package/src/packlets/file-tree/fsTree.ts +118 -0
- package/src/packlets/file-tree/in-memory/inMemoryTree.ts +171 -0
- package/src/packlets/file-tree/in-memory/index.ts +23 -0
- package/src/packlets/file-tree/in-memory/treeBuilder.ts +203 -0
- package/src/packlets/file-tree/index.ts +31 -0
- package/src/packlets/hash/crcNormalizer.ts +81 -0
- package/src/packlets/hash/hashingNormalizer.ts +102 -0
- package/src/packlets/hash/index.ts +24 -0
- package/src/packlets/validation/array.ts +83 -0
- package/src/packlets/validation/boolean.ts +63 -0
- package/src/packlets/validation/classes.ts +38 -0
- package/src/packlets/validation/common.ts +38 -0
- package/src/packlets/validation/field.ts +99 -0
- package/src/packlets/validation/genericValidator.ts +205 -0
- package/src/packlets/validation/index.ts +31 -0
- package/src/packlets/validation/number.ts +66 -0
- package/src/packlets/validation/object.ts +208 -0
- package/src/packlets/validation/oneOf.ts +77 -0
- package/src/packlets/validation/string.ts +66 -0
- package/src/packlets/validation/traits.ts +113 -0
- package/src/packlets/validation/typeGuard.ts +82 -0
- package/src/packlets/validation/validator.ts +145 -0
- package/src/packlets/validation/validatorBase.ts +61 -0
- package/src/packlets/validation/validators.ts +246 -0
- package/CHANGELOG.md +0 -135
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/base/brand.d.ts.map +0 -1
- package/lib/packlets/base/brand.js.map +0 -1
- package/lib/packlets/base/index.d.ts.map +0 -1
- package/lib/packlets/base/index.js.map +0 -1
- package/lib/packlets/base/logger.d.ts.map +0 -1
- package/lib/packlets/base/logger.js.map +0 -1
- package/lib/packlets/base/mapResults.d.ts.map +0 -1
- package/lib/packlets/base/mapResults.js.map +0 -1
- package/lib/packlets/base/messageAggregator.d.ts.map +0 -1
- package/lib/packlets/base/messageAggregator.js.map +0 -1
- package/lib/packlets/base/normalize.d.ts.map +0 -1
- package/lib/packlets/base/normalize.js.map +0 -1
- package/lib/packlets/base/result.d.ts.map +0 -1
- package/lib/packlets/base/result.js.map +0 -1
- package/lib/packlets/base/utils.d.ts.map +0 -1
- package/lib/packlets/base/utils.js.map +0 -1
- package/lib/packlets/collections/collectible.d.ts.map +0 -1
- package/lib/packlets/collections/collectible.js.map +0 -1
- package/lib/packlets/collections/collector.d.ts.map +0 -1
- package/lib/packlets/collections/collector.js.map +0 -1
- package/lib/packlets/collections/collectorValidator.d.ts.map +0 -1
- package/lib/packlets/collections/collectorValidator.js.map +0 -1
- package/lib/packlets/collections/common.d.ts.map +0 -1
- package/lib/packlets/collections/common.js.map +0 -1
- package/lib/packlets/collections/convertingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/convertingCollector.js.map +0 -1
- package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +0 -1
- package/lib/packlets/collections/convertingCollectorValidator.js.map +0 -1
- package/lib/packlets/collections/index.d.ts.map +0 -1
- package/lib/packlets/collections/index.js.map +0 -1
- package/lib/packlets/collections/keyValueConverters.d.ts.map +0 -1
- package/lib/packlets/collections/keyValueConverters.js.map +0 -1
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/readonlyResultMap.js.map +0 -1
- package/lib/packlets/collections/resultMap.d.ts.map +0 -1
- package/lib/packlets/collections/resultMap.js.map +0 -1
- package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
- package/lib/packlets/collections/resultMapValidator.js.map +0 -1
- package/lib/packlets/collections/utils.d.ts.map +0 -1
- package/lib/packlets/collections/utils.js.map +0 -1
- package/lib/packlets/collections/validatingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/validatingCollector.js.map +0 -1
- package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/validatingConvertingCollector.js.map +0 -1
- package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/validatingResultMap.js.map +0 -1
- package/lib/packlets/conversion/baseConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/baseConverter.js.map +0 -1
- package/lib/packlets/conversion/converter.d.ts.map +0 -1
- package/lib/packlets/conversion/converter.js.map +0 -1
- package/lib/packlets/conversion/converters.d.ts.map +0 -1
- package/lib/packlets/conversion/converters.js.map +0 -1
- package/lib/packlets/conversion/defaultingConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/defaultingConverter.js.map +0 -1
- package/lib/packlets/conversion/index.d.ts.map +0 -1
- package/lib/packlets/conversion/index.js.map +0 -1
- package/lib/packlets/conversion/objectConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/objectConverter.js.map +0 -1
- package/lib/packlets/conversion/stringConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/stringConverter.js.map +0 -1
- package/lib/packlets/file-tree/directoryItem.d.ts.map +0 -1
- package/lib/packlets/file-tree/directoryItem.js.map +0 -1
- package/lib/packlets/file-tree/fileItem.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileItem.js.map +0 -1
- package/lib/packlets/file-tree/fileTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileTree.js.map +0 -1
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileTreeAccessors.js.map +0 -1
- package/lib/packlets/file-tree/fsTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/fsTree.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/index.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/index.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +0 -1
- package/lib/packlets/file-tree/index.d.ts.map +0 -1
- package/lib/packlets/file-tree/index.js.map +0 -1
- package/lib/packlets/hash/crcNormalizer.d.ts.map +0 -1
- package/lib/packlets/hash/crcNormalizer.js.map +0 -1
- package/lib/packlets/hash/hashingNormalizer.d.ts.map +0 -1
- package/lib/packlets/hash/hashingNormalizer.js.map +0 -1
- package/lib/packlets/hash/index.d.ts.map +0 -1
- package/lib/packlets/hash/index.js.map +0 -1
- package/lib/packlets/validation/array.d.ts.map +0 -1
- package/lib/packlets/validation/array.js.map +0 -1
- package/lib/packlets/validation/boolean.d.ts.map +0 -1
- package/lib/packlets/validation/boolean.js.map +0 -1
- package/lib/packlets/validation/classes.d.ts.map +0 -1
- package/lib/packlets/validation/classes.js.map +0 -1
- package/lib/packlets/validation/common.d.ts.map +0 -1
- package/lib/packlets/validation/common.js.map +0 -1
- package/lib/packlets/validation/field.d.ts.map +0 -1
- package/lib/packlets/validation/field.js.map +0 -1
- package/lib/packlets/validation/genericValidator.d.ts.map +0 -1
- package/lib/packlets/validation/genericValidator.js.map +0 -1
- package/lib/packlets/validation/index.d.ts.map +0 -1
- package/lib/packlets/validation/index.js.map +0 -1
- package/lib/packlets/validation/number.d.ts.map +0 -1
- package/lib/packlets/validation/number.js.map +0 -1
- package/lib/packlets/validation/object.d.ts.map +0 -1
- package/lib/packlets/validation/object.js.map +0 -1
- package/lib/packlets/validation/oneOf.d.ts.map +0 -1
- package/lib/packlets/validation/oneOf.js.map +0 -1
- package/lib/packlets/validation/string.d.ts.map +0 -1
- package/lib/packlets/validation/string.js.map +0 -1
- package/lib/packlets/validation/traits.d.ts.map +0 -1
- package/lib/packlets/validation/traits.js.map +0 -1
- package/lib/packlets/validation/typeGuard.d.ts.map +0 -1
- package/lib/packlets/validation/typeGuard.js.map +0 -1
- package/lib/packlets/validation/validator.d.ts.map +0 -1
- package/lib/packlets/validation/validator.js.map +0 -1
- package/lib/packlets/validation/validatorBase.d.ts.map +0 -1
- package/lib/packlets/validation/validatorBase.js.map +0 -1
- package/lib/packlets/validation/validators.d.ts.map +0 -1
- package/lib/packlets/validation/validators.js.map +0 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Failure, fail } from '../base';
|
|
24
|
+
import { Validator, ValidatorOptions } from './validator';
|
|
25
|
+
import { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Parameters used to construct a {@link Validation.Classes.ArrayValidator | ArrayValidator}.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
32
|
+
export interface ArrayValidatorConstructorParams<T, TC = unknown>
|
|
33
|
+
extends ValidatorBaseConstructorParams<T[], TC> {
|
|
34
|
+
validateElement: Validator<T, TC>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* An in-place {@link Validator | Validator} for arrays of validated
|
|
39
|
+
* values or objects.
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export class ArrayValidator<T, TC = unknown> extends ValidatorBase<T[], TC> {
|
|
43
|
+
/**
|
|
44
|
+
* {@link Validation.ValidatorOptions | Options} which apply to this
|
|
45
|
+
* validator.
|
|
46
|
+
*/
|
|
47
|
+
public readonly options: ValidatorOptions<TC>;
|
|
48
|
+
|
|
49
|
+
protected readonly _validateElement: Validator<T, TC>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Constructs a new {@link Validation.Classes.ArrayValidator | ArrayValidator}.
|
|
53
|
+
* @param params - Optional {@link Validation.Classes.ArrayValidatorConstructorParams | init params} for the
|
|
54
|
+
* new {@link Validation.Classes.ArrayValidator | ArrayValidator}.
|
|
55
|
+
*/
|
|
56
|
+
public constructor(params: ArrayValidatorConstructorParams<T, TC>) {
|
|
57
|
+
super(params);
|
|
58
|
+
this._validateElement = params.validateElement;
|
|
59
|
+
this.options = params.options ?? {};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Static method which validates that a supplied `unknown` value is a `array`
|
|
64
|
+
* and that every element of the array can be validated by the supplied array
|
|
65
|
+
* validator.
|
|
66
|
+
* @param from - The `unknown` value to be tested.
|
|
67
|
+
* @param context - Optional validation context will be propagated to element validator.
|
|
68
|
+
* @returns Returns `true` if `from` is an `array` of valid elements, or
|
|
69
|
+
* {@link Failure} with an error message if not.
|
|
70
|
+
*/
|
|
71
|
+
protected _validate<T>(from: unknown, context?: TC): boolean | Failure<T> {
|
|
72
|
+
if (Array.isArray(from)) {
|
|
73
|
+
for (const elem of from) {
|
|
74
|
+
const result = this._validateElement.validate(elem, context);
|
|
75
|
+
if (!result.isSuccess()) {
|
|
76
|
+
return fail(result.message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return fail<T>(`"${from}": not an array`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Failure, fail } from '../base';
|
|
24
|
+
import { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parameters used to construct a {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export type BooleanValidatorConstructorParams<TC = unknown> = GenericValidatorConstructorParams<boolean, TC>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An in-place {@link Validation.Validator | Validator} for `boolean` values.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export class BooleanValidator<TC = unknown> extends GenericValidator<boolean, TC> {
|
|
37
|
+
/**
|
|
38
|
+
* Constructs a new {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
39
|
+
* @param params - Optional {@link Validation.Classes.BooleanValidatorConstructorParams | init params} for the
|
|
40
|
+
* new {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
41
|
+
*/
|
|
42
|
+
public constructor(params?: BooleanValidatorConstructorParams<TC>) {
|
|
43
|
+
/* c8 ignore next */
|
|
44
|
+
params = params ?? {};
|
|
45
|
+
super({
|
|
46
|
+
validator: BooleanValidator.validateBoolean,
|
|
47
|
+
...params
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Static method which validates that a supplied `unknown` value is a `boolean`.
|
|
53
|
+
* @param from - The `unknown` value to be tested.
|
|
54
|
+
* @returns Returns `true` if `from` is a `boolean`, or {@link Failure} with an error
|
|
55
|
+
* message if not.
|
|
56
|
+
*/
|
|
57
|
+
public static validateBoolean(from: unknown): boolean | Failure<boolean> {
|
|
58
|
+
if (typeof from === 'boolean') {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return fail(`"${from}": not a boolean`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/* c8 ignore start */
|
|
24
|
+
|
|
25
|
+
export { ArrayValidator, ArrayValidatorConstructorParams } from './array';
|
|
26
|
+
export { BooleanValidator, BooleanValidatorConstructorParams } from './boolean';
|
|
27
|
+
export { NumberValidator, NumberValidatorConstructorParams } from './number';
|
|
28
|
+
export {
|
|
29
|
+
FieldValidators,
|
|
30
|
+
ObjectValidator,
|
|
31
|
+
ObjectValidatorConstructorParams,
|
|
32
|
+
ObjectValidatorOptions
|
|
33
|
+
} from './object';
|
|
34
|
+
export { OneOfValidator, OneOfValidatorConstructorParams } from './oneOf';
|
|
35
|
+
export { StringValidator, StringValidatorConstructorParams } from './string';
|
|
36
|
+
export { TypeGuardValidator, TypeGuardValidatorConstructorParams } from './typeGuard';
|
|
37
|
+
|
|
38
|
+
/* c8 ignore stop */
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Failure } from '../base';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A type guard function which validates a specific type, with an optional context
|
|
27
|
+
* that can be used to shape the validation.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export type TypeGuardWithContext<T, TC = unknown> = (from: unknown, context?: TC) => from is T;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Type for a validation function, which validates that a supplied `unknown`
|
|
34
|
+
* value is a valid value of type `<T>`, possibly as influenced by
|
|
35
|
+
* an optionally-supplied validation context of type `<TC>`.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Failure, fail, isKeyOf } from '../base';
|
|
24
|
+
import { Validator, ValidatorOptions } from './validator';
|
|
25
|
+
import { ValidatorBase } from './validatorBase';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Parameters used to construct a {@link Validation.FieldValidator | FieldValidator}.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
32
|
+
export interface FieldValidatorOptions<TC> extends ValidatorOptions<TC> {
|
|
33
|
+
optional?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An in-place {@link Validation.Validator | Validator} for properties
|
|
38
|
+
* an an object.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export class FieldValidator<T, TC = unknown> extends ValidatorBase<T, TC> {
|
|
42
|
+
/**
|
|
43
|
+
* The name of the property that this validator should validate.
|
|
44
|
+
*/
|
|
45
|
+
public readonly fieldName: string;
|
|
46
|
+
/**
|
|
47
|
+
* The {@link Validation.Validator | Validator} to be applied against the named property.
|
|
48
|
+
*/
|
|
49
|
+
public readonly fieldValidator: Validator<T, TC>;
|
|
50
|
+
/**
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
protected readonly _fieldOptions: FieldValidatorOptions<TC>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Constructs a new {@link Validation.FieldValidator | FieldValidator.}.
|
|
57
|
+
* @param fieldName - The name of the property that this validator should validate.
|
|
58
|
+
* @param fieldValidator - The {@link Validation.Validator | Validator} to be applied
|
|
59
|
+
* against the named property.
|
|
60
|
+
* @param options - Additional {@link Validation.FieldValidatorOptions | options} to be
|
|
61
|
+
* applied to this validation.
|
|
62
|
+
*/
|
|
63
|
+
public constructor(
|
|
64
|
+
fieldName: string,
|
|
65
|
+
fieldValidator: Validator<T, TC>,
|
|
66
|
+
options?: FieldValidatorOptions<TC>
|
|
67
|
+
) {
|
|
68
|
+
super({ options });
|
|
69
|
+
this.fieldName = fieldName;
|
|
70
|
+
this.fieldValidator = fieldValidator;
|
|
71
|
+
/* c8 ignore next */
|
|
72
|
+
this._fieldOptions = options ?? {};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* {@inheritdoc Validation.ValidatorBase._validate}
|
|
77
|
+
*/
|
|
78
|
+
protected _validate(from: unknown, context?: TC): boolean | Failure<T> {
|
|
79
|
+
if (typeof from === 'object' && !Array.isArray(from) && from !== null) {
|
|
80
|
+
const optional = this._fieldOptions.optional === true;
|
|
81
|
+
|
|
82
|
+
if (isKeyOf(this.fieldName, from)) {
|
|
83
|
+
if (!optional || from[this.fieldName] !== undefined) {
|
|
84
|
+
const result = this.fieldValidator
|
|
85
|
+
.validate(from[this.fieldName], context)
|
|
86
|
+
.onFailure((message: string) => {
|
|
87
|
+
return fail(`${this.fieldName}: ${message}`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return result.success ? true : result;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return optional ? true : fail(`"${this.fieldName}": Field not found in "${JSON.stringify(from)}`);
|
|
95
|
+
}
|
|
96
|
+
/* c8 ignore next 2 -- defense in depth */
|
|
97
|
+
return fail(`Cannot validate field '${this.fieldName}' from non-object "${JSON.stringify(from)}"`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Brand, Failure, Result, fail, succeed } from '../base';
|
|
24
|
+
import { ConstraintTrait, ValidatorTraits } from './traits';
|
|
25
|
+
import { Constraint, ValidationErrorFormatter, Validator, ValidatorOptions } from './validator';
|
|
26
|
+
|
|
27
|
+
import { ValidatorFunc } from './common';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use {@link Validation.Common.ValidatorFunc | Validation.Common.ValidatorFunc} instead.
|
|
31
|
+
*/
|
|
32
|
+
export { ValidatorFunc };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
39
|
+
export interface GenericValidatorConstructorParams<T, TC> {
|
|
40
|
+
options?: ValidatorOptions<TC>;
|
|
41
|
+
traits?: Partial<ValidatorTraits>;
|
|
42
|
+
validator?: ValidatorFunc<T, TC>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generic base implementation for an in-place {@link Validation.Validator | Validator}.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export class GenericValidator<T, TC = unknown> implements Validator<T, TC> {
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritdoc Validation.Validator.traits}
|
|
52
|
+
*/
|
|
53
|
+
public readonly traits: ValidatorTraits;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
protected readonly _validator: ValidatorFunc<T, TC>;
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
protected readonly _options: ValidatorOptions<TC>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.
|
|
66
|
+
* @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}
|
|
67
|
+
* used to configure validation.
|
|
68
|
+
*/
|
|
69
|
+
public constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>) {
|
|
70
|
+
if (!params.validator) {
|
|
71
|
+
throw new Error('No validator function supplied');
|
|
72
|
+
}
|
|
73
|
+
this._validator = params.validator;
|
|
74
|
+
this._options = params.options ?? {};
|
|
75
|
+
this.traits = new ValidatorTraits(params.traits);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* {@inheritdoc Validation.Validator.isOptional}
|
|
80
|
+
*/
|
|
81
|
+
public get isOptional(): boolean {
|
|
82
|
+
return this.traits.isOptional;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* {@inheritdoc Validation.Validator.brand}
|
|
87
|
+
*/
|
|
88
|
+
public get brand(): string | undefined {
|
|
89
|
+
return this.traits.brand;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* {@inheritdoc Validation.Validator.validate}
|
|
94
|
+
*/
|
|
95
|
+
public validate(from: unknown, context?: TC): Result<T> {
|
|
96
|
+
const result = this._validator(from, this._context(context));
|
|
97
|
+
if (typeof result === 'boolean') {
|
|
98
|
+
return result ? succeed(from as T) : fail<T>('Invalid value');
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* {@inheritdoc Validation.Validator.convert}
|
|
105
|
+
*/
|
|
106
|
+
public convert(from: unknown, context?: TC): Result<T> {
|
|
107
|
+
const result = this._validator(from, this._context(context));
|
|
108
|
+
if (typeof result === 'boolean') {
|
|
109
|
+
return result ? succeed(from as T) : fail<T>('Invalid value');
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* {@inheritdoc Validation.Validator.validateOptional}
|
|
116
|
+
*/
|
|
117
|
+
public validateOptional(from: unknown, context?: TC): Result<T | undefined> {
|
|
118
|
+
return from === undefined ? succeed(undefined) : this.validate(from, context);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* {@inheritdoc Validation.Validator.guard}
|
|
123
|
+
*/
|
|
124
|
+
public guard(from: unknown, context?: TC): from is T {
|
|
125
|
+
return this._validator(from, this._context(context)) === true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* {@inheritdoc Validation.Validator.optional}
|
|
130
|
+
*/
|
|
131
|
+
public optional(): Validator<T | undefined, TC> {
|
|
132
|
+
return new GenericValidator({
|
|
133
|
+
validator: (from: unknown, context?: TC) => {
|
|
134
|
+
return from === undefined || this._validator(from, this._context(context));
|
|
135
|
+
},
|
|
136
|
+
traits: { isOptional: true }
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* {@inheritdoc Validation.Validator.withConstraint}
|
|
142
|
+
*/
|
|
143
|
+
public withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC> {
|
|
144
|
+
trait = trait ?? { type: 'function' };
|
|
145
|
+
return new GenericValidator({
|
|
146
|
+
validator: (from: unknown, context?: TC): boolean | Failure<T> => {
|
|
147
|
+
const result = this._validator(from, this._context(context));
|
|
148
|
+
if (result === true) {
|
|
149
|
+
const constraintResult = constraint(from as T);
|
|
150
|
+
if (typeof constraintResult === 'boolean') {
|
|
151
|
+
return constraintResult
|
|
152
|
+
? true
|
|
153
|
+
: fail(`Invalid value "${JSON.stringify(from)}": does not meet constraint.`);
|
|
154
|
+
}
|
|
155
|
+
return constraintResult;
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
},
|
|
159
|
+
traits: { constraints: [trait] }
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* {@inheritdoc Validation.Validator.brand}
|
|
165
|
+
*/
|
|
166
|
+
public withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC> {
|
|
167
|
+
if (this.brand) {
|
|
168
|
+
throw new Error(`Cannot replace existing brand "${this.brand}" with "${brand}".`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return new GenericValidator<Brand<T, B>, TC>({
|
|
172
|
+
validator: (from: unknown, context?: TC) => {
|
|
173
|
+
return this._validator(from, this._context(context)) as boolean | Failure<Brand<T, B>>;
|
|
174
|
+
},
|
|
175
|
+
traits: { brand }
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* {@inheritdoc Validation.Validator.withFormattedError}
|
|
181
|
+
*/
|
|
182
|
+
public withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC> {
|
|
183
|
+
return new GenericValidator<T, TC>({
|
|
184
|
+
validator: (from: unknown, context?: TC) => {
|
|
185
|
+
const result = this._validator(from, this._context(context));
|
|
186
|
+
if (result === true) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
/* c8 ignore next - defense in depth */
|
|
190
|
+
const message = result === false ? undefined : result.message;
|
|
191
|
+
return fail(formatter(from, message, this._context(context)));
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Gets a default or explicit context.
|
|
198
|
+
* @param explicitContext - Optional explicit context.
|
|
199
|
+
* @returns The appropriate context to use.
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
protected _context(explicitContext?: TC): TC | undefined {
|
|
203
|
+
return explicitContext ?? this._options.defaultContext;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export * from './common';
|
|
24
|
+
export * from './traits';
|
|
25
|
+
export * from './validator';
|
|
26
|
+
|
|
27
|
+
import * as Classes from './classes';
|
|
28
|
+
import * as Base from './genericValidator';
|
|
29
|
+
import * as Validators from './validators';
|
|
30
|
+
|
|
31
|
+
export { Base, Classes, Validators };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Failure, fail } from '../base';
|
|
24
|
+
import { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parameters used to construct a {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export type NumberValidatorConstructorParams<
|
|
31
|
+
T extends number = number,
|
|
32
|
+
TC = unknown
|
|
33
|
+
> = GenericValidatorConstructorParams<T, TC>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An in-place {@link Validation.Validator | Validator} for `number` values.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export class NumberValidator<T extends number = number, TC = unknown> extends GenericValidator<T, TC> {
|
|
40
|
+
/**
|
|
41
|
+
* Constructs a new {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
42
|
+
* @param params - Optional {@link Validation.Classes.NumberValidatorConstructorParams | init params} for the
|
|
43
|
+
* new {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
44
|
+
*/
|
|
45
|
+
public constructor(params?: NumberValidatorConstructorParams<T, TC>) {
|
|
46
|
+
/* c8 ignore next 1 */
|
|
47
|
+
params = params ?? {};
|
|
48
|
+
super({
|
|
49
|
+
validator: NumberValidator.validateNumber,
|
|
50
|
+
...params
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Static method which validates that a supplied `unknown` value is a `number`.
|
|
56
|
+
* @param from - The `unknown` value to be tested.
|
|
57
|
+
* @returns Returns `true` if `from` is a `number`, or {@link Failure} with an error
|
|
58
|
+
* message if not.
|
|
59
|
+
*/
|
|
60
|
+
public static validateNumber<T extends number>(from: unknown): boolean | Failure<T> {
|
|
61
|
+
if (typeof from === 'number') {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return fail<T>(`"${from}": not a number`);
|
|
65
|
+
}
|
|
66
|
+
}
|