@fgv/ts-utils 5.0.0-21 → 5.0.0-23

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 (80) hide show
  1. package/dist/ts-utils.d.ts +307 -33
  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/hash/index.ts → lib/packlets/logging/index.js} +19 -3
  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/lib/test/unit/consoleLogger.test.d.ts +2 -0
  19. package/lib/test/unit/hashingNormalizerCollisions.test.d.ts +2 -0
  20. package/package.json +1 -1
  21. package/lib/packlets/base/logger.d.ts +0 -52
  22. package/lib/packlets/base/logger.js +0 -128
  23. package/src/index.ts +0 -63
  24. package/src/packlets/base/brand.ts +0 -28
  25. package/src/packlets/base/index.ts +0 -31
  26. package/src/packlets/base/logger.ts +0 -156
  27. package/src/packlets/base/mapResults.ts +0 -302
  28. package/src/packlets/base/messageAggregator.ts +0 -120
  29. package/src/packlets/base/normalize.ts +0 -144
  30. package/src/packlets/base/result.ts +0 -961
  31. package/src/packlets/base/utils.ts +0 -236
  32. package/src/packlets/collections/collectible.ts +0 -238
  33. package/src/packlets/collections/collector.ts +0 -273
  34. package/src/packlets/collections/collectorValidator.ts +0 -178
  35. package/src/packlets/collections/common.ts +0 -27
  36. package/src/packlets/collections/convertingCollector.ts +0 -223
  37. package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
  38. package/src/packlets/collections/index.ts +0 -39
  39. package/src/packlets/collections/keyValueConverters.ts +0 -142
  40. package/src/packlets/collections/readonlyResultMap.ts +0 -99
  41. package/src/packlets/collections/resultMap.ts +0 -322
  42. package/src/packlets/collections/resultMapValidator.ts +0 -188
  43. package/src/packlets/collections/utils.ts +0 -33
  44. package/src/packlets/collections/validatingCollector.ts +0 -123
  45. package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
  46. package/src/packlets/collections/validatingResultMap.ts +0 -94
  47. package/src/packlets/conversion/baseConverter.ts +0 -330
  48. package/src/packlets/conversion/converter.ts +0 -260
  49. package/src/packlets/conversion/converters.ts +0 -1039
  50. package/src/packlets/conversion/defaultingConverter.ts +0 -188
  51. package/src/packlets/conversion/index.ts +0 -30
  52. package/src/packlets/conversion/objectConverter.ts +0 -259
  53. package/src/packlets/conversion/stringConverter.ts +0 -165
  54. package/src/packlets/file-tree/directoryItem.ts +0 -84
  55. package/src/packlets/file-tree/fileItem.ts +0 -119
  56. package/src/packlets/file-tree/fileTree.ts +0 -152
  57. package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
  58. package/src/packlets/file-tree/fsTree.ts +0 -118
  59. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
  60. package/src/packlets/file-tree/in-memory/index.ts +0 -23
  61. package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
  62. package/src/packlets/file-tree/index.ts +0 -31
  63. package/src/packlets/hash/crcNormalizer.ts +0 -81
  64. package/src/packlets/hash/hashingNormalizer.ts +0 -102
  65. package/src/packlets/validation/array.ts +0 -84
  66. package/src/packlets/validation/boolean.ts +0 -63
  67. package/src/packlets/validation/classes.ts +0 -38
  68. package/src/packlets/validation/common.ts +0 -28
  69. package/src/packlets/validation/field.ts +0 -99
  70. package/src/packlets/validation/genericValidator.ts +0 -204
  71. package/src/packlets/validation/index.ts +0 -31
  72. package/src/packlets/validation/number.ts +0 -66
  73. package/src/packlets/validation/object.ts +0 -208
  74. package/src/packlets/validation/oneOf.ts +0 -78
  75. package/src/packlets/validation/string.ts +0 -66
  76. package/src/packlets/validation/traits.ts +0 -113
  77. package/src/packlets/validation/typeGuard.ts +0 -83
  78. package/src/packlets/validation/validator.ts +0 -157
  79. package/src/packlets/validation/validatorBase.ts +0 -66
  80. package/src/packlets/validation/validators.ts +0 -254
