@fgv/ts-utils 5.0.0-2 → 5.0.0-20
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/ts-utils.d.ts +20 -8
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/base/result.d.ts +8 -0
- package/lib/packlets/base/result.js +11 -0
- package/lib/packlets/hash/crcNormalizer.js +3 -1
- package/lib/packlets/validation/array.d.ts +2 -1
- package/lib/packlets/validation/array.js +2 -1
- package/lib/packlets/validation/common.d.ts +0 -8
- package/lib/packlets/validation/field.d.ts +1 -1
- package/lib/packlets/validation/field.js +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +1 -6
- package/lib/packlets/validation/genericValidator.js +11 -11
- package/lib/packlets/validation/object.d.ts +1 -1
- package/lib/packlets/validation/object.js +1 -1
- package/lib/packlets/validation/oneOf.d.ts +2 -1
- package/lib/packlets/validation/oneOf.js +2 -1
- package/lib/packlets/validation/typeGuard.d.ts +3 -2
- package/lib/packlets/validation/typeGuard.js +2 -1
- package/lib/packlets/validation/validator.d.ts +7 -0
- package/lib/packlets/validation/validatorBase.d.ts +2 -1
- package/lib/packlets/validation/validatorBase.js +1 -1
- package/lib/packlets/validation/validators.d.ts +2 -2
- package/lib/packlets/validation/validators.js +3 -3
- 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/recursiveValidation.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 +84 -0
- package/src/packlets/validation/boolean.ts +63 -0
- package/src/packlets/validation/classes.ts +38 -0
- package/src/packlets/validation/common.ts +28 -0
- package/src/packlets/validation/field.ts +99 -0
- package/src/packlets/validation/genericValidator.ts +204 -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 +78 -0
- package/src/packlets/validation/string.ts +66 -0
- package/src/packlets/validation/traits.ts +113 -0
- package/src/packlets/validation/typeGuard.ts +83 -0
- package/src/packlets/validation/validator.ts +157 -0
- package/src/packlets/validation/validatorBase.ts +66 -0
- package/src/packlets/validation/validators.ts +254 -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,208 @@
|
|
|
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 { Validator, ValidatorOptions } from './validator';
|
|
25
|
+
import { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';
|
|
26
|
+
|
|
27
|
+
import { FieldValidator } from './field';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Per-property {@link Validation.Validator | validators} for each of the properties in `<T>`.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export type FieldValidators<T, TC = unknown> = { [key in keyof T]: Validator<T[key], TC> };
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Options for an {@link Validation.Classes.ObjectValidator | ObjectValidator}.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
40
|
+
export interface ObjectValidatorOptions<T, TC> extends ValidatorOptions<TC> {
|
|
41
|
+
/**
|
|
42
|
+
* If present, lists optional fields. Missing non-optional fields cause an error.
|
|
43
|
+
*/
|
|
44
|
+
optionalFields?: (keyof T)[];
|
|
45
|
+
/**
|
|
46
|
+
* If true, unrecognized fields yield an error. If false or undefined (default),
|
|
47
|
+
* unrecognized fields are ignored.
|
|
48
|
+
*/
|
|
49
|
+
strict?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Options for the {@link Validation.Classes.ObjectValidator | ObjectValidator} constructor.
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
57
|
+
export interface ObjectValidatorConstructorParams<T, TC> extends ValidatorBaseConstructorParams<T, TC> {
|
|
58
|
+
/**
|
|
59
|
+
* A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a
|
|
60
|
+
* {@link Validation.Validator | Validator} for each of the expected properties
|
|
61
|
+
* of a result object.
|
|
62
|
+
*/
|
|
63
|
+
fields: FieldValidators<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Optional additional {@link Validation.Classes.ObjectValidatorOptions | ValidatorOptions} to
|
|
66
|
+
* configure validation.
|
|
67
|
+
*/
|
|
68
|
+
options?: ObjectValidatorOptions<T, TC>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* In-place {@link Validation.Validator | Validator} for an object of type `<T>`.
|
|
73
|
+
* @remarks
|
|
74
|
+
* By default, succeeds if all of the required fields exist and are validate, and fails if
|
|
75
|
+
* any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}
|
|
76
|
+
* for other validation options.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export class ObjectValidator<T, TC = unknown> extends ValidatorBase<T, TC> {
|
|
80
|
+
/**
|
|
81
|
+
* A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a
|
|
82
|
+
* {@link Validation.Validator | Validator} for each of the expected properties
|
|
83
|
+
*/
|
|
84
|
+
public readonly fields: FieldValidators<T>;
|
|
85
|
+
/**
|
|
86
|
+
* {@link Validation.Classes.ObjectValidatorOptions | Options} which apply to this
|
|
87
|
+
* validator.
|
|
88
|
+
*/
|
|
89
|
+
public readonly options: ObjectValidatorOptions<T, TC>;
|
|
90
|
+
/**
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
93
|
+
protected readonly _innerValidators: FieldValidators<T>;
|
|
94
|
+
/**
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
protected readonly _allowedFields?: Set<keyof T>;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.
|
|
101
|
+
* @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing
|
|
102
|
+
* a {@link Validation.Validator | Validator} for each field.
|
|
103
|
+
* @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure
|
|
104
|
+
* validation.
|
|
105
|
+
*/
|
|
106
|
+
public constructor(params: ObjectValidatorConstructorParams<T, TC>) {
|
|
107
|
+
super(params);
|
|
108
|
+
|
|
109
|
+
this.fields = params.fields;
|
|
110
|
+
this.options = params.options ?? {};
|
|
111
|
+
this._innerValidators = ObjectValidator._resolveValidators(this.fields, this.options);
|
|
112
|
+
this._allowedFields =
|
|
113
|
+
this.options.strict === true ? new Set(Object.keys(this.fields) as (keyof T)[]) : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be
|
|
118
|
+
* used by this converter by applying any options or traits defined in the options
|
|
119
|
+
* to the field converters passed to the constructor.
|
|
120
|
+
* @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed
|
|
121
|
+
* in to the constructor.
|
|
122
|
+
* @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}
|
|
123
|
+
* passed in to the constructor.
|
|
124
|
+
* @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured
|
|
125
|
+
* individual {@link Validation.Validator | field validators} to be applied.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
protected static _resolveValidators<T, TC>(
|
|
129
|
+
fields: FieldValidators<T, TC>,
|
|
130
|
+
options?: ObjectValidatorOptions<T, TC>
|
|
131
|
+
): FieldValidators<T, TC> {
|
|
132
|
+
const resolved: Partial<FieldValidators<T, TC>> = {};
|
|
133
|
+
for (const key in fields) {
|
|
134
|
+
if (fields[key]) {
|
|
135
|
+
/* c8 ignore next */
|
|
136
|
+
const optional = fields[key].isOptional || options?.optionalFields?.includes(key);
|
|
137
|
+
resolved[key] = new FieldValidator(key, fields[key], { optional });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return resolved as FieldValidators<T, TC>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
|
|
145
|
+
* new optional properties as specified by a supplied
|
|
146
|
+
* {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.
|
|
147
|
+
* @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new
|
|
148
|
+
* {@link Validation.Classes.ObjectValidator | validator}.
|
|
149
|
+
* @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
|
|
150
|
+
* source properties.
|
|
151
|
+
*/
|
|
152
|
+
public partial(options?: ObjectValidatorOptions<T, TC>): ObjectValidator<Partial<T>, TC> {
|
|
153
|
+
/* c8 ignore next */
|
|
154
|
+
options = options ?? {};
|
|
155
|
+
return new ObjectValidator<Partial<T>, TC>({
|
|
156
|
+
fields: this.fields as FieldValidators<Partial<T>, TC>,
|
|
157
|
+
options: {
|
|
158
|
+
...this.options,
|
|
159
|
+
...options
|
|
160
|
+
},
|
|
161
|
+
traits: this.traits
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
|
|
167
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
168
|
+
* @param addOptionalProperties - The keys to be made optional.
|
|
169
|
+
* @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
|
|
170
|
+
* source properties.
|
|
171
|
+
*/
|
|
172
|
+
public addPartial(addOptionalFields: (keyof T)[]): ObjectValidator<Partial<T>, TC> {
|
|
173
|
+
return this.partial({
|
|
174
|
+
optionalFields: [...(this.options.optionalFields ?? []), ...addOptionalFields]
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* {@inheritdoc Validation.ValidatorBase._validate}
|
|
180
|
+
* @internal
|
|
181
|
+
*/
|
|
182
|
+
protected _validate(from: unknown, context?: TC, self?: Validator<T, TC>): boolean | Failure<T> {
|
|
183
|
+
if (typeof from !== 'object' || from === null || Array.isArray(from)) {
|
|
184
|
+
return fail('source is not an object');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// eslint bug thinks key is used before defined
|
|
188
|
+
// eslint-disable-next-line no-use-before-define
|
|
189
|
+
const converted = {} as { [key in keyof T]: T[key] };
|
|
190
|
+
const errors: string[] = [];
|
|
191
|
+
for (const key in this._innerValidators) {
|
|
192
|
+
if (this._innerValidators[key]) {
|
|
193
|
+
const result = this._innerValidators[key].validate(from, context);
|
|
194
|
+
if (result.success && result.value !== undefined) {
|
|
195
|
+
converted[key] = result.value;
|
|
196
|
+
} else if (result.isFailure()) {
|
|
197
|
+
errors.push(result.message);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (this._allowedFields) {
|
|
203
|
+
const invalid = Object.keys(from).filter((k) => !this._allowedFields!.has(k as keyof T));
|
|
204
|
+
invalid.forEach((key) => errors.push(`${key}: unexpected field in source object.`));
|
|
205
|
+
}
|
|
206
|
+
return errors.length === 0 ? true : fail(errors.join('\n'));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 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.OneOfValidator | OneOfValidator}.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
32
|
+
export interface OneOfValidatorConstructorParams<T, TC = unknown>
|
|
33
|
+
extends ValidatorBaseConstructorParams<T, TC> {
|
|
34
|
+
validators: Validator<T, TC>[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* An in-place {@link Validator | Validator} which validates that a supplied
|
|
39
|
+
* value matches one of several other validators.
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export class OneOfValidator<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 _validators: Validator<T, TC>[];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Constructs a new {@link Validation.Classes.OneOfValidator | OneOfValidator}.
|
|
53
|
+
* @param params - Optional {@link Validation.Classes.OneOfValidatorConstructorParams | init params} for the
|
|
54
|
+
* new {@link Validation.Classes.OneOfValidator | OneOfValidator}.
|
|
55
|
+
*/
|
|
56
|
+
public constructor(params: OneOfValidatorConstructorParams<T, TC>) {
|
|
57
|
+
super(params);
|
|
58
|
+
this._validators = params.validators;
|
|
59
|
+
this.options = params.options ?? {};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Static method which validates that a supplied `unknown` value matches at least one
|
|
64
|
+
* of the configured validators.
|
|
65
|
+
* @param from - The `unknown` value to be tested.
|
|
66
|
+
* @param context - Optional validation context will be propagated to element validator.
|
|
67
|
+
* @param self - Optional self-reference for recursive validation.
|
|
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, self?: Validator<T, TC>): boolean | Failure<T> {
|
|
72
|
+
const found = this._validators.some((v) => v.validate(from).success);
|
|
73
|
+
if (found) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return fail(`value "${from}" does not match any validator.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -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.StringValidator | StringValidator}.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export type StringValidatorConstructorParams<
|
|
31
|
+
T extends string = string,
|
|
32
|
+
TC = unknown
|
|
33
|
+
> = GenericValidatorConstructorParams<T, TC>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An in-place {@link Validation.Validator | Validator} for `string` values.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export class StringValidator<T extends string = string, TC = unknown> extends GenericValidator<T, TC> {
|
|
40
|
+
/**
|
|
41
|
+
* Constructs a new {@link Validation.Classes.StringValidator | StringValidator}.
|
|
42
|
+
* @param params - Optional {@link Validation.Classes.StringValidatorConstructorParams | init params}
|
|
43
|
+
* for the new {@link Validation.Classes.StringValidator | StringValidator}.
|
|
44
|
+
*/
|
|
45
|
+
public constructor(params?: StringValidatorConstructorParams<T, TC>) {
|
|
46
|
+
/* c8 ignore next */
|
|
47
|
+
params = params ?? {};
|
|
48
|
+
super({
|
|
49
|
+
validator: StringValidator.validateString,
|
|
50
|
+
...params
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Static method which validates that a supplied `unknown` value is a `string`.
|
|
56
|
+
* @param from - The `unknown` value to be tested.
|
|
57
|
+
* @returns Returns `true` if `from` is a `string`, or {@link Failure} with an error
|
|
58
|
+
* message if not.
|
|
59
|
+
*/
|
|
60
|
+
public static validateString<T extends string>(from: unknown): boolean | Failure<T> {
|
|
61
|
+
if (typeof from === 'string') {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return fail<T>(`"${from}": not a string`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
/**
|
|
24
|
+
* A {@link Validation.ConstraintTrait | ConstraintTrait} indicating that
|
|
25
|
+
* a {@link Validation.Constraint | Constraint<T>} function provides an
|
|
26
|
+
* additional constraint implementation.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
30
|
+
export interface FunctionConstraintTrait {
|
|
31
|
+
type: 'function';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Union of all supported constraint traits.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export type ConstraintTrait = FunctionConstraintTrait;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Interface describing the supported validator traits.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
45
|
+
export interface ValidatorTraitValues {
|
|
46
|
+
/**
|
|
47
|
+
* Indicates whether the validator accepts `undefined` as
|
|
48
|
+
* a valid value.
|
|
49
|
+
*/
|
|
50
|
+
readonly isOptional: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* If present, indicates that the result will be branded
|
|
54
|
+
* with the corresponding brand.
|
|
55
|
+
*/
|
|
56
|
+
readonly brand?: string;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Zero or more additional {@link Validation.ConstraintTrait | ConstraintTrait}s
|
|
60
|
+
* describing additional constraints applied by this {@link Validation.Validator | Validator}.
|
|
61
|
+
*/
|
|
62
|
+
readonly constraints: ConstraintTrait[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Default {@link Validation.ValidatorTraitValues | validation traits}.
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export const defaultValidatorTraits: ValidatorTraitValues = {
|
|
70
|
+
isOptional: false,
|
|
71
|
+
constraints: []
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Generic implementation of {@link Validation.ValidatorTraitValues | ValidatorTraitValues}.
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
export class ValidatorTraits implements ValidatorTraitValues {
|
|
79
|
+
/**
|
|
80
|
+
* {@inheritdoc Validation.ValidatorTraitValues.isOptional}
|
|
81
|
+
*/
|
|
82
|
+
public readonly isOptional: boolean;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* {@inheritdoc Validation.ValidatorTraitValues.brand}
|
|
86
|
+
*/
|
|
87
|
+
public readonly brand?: string;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* {@inheritdoc Validation.ValidatorTraitValues.constraints}
|
|
91
|
+
*/
|
|
92
|
+
public readonly constraints: ConstraintTrait[];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Constructs a new {@link Validation.ValidatorTraits | ValidatorTraits} optionally
|
|
96
|
+
* initialized with the supplied base and initial values.
|
|
97
|
+
* @remarks
|
|
98
|
+
* Initial values take priority over base values, which fall back to the global default values.
|
|
99
|
+
* @param init - Partial initial values to be set in the resulting {@link Validation.Validator | Validator}.
|
|
100
|
+
* @param base - Base values to be used when no initial values are present.
|
|
101
|
+
*/
|
|
102
|
+
public constructor(init?: Partial<ValidatorTraitValues>, base?: ValidatorTraitValues) {
|
|
103
|
+
/* c8 ignore next 2 */
|
|
104
|
+
this.isOptional = init?.isOptional ?? base?.isOptional ?? defaultValidatorTraits.isOptional;
|
|
105
|
+
this.brand = init?.brand ?? base?.brand ?? defaultValidatorTraits.brand;
|
|
106
|
+
this.constraints = [
|
|
107
|
+
...defaultValidatorTraits.constraints,
|
|
108
|
+
/* c8 ignore next 1 */
|
|
109
|
+
...(base?.constraints ?? []),
|
|
110
|
+
...(init?.constraints ?? [])
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';
|
|
25
|
+
|
|
26
|
+
import { TypeGuardWithContext } from './common';
|
|
27
|
+
import { Validator, ValidatorOptions } from './validator';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parameters used to construct a {@link Validation.Classes.TypeGuardValidator}.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
34
|
+
export interface TypeGuardValidatorConstructorParams<T, TC = unknown>
|
|
35
|
+
extends ValidatorBaseConstructorParams<T, TC> {
|
|
36
|
+
guard: TypeGuardWithContext<T, TC>;
|
|
37
|
+
description: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An in-place {@link Validation.Validator | Validator} that can be instantiated using a type guard
|
|
42
|
+
* function.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export class TypeGuardValidator<T, TC = unknown> extends ValidatorBase<T, TC> {
|
|
46
|
+
/**
|
|
47
|
+
* {@link Validation.ValidatorOptions | Options} which apply to this
|
|
48
|
+
* validator.
|
|
49
|
+
*/
|
|
50
|
+
public readonly options: ValidatorOptions<TC>;
|
|
51
|
+
public readonly description: string;
|
|
52
|
+
|
|
53
|
+
protected readonly _guard: TypeGuardWithContext<T, TC>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Constructs a new {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator}.
|
|
57
|
+
* @param params - Optional {@link Validation.Classes.TypeGuardValidatorConstructorParams | init params} for the
|
|
58
|
+
* new {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator}.
|
|
59
|
+
*/
|
|
60
|
+
public constructor(params: TypeGuardValidatorConstructorParams<T, TC>) {
|
|
61
|
+
super(params);
|
|
62
|
+
this.description = params.description;
|
|
63
|
+
this._guard = params.guard;
|
|
64
|
+
this.options = params.options ?? {};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Static method which validates that a supplied `unknown` value matches the supplied
|
|
69
|
+
* type guard, returning a `Failure<T>` containing more information about a failure.
|
|
70
|
+
* @param from - Value to be converted.
|
|
71
|
+
* @param context - Optional validation context.
|
|
72
|
+
* @param self - Optional self-reference for recursive validation.
|
|
73
|
+
* @returns `true` if `from` is valid, {@link Failure | Failure<T>}
|
|
74
|
+
* with an error message if `from` is invalid.
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
protected _validate(from: unknown, context?: TC, self?: Validator<T, TC>): boolean | Failure<T> {
|
|
78
|
+
if (this._guard(from, context)) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return fail(`invalid ${this.description} (${JSON.stringify(from)})`);
|
|
82
|
+
}
|
|
83
|
+
}
|