@fgv/ts-utils 5.0.0-22 → 5.0.0-24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.json +0 -21
  2. package/lib/index.d.ts +2 -1
  3. package/lib/index.js +3 -1
  4. package/lib/packlets/base/index.d.ts +0 -2
  5. package/lib/packlets/base/index.js +1 -25
  6. package/lib/packlets/base/result.d.ts +49 -0
  7. package/lib/packlets/base/result.js +36 -0
  8. package/lib/packlets/collections/collector.d.ts +9 -0
  9. package/lib/packlets/collections/collector.js +6 -0
  10. package/lib/packlets/collections/validatingCollector.d.ts +5 -1
  11. package/lib/packlets/hash/hashingNormalizer.js +4 -3
  12. package/lib/packlets/logging/index.d.ts +3 -0
  13. package/{src/packlets/base/index.ts → lib/packlets/logging/index.js} +19 -10
  14. package/lib/packlets/logging/logReporter.d.ts +69 -0
  15. package/lib/packlets/logging/logReporter.js +72 -0
  16. package/lib/packlets/logging/logger.d.ts +183 -0
  17. package/lib/packlets/logging/logger.js +237 -0
  18. package/package.json +6 -6
  19. package/.vscode/launch.json +0 -16
  20. package/.vscode/settings.json +0 -36
  21. package/dist/ts-utils.d.ts +0 -4832
  22. package/dist/tsdoc-metadata.json +0 -11
  23. package/lib/packlets/base/logger.d.ts +0 -52
  24. package/lib/packlets/base/logger.js +0 -128
  25. package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +0 -52
  26. package/lib/test/helpers/jest/helpers/fsHelpers.js +0 -180
  27. package/lib/test/helpers/jest/helpers/index.d.ts +0 -2
  28. package/lib/test/helpers/jest/helpers/index.js +0 -18
  29. package/lib/test/helpers/jest/index.d.ts +0 -3
  30. package/lib/test/helpers/jest/index.js +0 -27
  31. package/lib/test/helpers/jest/matchers/index.d.ts +0 -16
  32. package/lib/test/helpers/jest/matchers/index.js +0 -19
  33. package/lib/test/helpers/jest/matchers/toFail/index.d.ts +0 -16
  34. package/lib/test/helpers/jest/matchers/toFail/index.js +0 -25
  35. package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +0 -4
  36. package/lib/test/helpers/jest/matchers/toFail/predicate.js +0 -9
  37. package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +0 -16
  38. package/lib/test/helpers/jest/matchers/toFailTest/index.js +0 -30
  39. package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +0 -3
  40. package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +0 -12
  41. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +0 -17
  42. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +0 -26
  43. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +0 -4
  44. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +0 -15
  45. package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +0 -18
  46. package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +0 -31
  47. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +0 -4
  48. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +0 -30
  49. package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +0 -18
  50. package/lib/test/helpers/jest/matchers/toFailWith/index.js +0 -29
  51. package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +0 -4
  52. package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +0 -20
  53. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +0 -19
  54. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +0 -29
  55. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +0 -4
  56. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +0 -26
  57. package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +0 -16
  58. package/lib/test/helpers/jest/matchers/toSucceed/index.js +0 -25
  59. package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +0 -4
  60. package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +0 -9
  61. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +0 -17
  62. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +0 -26
  63. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +0 -17
  64. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +0 -26
  65. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +0 -19
  66. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +0 -55
  67. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +0 -4
  68. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +0 -21
  69. package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +0 -18
  70. package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +0 -29
  71. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +0 -4
  72. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +0 -16
  73. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +0 -18
  74. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +0 -29
  75. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +0 -4
  76. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +0 -21
  77. package/lib/test/helpers/jest/resolvers/cli.d.ts +0 -5
  78. package/lib/test/helpers/jest/resolvers/cli.js +0 -48
  79. package/lib/test/helpers/jest/resolvers/ide.d.ts +0 -5
  80. package/lib/test/helpers/jest/resolvers/ide.js +0 -48
  81. package/lib/test/helpers/jest/ts-utils.d.ts +0 -2
  82. package/lib/test/helpers/jest/ts-utils.js +0 -18
  83. package/lib/test/helpers/jest/types/index.d.ts +0 -79
  84. package/lib/test/helpers/jest/types/index.js +0 -4
  85. package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +0 -6
  86. package/lib/test/helpers/jest/utils/matcherHelpers.js +0 -53
  87. package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +0 -4
  88. package/lib/test/helpers/jest/utils/snapshotResolver.js +0 -19
  89. package/lib/test/unit/collections/collectible.test.d.ts +0 -2
  90. package/lib/test/unit/collections/collector.test.d.ts +0 -2
  91. package/lib/test/unit/collections/collectors.test.d.ts +0 -2
  92. package/lib/test/unit/collections/convertingCollector.test.d.ts +0 -2
  93. package/lib/test/unit/collections/helpers.d.ts +0 -44
  94. package/lib/test/unit/collections/helpers.js +0 -115
  95. package/lib/test/unit/collections/resultMap.test.d.ts +0 -2
  96. package/lib/test/unit/collections/validatingCollector.test.d.ts +0 -2
  97. package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +0 -2
  98. package/lib/test/unit/collections/validatingResultMap.test.d.ts +0 -2
  99. package/lib/test/unit/converter.test.d.ts +0 -2
  100. package/lib/test/unit/converters.test.d.ts +0 -2
  101. package/lib/test/unit/defaultingConverter.test.d.ts +0 -2
  102. package/lib/test/unit/file-tree/fileItem.test.d.ts +0 -2
  103. package/lib/test/unit/file-tree/fileTree.test.d.ts +0 -2
  104. package/lib/test/unit/file-tree/fsTree.test.d.ts +0 -2
  105. package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +0 -2
  106. package/lib/test/unit/file-tree/sample/rootFile.json +0 -1
  107. package/lib/test/unit/file-tree/sample/some/other/path/file2.json +0 -1
  108. package/lib/test/unit/file-tree/sample/some/path/below/file4.json +0 -1
  109. package/lib/test/unit/file-tree/sample/some/path/file1.json +0 -1
  110. package/lib/test/unit/file-tree/sample/some/path/file3.json +0 -1
  111. package/lib/test/unit/hash.test.d.ts +0 -2
  112. package/lib/test/unit/logger.test.d.ts +0 -2
  113. package/lib/test/unit/mapResults.test.d.ts +0 -3
  114. package/lib/test/unit/messageAggregator.test.d.ts +0 -3
  115. package/lib/test/unit/normalize.test.d.ts +0 -2
  116. package/lib/test/unit/result.test.d.ts +0 -3
  117. package/lib/test/unit/utils.test.d.ts +0 -3
  118. package/lib/test/unit/validation/array.test.d.ts +0 -2
  119. package/lib/test/unit/validation/boolean.test.d.ts +0 -2
  120. package/lib/test/unit/validation/genericValidator.test.d.ts +0 -2
  121. package/lib/test/unit/validation/number.test.d.ts +0 -2
  122. package/lib/test/unit/validation/object.test.d.ts +0 -2
  123. package/lib/test/unit/validation/oneOf.test.d.ts +0 -2
  124. package/lib/test/unit/validation/recordOf.test.d.ts +0 -2
  125. package/lib/test/unit/validation/recursiveValidation.test.d.ts +0 -2
  126. package/lib/test/unit/validation/string.test.d.ts +0 -2
  127. package/lib/test/unit/validation/traits.test.d.ts +0 -2
  128. package/lib/test/unit/validation/typeGuard.test.d.ts +0 -2
  129. package/lib/test/unit/validation/validators.test.d.ts +0 -2
  130. package/src/index.ts +0 -63
  131. package/src/packlets/base/brand.ts +0 -28
  132. package/src/packlets/base/logger.ts +0 -156
  133. package/src/packlets/base/mapResults.ts +0 -302
  134. package/src/packlets/base/messageAggregator.ts +0 -120
  135. package/src/packlets/base/normalize.ts +0 -144
  136. package/src/packlets/base/result.ts +0 -961
  137. package/src/packlets/base/utils.ts +0 -236
  138. package/src/packlets/collections/collectible.ts +0 -238
  139. package/src/packlets/collections/collector.ts +0 -273
  140. package/src/packlets/collections/collectorValidator.ts +0 -178
  141. package/src/packlets/collections/common.ts +0 -27
  142. package/src/packlets/collections/convertingCollector.ts +0 -223
  143. package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
  144. package/src/packlets/collections/index.ts +0 -39
  145. package/src/packlets/collections/keyValueConverters.ts +0 -142
  146. package/src/packlets/collections/readonlyResultMap.ts +0 -99
  147. package/src/packlets/collections/resultMap.ts +0 -322
  148. package/src/packlets/collections/resultMapValidator.ts +0 -188
  149. package/src/packlets/collections/utils.ts +0 -33
  150. package/src/packlets/collections/validatingCollector.ts +0 -123
  151. package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
  152. package/src/packlets/collections/validatingResultMap.ts +0 -94
  153. package/src/packlets/conversion/baseConverter.ts +0 -330
  154. package/src/packlets/conversion/converter.ts +0 -260
  155. package/src/packlets/conversion/converters.ts +0 -1039
  156. package/src/packlets/conversion/defaultingConverter.ts +0 -188
  157. package/src/packlets/conversion/index.ts +0 -30
  158. package/src/packlets/conversion/objectConverter.ts +0 -259
  159. package/src/packlets/conversion/stringConverter.ts +0 -165
  160. package/src/packlets/file-tree/directoryItem.ts +0 -84
  161. package/src/packlets/file-tree/fileItem.ts +0 -119
  162. package/src/packlets/file-tree/fileTree.ts +0 -152
  163. package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
  164. package/src/packlets/file-tree/fsTree.ts +0 -118
  165. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
  166. package/src/packlets/file-tree/in-memory/index.ts +0 -23
  167. package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
  168. package/src/packlets/file-tree/index.ts +0 -31
  169. package/src/packlets/hash/crcNormalizer.ts +0 -81
  170. package/src/packlets/hash/hashingNormalizer.ts +0 -102
  171. package/src/packlets/hash/index.ts +0 -24
  172. package/src/packlets/validation/array.ts +0 -84
  173. package/src/packlets/validation/boolean.ts +0 -63
  174. package/src/packlets/validation/classes.ts +0 -38
  175. package/src/packlets/validation/common.ts +0 -28
  176. package/src/packlets/validation/field.ts +0 -99
  177. package/src/packlets/validation/genericValidator.ts +0 -204
  178. package/src/packlets/validation/index.ts +0 -31
  179. package/src/packlets/validation/number.ts +0 -66
  180. package/src/packlets/validation/object.ts +0 -208
  181. package/src/packlets/validation/oneOf.ts +0 -78
  182. package/src/packlets/validation/string.ts +0 -66
  183. package/src/packlets/validation/traits.ts +0 -113
  184. package/src/packlets/validation/typeGuard.ts +0 -83
  185. package/src/packlets/validation/validator.ts +0 -157
  186. package/src/packlets/validation/validatorBase.ts +0 -66
  187. package/src/packlets/validation/validators.ts +0 -254