@@ -1,157 +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 } from '../base';
24
- import { ConstraintTrait, ValidatorTraits } from './traits';
25
-
26
- /**
27
- * Type for a validation function, which validates that a supplied `unknown`
28
- * value is a valid value of type `<T>`, possibly as influenced by
29
- * an optionally-supplied validation context of type `<TC>`.
30
- * @public
31
- */
32
- export type ValidatorFunc<T, TC> = (
33
- from: unknown,
34
- context?: TC,
35
- self?: Validator<T, TC>
36
- ) => boolean | Failure<T>;
37
-
38
- /**
39
- * Options that apply to any {@link Validation.Validator | Validator}.
40
- * @public
41
- */
42
- // eslint-disable-next-line @typescript-eslint/naming-convention
43
- export interface ValidatorOptions<TC> {
44
- defaultContext?: TC;
45
- }
46
-
47
- /**
48
- * A {@link Validation.Constraint | Constraint<T>} function returns
49
- * `true` if the supplied value meets the constraint. Can return
50
- * {@link Failure} with an error message or simply return `false`
51
- * for a default message.
52
- * @public
53
- */
54
- export type Constraint<T> = (val: T) => boolean | Failure<T>;
55
-
56
- /**
57
- * Formats an incoming error message and value that failed validation.
58
- * @param val - The value that failed validation.
59
- * @param message - The default error message, if any.
60
- * @param context - Optional validation context.
61
- * @returns The formatted error message.
62
- * @public
63
- */
64
- export type ValidationErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;
65
-
66
- /**
67
- * In-place validation that a supplied unknown matches some
68
- * required characteristics (type, values, etc).
69
- * @public
70
- */
71
- // eslint-disable-next-line @typescript-eslint/naming-convention
72
- export interface Validator<T, TC = unknown> {
73
- /**
74
- * {@link Validation.ValidatorTraits | Traits} describing this validation.
75
- */
76
- readonly traits: ValidatorTraits;
77
-
78
- /**
79
- * Indicates whether this element is explicitly optional.
80
- */
81
- readonly isOptional: boolean;
82
-
83
- /**
84
- * The brand for a branded type.
85
- */
86
- readonly brand: string | undefined;
87
-
88
- /**
89
- * Tests to see if a supplied `unknown` value matches this validation. All
90
- * validate calls are guaranteed to return the entity passed in on Success.
91
- * @param from - The `unknown` value to be tested.
92
- * @param context - Optional validation context.
93
- * @returns {@link Success} with the typed, validated value,
94
- * or {@link Failure} with an error message if validation fails.
95
- */
96
- validate(from: unknown, context?: TC): Result<T>;
97
-
98
- /**
99
- * Tests to see if a supplied 'unknown' value matches this validation. In
100
- * contrast to {@link Validator.validate | validate}, makes no guarantees
101
- * about the identity of the returned value.
102
- * @param from - The `unknown` value to be tested.
103
- * @param context - Optional validation context.
104
- * @returns {@link Success} with the typed, conversion value,
105
- * or {@link Failure} with an error message if conversion fails.
106
- */
107
- convert(from: unknown, context?: TC): Result<T>;
108
-
109
- /**
110
- * Tests to see if a supplied `unknown` value matches this
111
- * validation. Accepts `undefined`.
112
- * @param from - The `unknown` value to be tested.
113
- * @param context - Optional validation context.
114
- * @returns {@link Success} with the typed, validated value,
115
- * or {@link Failure} with an error message if validation fails.
116
- */
117
- validateOptional(from: unknown, context?: TC): Result<T | undefined>;
118
-
119
- /**
120
- * Non-throwing type guard
121
- * @param from - The value to be tested.
122
- * @param context - Optional validation context.
123
- */
124
- guard(from: unknown, context?: TC): from is T;
125
-
126
- /**
127
- * Creates an {@link Validation.Validator | in-place validator}
128
- * which is derived from this one but which also matches `undefined`.
129
- */
130
- optional(): Validator<T | undefined, TC>;
131
-
132
- /**
133
- * Creates an {@link Validation.Validator | in-place validator}
134
- * which is derived from this one but which applies additional constraints.
135
- * @param constraint - the constraint to be applied
136
- * @param trait - As optional {@link Validation.ConstraintTrait | ConstraintTrait}
137
- * to be applied to the resulting {@link Validation.Validator | Validator}.
138
- * @returns A new {@link Validation.Validator | Validator}.
139
- */
140
- withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC>;
141
-
142
- /**
143
- * Creates a new {@link Validation.Validator | in-place validator} which
144
- * is derived from this one but which matches a branded result.
145
- * @param brand - The brand to be applied.
146
- */
147
- withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
148
-
149
- /**
150
- * Creates a new {@link Validation.Validator | in-place validator} which
151
- * is derived from this one but which returns an error message supplied
152
- * by the provided formatter if an error occurs.
153
- * @param formatter - The error message formatter to be applied.
154
- * @returns A new {@link Validation.Validator | Validator}.
155
- */
156
- withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC>;
157
- }
@@ -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 { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
24
-
25
- import { Failure } from '../base';
26
-
27
- /**
28
- * @internal
29
- */
30
- export type ValidatorBaseConstructorParams<T, TC> = Omit<
31
- GenericValidatorConstructorParams<T, TC>,
32
- 'validator'
33
- >;
34
-
35
- /**
36
- * Abstract base helper class for specific validator implementations
37
- * @internal
38
- */
39
- export abstract class ValidatorBase<T, TC = unknown> extends GenericValidator<T, TC> {
40
- /**
41
- * Inner constructor
42
- * @param params - Initialization params.
43
- * @internal
44
- */
45
- protected constructor(params: Partial<ValidatorBaseConstructorParams<T, TC>>) {
46
- super({
47
- validator: (from, context, self) => this._validate(from, context, self),
48
- ...params
49
- });
50
- }
51
-
52
- /**
53
- * Abstract validation method to me implemented by derived classes.
54
- * @param from - Value to be converted.
55
- * @param context - Optional validation context.
56
- * @param self - Optional self-reference for recursive validation.
57
- * @returns `true` if `from` is valid, {@link Failure | Failure<T>}
58
- * with an error message if `from` is invalid.
59
- * @internal
60
- */
61
- protected abstract _validate(
62
- from: unknown,
63
- context?: TC,
64
- self?: import('./validator').Validator<T, TC>
65
- ): boolean | Failure<T>;
66
- }
@@ -1,254 +0,0 @@
1
- /*
2
- * Copyright (c) 2023 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, MessageAggregator } from '../base';
24
- import { ArrayValidator, ArrayValidatorConstructorParams } from './array';
25
- import { FieldValidators, ObjectValidator, ObjectValidatorConstructorParams } from './object';
26
- import { TypeGuardValidator, TypeGuardValidatorConstructorParams } from './typeGuard';
27
-
28
- import { BooleanValidator } from './boolean';
29
- import { TypeGuardWithContext } from './common';
30
- import { GenericValidator } from './genericValidator';
31
- import { NumberValidator } from './number';
32
- import { OneOfValidator, OneOfValidatorConstructorParams } from './oneOf';
33
- import { StringValidator } from './string';
34
- import { Validator, ValidatorFunc } from './validator';
35
-
36
- /**
37
- * A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
38
- * @public
39
- */
40
- export const string: Validator<string> = new StringValidator();
41
-
42
- /**
43
- * A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
44
- * @public
45
- */
46
- export const number: Validator<number> = new NumberValidator();
47
-
48
- /**
49
- * A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
50
- * @public
51
- */
52
- export const boolean: Validator<boolean> = new BooleanValidator();
53
-
54
- /**
55
- * Helper function to create a {@link Validation.Classes.ObjectValidator | ObjectValidator} which validates
56
- * an object in place.
57
- * @param fields - A {@link Validation.Classes.FieldValidators | field validator definition}
58
- * describing the validations to be applied.
59
- * @param params - Optional {@link Validation.Classes.ObjectValidatorConstructorParams | parameters}
60
- * to refine the behavior of the resulting {@link Validation.Validator | validator}.
61
- * @returns A new {@link Validation.Validator | Validator} which validates the desired
62
- * object in place.
63
- * @public
64
- */
65
- export function object<T, TC = unknown>(
66
- fields: FieldValidators<T, TC>,
67
- params?: Omit<ObjectValidatorConstructorParams<T, TC>, 'fields'>
68
- ): ObjectValidator<T, TC> {
69
- return new ObjectValidator({ fields, ...(params ?? {}) });
70
- }
71
-
72
- /**
73
- * Helper function to create a {@link Validation.Classes.ArrayValidator | ArrayValidator} which
74
- * validates an array in place.
75
- * @param validateElement - A {@link Validation.Validator | validator} which validates each element.
76
- * @returns A new {@link Validation.Classes.ArrayValidator | ArrayValidator } which validates the desired
77
- * array in place.
78
- * @public
79
- */
80
- export function arrayOf<T, TC>(
81
- validateElement: Validator<T, TC>,
82
- params?: Omit<ArrayValidatorConstructorParams<T, TC>, 'validateElement'>
83
- ): ArrayValidator<T, TC> {
84
- return new ArrayValidator({ validateElement, ...(params ?? {}) });
85
- }
86
-
87
- /**
88
- * Options for {@link Validators.recordOf} helper function.
89
- * @public
90
- */
91
- export interface IRecordOfValidatorOptions<TK extends string = string, TC = unknown> {
92
- /**
93
- * If `onError` is `'fail'` (default), then the entire validation fails if any key or element
94
- * cannot be validated. If `onError` is `'ignore'`, failing elements are silently ignored.
95
- */
96
- onError?: 'fail' | 'ignore';
97
- /**
98
- * If present, `keyValidator` is used to validate the source object property names.
99
- * @remarks
100
- * Can be used to validate key names to supported values and/or strong types.
101
- */
102
- keyValidator?: Validator<TK, TC>;
103
- }
104
-
105
- /**
106
- * A helper function to create a {@link Validation.Validator | Validator} which validates the `string`-keyed properties
107
- * using a supplied {@link Validation.Validator | Validator<T, TC>} to produce a `Record<TK, T>`.
108
- * @remarks
109
- * If present, the supplied {@link Validators.IRecordOfValidatorOptions | options} can provide a strongly-typed
110
- * validator for keys and/or control the handling of elements that fail validation.
111
- * @param validator - {@link Validation.Validator | Validator} used for each item in the source object.
112
- * @param options - Optional {@link Validators.IRecordOfValidatorOptions | IRecordOfValidatorOptions<TK, TC>} which
113
- * supplies a key validator and/or error-handling options.
114
- * @returns A {@link Validation.Validator | Validator} which validates `Record<TK, T>`.
115
- * @public
116
- */
117
- export function recordOf<T, TC = unknown, TK extends string = string>(
118
- validator: Validator<T, TC>,
119
- options?: IRecordOfValidatorOptions<TK, TC>
120
- ): Validator<Record<TK, T>, TC> {
121
- const opts: IRecordOfValidatorOptions<TK, TC> = { onError: 'fail', ...options };
122
-
123
- return new GenericValidator({
124
- validator: (
125
- from: unknown,
126
- context?: TC,
127
- self?: Validator<Record<TK, T>, TC>
128
- ): boolean | Failure<Record<TK, T>> => {
129
- if (typeof from !== 'object' || from === null || Array.isArray(from)) {
130
- return fail(`Not a string-keyed object: ${JSON.stringify(from)}`);
131
- }
132
-
133
- const errors = new MessageAggregator();
134
-
135
- for (const key in from) {
136
- if (isKeyOf(key, from)) {
137
- // Validate key if keyValidator is provided
138
- if (opts.keyValidator) {
139
- const keyResult = opts.keyValidator.validate(key, context);
140
- if (!keyResult.isSuccess()) {
141
- if (opts.onError === 'ignore') {
142
- continue;
143
- }
144
- errors.addMessage(`Key "${key}": ${keyResult.message}`);
145
- if (opts.onError === 'fail') {
146
- return fail(keyResult.message);
147
- }
148
- }
149
- }
150
-
151
- // Validate value
152
- const valueResult = validator.validate(from[key] as unknown, context);
153
- if (!valueResult.isSuccess()) {
154
- if (opts.onError === 'ignore') {
155
- continue;
156
- }
157
- errors.addMessage(`Property "${key}": ${valueResult.message}`);
158
- if (opts.onError === 'fail') {
159
- return fail(valueResult.message);
160
- }
161
- }
162
- }
163
- }
164
-
165
- /* c8 ignore next 1 - defense in depth */
166
- return errors.hasMessages ? fail(errors.toString()) : true;
167
- }
168
- });
169
- }
170
-
171
- /**
172
- * Helper function to create a {@link Validation.Validator} which validates an enumerated
173
- * value in place.
174
- * @public
175
- */
176
- export function enumeratedValue<T extends string>(values: ReadonlyArray<T>): Validator<T, ReadonlyArray<T>> {
177
- return new GenericValidator({
178
- validator: (
179
- from: unknown,
180
- context?: ReadonlyArray<T>,
181
- self?: Validator<T, ReadonlyArray<T>>
182
- ): boolean | Failure<T> => {
183
- if (typeof from === 'string') {
184
- const v = context ?? values;
185
- const index = v.indexOf(from as T);
186
- return index >= 0 ? true : fail(`Invalid enumerated value "${from}" - expected: (${v.join(', ')})`);
187
- }
188
- return fail(`Not a string: "${JSON.stringify(from, undefined, 2)}`);
189
- }
190
- });
191
- }
192
-
193
- /**
194
- * Helper function to create a {@link Validation.Validator} which validates a literal value.
195
- * @param value - the literal value to be validated
196
- * @public
197
- */
198
- export function literal<T extends string | number | boolean | symbol | null | undefined>(
199
- value: T
200
- ): Validator<T> {
201
- return new GenericValidator({
202
- validator: (from: unknown, context?: unknown, self?: Validator<T>): boolean | Failure<T> => {
203
- return from === value
204
- ? true
205
- : fail(`Expected literal ${String(value)}, found "${JSON.stringify(from, undefined, 2)}`);
206
- }
207
- });
208
- }
209
-
210
- /**
211
- * Helper function to create a {@link Validation.Validator | Validator} which validates one
212
- * of several possible validated values.
213
- * @param validators - the {@link Validation.Validator | validators} to be considered.
214
- * @param params - Optional {@link Validation.Classes.OneOfValidatorConstructorParams | params} used to construct the validator.
215
- * @returns A new {@link Validator | Validator} which validates values that match any of
216
- * the supplied validators.
217
- * @public
218
- */
219
- export function oneOf<T, TC = unknown>(
220
- validators: Array<Validator<T, TC>>,
221
- params?: Omit<OneOfValidatorConstructorParams<T, TC>, 'validators'>
222
- ): OneOfValidator<T, TC> {
223
- return new OneOfValidator<T, TC>({ ...(params ?? {}), validators });
224
- }
225
-
226
- /**
227
- * Helper function to create a {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator} which
228
- * validates a value or object in place.
229
- * @param description - a description of the thing to be validated for use in error messages
230
- * @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
231
- * @returns A new {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator } which validates
232
- * the values using the supplied type guard.
233
- * @public
234
- */
235
- export function isA<T, TC = unknown>(
236
- description: string,
237
- guard: TypeGuardWithContext<T, TC>,
238
- params?: Omit<TypeGuardValidatorConstructorParams<T, TC>, 'description' | 'guard'>
239
- ): TypeGuardValidator<T, TC> {
240
- return new TypeGuardValidator({ description, guard, ...(params ?? {}) });
241
- }
242
-
243
- /**
244
- * Helper function to create a {@link Validation.Validator | Validator} using a
245
- * supplied {@link Validation.ValidatorFunc | validator function}.
246
- * @param validator - A {@link Validation.ValidatorFunc | validator function} that a
247
- * supplied unknown value matches some condition.
248
- * @returns A new {@link Validation.Validator | Validator} which validates the desired
249
- * value using the supplied function.
250
- * @public
251
- */
252
- export function generic<T, TC = unknown>(validator: ValidatorFunc<T, TC>): Validator<T, TC> {
253
- return new GenericValidator({ validator });
254
- }