@@ -1,84 +0,0 @@
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
- * @param self - Optional self-reference for recursive validation.
69
- * @returns Returns `true` if `from` is an `array` of valid elements, or
70
- * {@link Failure} with an error message if not.
71
- */
72
- protected _validate(from: unknown, context?: TC, self?: Validator<T[], TC>): boolean | Failure<T[]> {
73
- if (Array.isArray(from)) {
74
- for (const elem of from) {
75
- const result = this._validateElement.validate(elem, context);
76
- if (!result.isSuccess()) {
77
- return fail(result.message);
78
- }
79
- }
80
- return true;
81
- }
82
- return fail<T[]>(`"${from}": not an array`);
83
- }
84
- }
@@ -1,63 +0,0 @@
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
- }
@@ -1,38 +0,0 @@
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 */
@@ -1,28 +0,0 @@
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
- /**
24
- * A type guard function which validates a specific type, with an optional context
25
- * that can be used to shape the validation.
26
- * @public
27
- */
28
- export type TypeGuardWithContext<T, TC = unknown> = (from: unknown, context?: TC) => from is T;
@@ -1,99 +0,0 @@
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, self?: Validator<T, 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
- }
@@ -1,204 +0,0 @@
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 {
26
- Constraint,
27
- ValidationErrorFormatter,
28
- Validator,
29
- ValidatorFunc,
30
- ValidatorOptions
31
- } from './validator';
32
-
33
- /**
34
- * Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.
35
- * @public
36
- */
37
- // eslint-disable-next-line @typescript-eslint/naming-convention
38
- export interface GenericValidatorConstructorParams<T, TC> {
39
- options?: ValidatorOptions<TC>;
40
- traits?: Partial<ValidatorTraits>;
41
- validator?: ValidatorFunc<T, TC>;
42
- }
43
-
44
- /**
45
- * Generic base implementation for an in-place {@link Validation.Validator | Validator}.
46
- * @public
47
- */
48
- export class GenericValidator<T, TC = unknown> implements Validator<T, TC> {
49
- /**
50
- * {@inheritdoc Validation.Validator.traits}
51
- */
52
- public readonly traits: ValidatorTraits;
53
-
54
- /**
55
- * @internal
56
- */
57
- protected readonly _validator: ValidatorFunc<T, TC>;
58
- /**
59
- * @internal
60
- */
61
- protected readonly _options: ValidatorOptions<TC>;
62
-
63
- /**
64
- * Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.
65
- * @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}
66
- * used to configure validation.
67
- */
68
- public constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>) {
69
- if (!params.validator) {
70
- throw new Error('No validator function supplied');
71
- }
72
- this._validator = params.validator;
73
- this._options = params.options ?? {};
74
- this.traits = new ValidatorTraits(params.traits);
75
- }
76
-
77
- /**
78
- * {@inheritdoc Validation.Validator.isOptional}
79
- */
80
- public get isOptional(): boolean {
81
- return this.traits.isOptional;
82
- }
83
-
84
- /**
85
- * {@inheritdoc Validation.Validator.brand}
86
- */
87
- public get brand(): string | undefined {
88
- return this.traits.brand;
89
- }
90
-
91
- /**
92
- * {@inheritdoc Validation.Validator.validate}
93
- */
94
- public validate(from: unknown, context?: TC): Result<T> {
95
- const result = this._validator(from, this._context(context), this);
96
- if (typeof result === 'boolean') {
97
- return result ? succeed(from as T) : fail<T>('Invalid value');
98
- }
99
- return result;
100
- }
101
-
102
- /**
103
- * {@inheritdoc Validation.Validator.convert}
104
- */
105
- public convert(from: unknown, context?: TC): Result<T> {
106
- const result = this._validator(from, this._context(context), this);
107
- if (typeof result === 'boolean') {
108
- return result ? succeed(from as T) : fail<T>('Invalid value');
109
- }
110
- return result;
111
- }
112
-
113
- /**
114
- * {@inheritdoc Validation.Validator.validateOptional}
115
- */
116
- public validateOptional(from: unknown, context?: TC): Result<T | undefined> {
117
- return from === undefined ? succeed(undefined) : this.validate(from, context);
118
- }
119
-
120
- /**
121
- * {@inheritdoc Validation.Validator.guard}
122
- */
123
- public guard(from: unknown, context?: TC): from is T {
124
- return this._validator(from, this._context(context), this) === true;
125
- }
126
-
127
- /**
128
- * {@inheritdoc Validation.Validator.optional}
129
- */
130
- public optional(): Validator<T | undefined, TC> {
131
- return new GenericValidator({
132
- validator: (from: unknown, context?: TC, self?: Validator<T | undefined, TC>) => {
133
- return from === undefined || this._validator(from, this._context(context), this);
134
- },
135
- traits: { isOptional: true }
136
- });
137
- }
138
-
139
- /**
140
- * {@inheritdoc Validation.Validator.withConstraint}
141
- */
142
- public withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC> {
143
- trait = trait ?? { type: 'function' };
144
- return new GenericValidator({
145
- validator: (from: unknown, context?: TC, self?: Validator<T, TC>): boolean | Failure<T> => {
146
- const result = this._validator(from, this._context(context), this);
147
- if (result === true) {
148
- const constraintResult = constraint(from as T);
149
- if (typeof constraintResult === 'boolean') {
150
- return constraintResult
151
- ? true
152
- : fail(`Invalid value "${JSON.stringify(from)}": does not meet constraint.`);
153
- }
154
- return constraintResult;
155
- }
156
- return result;
157
- },
158
- traits: { constraints: [trait] }
159
- });
160
- }
161
-
162
- /**
163
- * {@inheritdoc Validation.Validator.brand}
164
- */
165
- public withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC> {
166
- if (this.brand) {
167
- throw new Error(`Cannot replace existing brand "${this.brand}" with "${brand}".`);
168
- }
169
-
170
- return new GenericValidator<Brand<T, B>, TC>({
171
- validator: (from: unknown, context?: TC, self?: Validator<Brand<T, B>, TC>) => {
172
- return this._validator(from, this._context(context), this) as boolean | Failure<Brand<T, B>>;
173
- },
174
- traits: { brand }
175
- });
176
- }
177
-
178
- /**
179
- * {@inheritdoc Validation.Validator.withFormattedError}
180
- */
181
- public withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC> {
182
- return new GenericValidator<T, TC>({
183
- validator: (from: unknown, context?: TC, self?: Validator<T, TC>) => {
184
- const result = this._validator(from, this._context(context), this);
185
- if (result === true) {
186
- return true;
187
- }
188
- /* c8 ignore next - defense in depth */
189
- const message = result === false ? undefined : result.message;
190
- return fail(formatter(from, message, this._context(context)));
191
- }
192
- });
193
- }
194
-
195
- /**
196
- * Gets a default or explicit context.
197
- * @param explicitContext - Optional explicit context.
198
- * @returns The appropriate context to use.
199
- * @internal
200
- */
201
- protected _context(explicitContext?: TC): TC | undefined {
202
- return explicitContext ?? this._options.defaultContext;
203
- }
204
- }
@@ -1,31 +0,0 @@
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 };
@@ -1,66 +0,0 @@
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
- }