@fgv/ts-utils 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.json +18 -0
  2. package/CHANGELOG.md +10 -1
  3. package/dist/ts-utils.d.ts +553 -54
  4. package/lib/index.d.ts +3 -1
  5. package/lib/index.d.ts.map +1 -1
  6. package/lib/index.js +6 -1
  7. package/lib/index.js.map +1 -1
  8. package/lib/packlets/collections/common.d.ts +6 -0
  9. package/lib/packlets/collections/common.d.ts.map +1 -0
  10. package/lib/packlets/collections/common.js +24 -0
  11. package/lib/packlets/collections/common.js.map +1 -0
  12. package/lib/packlets/collections/index.d.ts +8 -0
  13. package/lib/packlets/collections/index.d.ts.map +1 -0
  14. package/lib/packlets/collections/index.js +68 -0
  15. package/lib/packlets/collections/index.js.map +1 -0
  16. package/lib/packlets/collections/readonlyResultMap.d.ts +58 -0
  17. package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -0
  18. package/lib/packlets/collections/readonlyResultMap.js +24 -0
  19. package/lib/packlets/collections/readonlyResultMap.js.map +1 -0
  20. package/lib/packlets/collections/resultMap.d.ts +155 -0
  21. package/lib/packlets/collections/resultMap.d.ts.map +1 -0
  22. package/lib/packlets/collections/resultMap.js +216 -0
  23. package/lib/packlets/collections/resultMap.js.map +1 -0
  24. package/lib/packlets/collections/resultMapValidator.d.ts +82 -0
  25. package/lib/packlets/collections/resultMapValidator.d.ts.map +1 -0
  26. package/lib/packlets/collections/resultMapValidator.js +104 -0
  27. package/lib/packlets/collections/resultMapValidator.js.map +1 -0
  28. package/lib/packlets/collections/utils.d.ts +69 -0
  29. package/lib/packlets/collections/utils.d.ts.map +1 -0
  30. package/lib/packlets/collections/utils.js +116 -0
  31. package/lib/packlets/collections/utils.js.map +1 -0
  32. package/lib/packlets/collections/validatingResultMap.d.ts +66 -0
  33. package/lib/packlets/collections/validatingResultMap.d.ts.map +1 -0
  34. package/lib/packlets/collections/validatingResultMap.js +93 -0
  35. package/lib/packlets/collections/validatingResultMap.js.map +1 -0
  36. package/lib/packlets/conversion/baseConverter.d.ts +7 -3
  37. package/lib/packlets/conversion/baseConverter.d.ts.map +1 -1
  38. package/lib/packlets/conversion/baseConverter.js +10 -0
  39. package/lib/packlets/conversion/baseConverter.js.map +1 -1
  40. package/lib/packlets/conversion/converter.d.ts +19 -3
  41. package/lib/packlets/conversion/converter.d.ts.map +1 -1
  42. package/lib/packlets/conversion/converter.js.map +1 -1
  43. package/lib/packlets/conversion/converters.d.ts +42 -42
  44. package/lib/packlets/conversion/converters.d.ts.map +1 -1
  45. package/lib/packlets/conversion/converters.js +9 -9
  46. package/lib/packlets/conversion/converters.js.map +1 -1
  47. package/lib/packlets/conversion/defaultingConverter.d.ts +6 -2
  48. package/lib/packlets/conversion/defaultingConverter.d.ts.map +1 -1
  49. package/lib/packlets/conversion/defaultingConverter.js +7 -0
  50. package/lib/packlets/conversion/defaultingConverter.js.map +1 -1
  51. package/lib/packlets/conversion/objectConverter.d.ts +3 -3
  52. package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
  53. package/lib/packlets/conversion/objectConverter.js.map +1 -1
  54. package/lib/packlets/validation/field.d.ts +1 -1
  55. package/lib/packlets/validation/field.d.ts.map +1 -1
  56. package/lib/packlets/validation/field.js.map +1 -1
  57. package/lib/packlets/validation/genericValidator.d.ts +6 -2
  58. package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
  59. package/lib/packlets/validation/genericValidator.js +19 -2
  60. package/lib/packlets/validation/genericValidator.js.map +1 -1
  61. package/lib/packlets/validation/validator.d.ts +18 -1
  62. package/lib/packlets/validation/validator.d.ts.map +1 -1
  63. package/lib/packlets/validation/validator.js.map +1 -1
  64. package/lib/packlets/validation/validatorBase.d.ts +1 -1
  65. package/lib/packlets/validation/validatorBase.d.ts.map +1 -1
  66. package/lib/packlets/validation/validatorBase.js.map +1 -1
  67. package/package.json +1 -1
@@ -105,7 +105,8 @@ class GenericValidator {
105
105
  trait = trait !== null && trait !== void 0 ? trait : { type: 'function' };
106
106
  return new GenericValidator({
107
107
  validator: (from, context) => {
108
- if (this._validator(from, this._context(context)) === true) {
108
+ const result = this._validator(from, this._context(context));
109
+ if (result === true) {
109
110
  const constraintResult = constraint(from);
110
111
  if (typeof constraintResult === 'boolean') {
111
112
  return constraintResult
@@ -114,7 +115,7 @@ class GenericValidator {
114
115
  }
115
116
  return constraintResult;
116
117
  }
117
- return false;
118
+ return result;
118
119
  },
119
120
  traits: { constraints: [trait] }
120
121
  });
@@ -133,6 +134,22 @@ class GenericValidator {
133
134
  traits: { brand }
134
135
  });
135
136
  }
137
+ /**
138
+ * {@inheritdoc Validation.Validator.withFormattedError}
139
+ */
140
+ withFormattedError(formatter) {
141
+ return new GenericValidator({
142
+ validator: (from, context) => {
143
+ const result = this._validator(from, this._context(context));
144
+ if (result === true) {
145
+ return true;
146
+ }
147
+ /* c8 ignore next - defense in depth */
148
+ const message = result === false ? undefined : result.message;
149
+ return (0, base_1.fail)(formatter(from, message, this._context(context)));
150
+ }
151
+ });
152
+ }
136
153
  /**
137
154
  * Gets a default or explicit context.
138
155
  * @param explicitContext - Optional explicit context.
@@ -1 +1 @@
1
- {"version":3,"file":"genericValidator.js","sourceRoot":"","sources":["../../../src/packlets/validation/genericValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAgE;AAChE,qCAA4D;AAsB5D;;;GAGG;AACH,MAAa,gBAAgB;IAe3B;;;;OAIG;IACH,YAAmB,MAAyD;;QAC1E,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACrC,IAA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AE;SACjC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,SAAS,CAAmB,KAAQ;QACzC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,kCAAkC,IAAI,CAAC,KAAK,WAAW,KAAK,IAAI,CAAC,CAAC;QACpF,CAAC;QAED,OAAO,IAAI,gBAAgB,CAAkB;YAC3C,SAAS,EAAE,CAAC,IAAa,EAAE,OAAY,EAAE,EAAE;gBACzC,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAmC,CAAC;YACzF,CAAC;YACD,MAAM,EAAE,EAAE,KAAK,EAAE;SAClB,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACO,QAAQ,CAAC,eAAoB;QACrC,OAAO,eAAe,aAAf,eAAe,cAAf,eAAe,GAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC;IACzD,CAAC;CACF;AA1ID,4CA0IC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Brand, Failure, Result, fail, succeed } from '../base';\nimport { ConstraintTrait, ValidatorTraits } from './traits';\nimport { Constraint, Validator, ValidatorOptions } from './validator';\n\n/**\n * Type for a validation function, which validates that a supplied `unknown`\n * value is a valid value of type `<T>`, possibly as influenced by\n * an optionally-supplied validation context of type `<TC>`.\n * @public\n */\nexport type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;\n\n/**\n * Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface GenericValidatorConstructorParams<T, TC> {\n options?: ValidatorOptions<TC>;\n traits?: Partial<ValidatorTraits>;\n validator?: ValidatorFunc<T, TC>;\n}\n\n/**\n * Generic base implementation for an in-place {@link Validation.Validator | Validator}.\n * @public\n */\nexport class GenericValidator<T, TC = undefined> implements Validator<T, TC> {\n /**\n * {@inheritdoc Validation.Validator.traits}\n */\n public readonly traits: ValidatorTraits;\n\n /**\n * @internal\n */\n protected readonly _validator: ValidatorFunc<T, TC>;\n /**\n * @internal\n */\n protected readonly _options: ValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.\n * @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}\n * used to configure validation.\n */\n public constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>) {\n if (!params.validator) {\n throw new Error('No validator function supplied');\n }\n this._validator = params.validator;\n this._options = params.options ?? {};\n this.traits = new ValidatorTraits(params.traits);\n }\n\n /**\n * {@inheritdoc Validation.Validator.isOptional}\n */\n public get isOptional(): boolean {\n return this.traits.isOptional;\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public get brand(): string | undefined {\n return this.traits.brand;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validate}\n */\n public validate(from: unknown, context?: TC): Result<T> {\n const result = this._validator(from, this._context(context));\n if (typeof result === 'boolean') {\n return result ? succeed(from as T) : fail<T>('Invalid value');\n }\n return result;\n }\n\n /**\n * {@inheritdoc Validation.Validator.convert}\n */\n public convert(from: unknown, context?: TC): Result<T> {\n const result = this._validator(from, this._context(context));\n if (typeof result === 'boolean') {\n return result ? succeed(from as T) : fail<T>('Invalid value');\n }\n return result;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validateOptional}\n */\n public validateOptional(from: unknown, context?: TC): Result<T | undefined> {\n return from === undefined ? succeed(undefined) : this.validate(from, context);\n }\n\n /**\n * {@inheritdoc Validation.Validator.guard}\n */\n public guard(from: unknown, context?: TC): from is T {\n return this._validator(from, this._context(context)) === true;\n }\n\n /**\n * {@inheritdoc Validation.Validator.optional}\n */\n public optional(): Validator<T | undefined, TC> {\n return new GenericValidator({\n validator: (from: unknown, context?: TC) => {\n return from === undefined || this._validator(from, this._context(context));\n },\n traits: { isOptional: true }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.withConstraint}\n */\n public withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC> {\n trait = trait ?? { type: 'function' };\n return new GenericValidator({\n validator: (from: unknown, context?: TC): boolean | Failure<T> => {\n if (this._validator(from, this._context(context)) === true) {\n const constraintResult = constraint(from as T);\n if (typeof constraintResult === 'boolean') {\n return constraintResult\n ? true\n : fail(`Invalid value \"${JSON.stringify(from)}\": does not meet constraint.`);\n }\n return constraintResult;\n }\n return false;\n },\n traits: { constraints: [trait] }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC> {\n if (this.brand) {\n throw new Error(`Cannot replace existing brand \"${this.brand}\" with \"${brand}\".`);\n }\n\n return new GenericValidator<Brand<T, B>, TC>({\n validator: (from: unknown, context?: TC) => {\n return this._validator(from, this._context(context)) as boolean | Failure<Brand<T, B>>;\n },\n traits: { brand }\n });\n }\n\n /**\n * Gets a default or explicit context.\n * @param explicitContext - Optional explicit context.\n * @returns The appropriate context to use.\n * @internal\n */\n protected _context(explicitContext?: TC): TC | undefined {\n return explicitContext ?? this._options.defaultContext;\n }\n}\n"]}
1
+ {"version":3,"file":"genericValidator.js","sourceRoot":"","sources":["../../../src/packlets/validation/genericValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAgE;AAChE,qCAA4D;AAsB5D;;;GAGG;AACH,MAAa,gBAAgB;IAe3B;;;;OAIG;IACH,YAAmB,MAAyD;;QAC1E,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACnD,CAAC;IAED;;OAEG;IACH,IAAW,UAAU;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IAC3B,CAAC;IAED;;OAEG;IACI,QAAQ,CAAC,IAAa,EAAE,OAAY;QACzC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7D,IAAI,OAAO,MAAM,KAAK,SAAS,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC,CAAC,CAAC,IAAA,cAAO,EAAC,IAAS,CAAC,CAAC,CAAC,CAAC,IAAA,WAAI,EAAI,eAAe,CAAC,CAAC;QAChE,CAAC;QACD,OAAO,MAAM,CAAC;IA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Y,EAAE,EAAE;gBACzC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;gBAC7D,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;oBACpB,OAAO,IAAI,CAAC;gBACd,CAAC;gBACD,uCAAuC;gBACvC,MAAM,OAAO,GAAG,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;gBAC9D,OAAO,IAAA,WAAI,EAAC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAChE,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACO,QAAQ,CAAC,eAAoB;QACrC,OAAO,eAAe,aAAf,eAAe,cAAf,eAAe,GAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC;IACzD,CAAC;CACF;AA5JD,4CA4JC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Brand, Failure, Result, fail, succeed } from '../base';\nimport { ConstraintTrait, ValidatorTraits } from './traits';\nimport { Constraint, ValidationErrorFormatter, Validator, ValidatorOptions } from './validator';\n\n/**\n * Type for a validation function, which validates that a supplied `unknown`\n * value is a valid value of type `<T>`, possibly as influenced by\n * an optionally-supplied validation context of type `<TC>`.\n * @public\n */\nexport type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;\n\n/**\n * Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface GenericValidatorConstructorParams<T, TC> {\n options?: ValidatorOptions<TC>;\n traits?: Partial<ValidatorTraits>;\n validator?: ValidatorFunc<T, TC>;\n}\n\n/**\n * Generic base implementation for an in-place {@link Validation.Validator | Validator}.\n * @public\n */\nexport class GenericValidator<T, TC = unknown> implements Validator<T, TC> {\n /**\n * {@inheritdoc Validation.Validator.traits}\n */\n public readonly traits: ValidatorTraits;\n\n /**\n * @internal\n */\n protected readonly _validator: ValidatorFunc<T, TC>;\n /**\n * @internal\n */\n protected readonly _options: ValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.\n * @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}\n * used to configure validation.\n */\n public constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>) {\n if (!params.validator) {\n throw new Error('No validator function supplied');\n }\n this._validator = params.validator;\n this._options = params.options ?? {};\n this.traits = new ValidatorTraits(params.traits);\n }\n\n /**\n * {@inheritdoc Validation.Validator.isOptional}\n */\n public get isOptional(): boolean {\n return this.traits.isOptional;\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public get brand(): string | undefined {\n return this.traits.brand;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validate}\n */\n public validate(from: unknown, context?: TC): Result<T> {\n const result = this._validator(from, this._context(context));\n if (typeof result === 'boolean') {\n return result ? succeed(from as T) : fail<T>('Invalid value');\n }\n return result;\n }\n\n /**\n * {@inheritdoc Validation.Validator.convert}\n */\n public convert(from: unknown, context?: TC): Result<T> {\n const result = this._validator(from, this._context(context));\n if (typeof result === 'boolean') {\n return result ? succeed(from as T) : fail<T>('Invalid value');\n }\n return result;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validateOptional}\n */\n public validateOptional(from: unknown, context?: TC): Result<T | undefined> {\n return from === undefined ? succeed(undefined) : this.validate(from, context);\n }\n\n /**\n * {@inheritdoc Validation.Validator.guard}\n */\n public guard(from: unknown, context?: TC): from is T {\n return this._validator(from, this._context(context)) === true;\n }\n\n /**\n * {@inheritdoc Validation.Validator.optional}\n */\n public optional(): Validator<T | undefined, TC> {\n return new GenericValidator({\n validator: (from: unknown, context?: TC) => {\n return from === undefined || this._validator(from, this._context(context));\n },\n traits: { isOptional: true }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.withConstraint}\n */\n public withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC> {\n trait = trait ?? { type: 'function' };\n return new GenericValidator({\n validator: (from: unknown, context?: TC): boolean | Failure<T> => {\n const result = this._validator(from, this._context(context));\n if (result === true) {\n const constraintResult = constraint(from as T);\n if (typeof constraintResult === 'boolean') {\n return constraintResult\n ? true\n : fail(`Invalid value \"${JSON.stringify(from)}\": does not meet constraint.`);\n }\n return constraintResult;\n }\n return result;\n },\n traits: { constraints: [trait] }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC> {\n if (this.brand) {\n throw new Error(`Cannot replace existing brand \"${this.brand}\" with \"${brand}\".`);\n }\n\n return new GenericValidator<Brand<T, B>, TC>({\n validator: (from: unknown, context?: TC) => {\n return this._validator(from, this._context(context)) as boolean | Failure<Brand<T, B>>;\n },\n traits: { brand }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.withFormattedError}\n */\n public withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC> {\n return new GenericValidator<T, TC>({\n validator: (from: unknown, context?: TC) => {\n const result = this._validator(from, this._context(context));\n if (result === true) {\n return true;\n }\n /* c8 ignore next - defense in depth */\n const message = result === false ? undefined : result.message;\n return fail(formatter(from, message, this._context(context)));\n }\n });\n }\n\n /**\n * Gets a default or explicit context.\n * @param explicitContext - Optional explicit context.\n * @returns The appropriate context to use.\n * @internal\n */\n protected _context(explicitContext?: TC): TC | undefined {\n return explicitContext ?? this._options.defaultContext;\n }\n}\n"]}
@@ -15,12 +15,21 @@ export interface ValidatorOptions<TC> {
15
15
  * @public
16
16
  */
17
17
  export type Constraint<T> = (val: T) => boolean | Failure<T>;
18
+ /**
19
+ * Formats an incoming error message and value that failed validation.
20
+ * @param val - The value that failed validation.
21
+ * @param message - The default error message, if any.
22
+ * @param context - Optional validation context.
23
+ * @returns The formatted error message.
24
+ * @public
25
+ */
26
+ export type ValidationErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;
18
27
  /**
19
28
  * In-place validation that a supplied unknown matches some
20
29
  * required characteristics (type, values, etc).
21
30
  * @public
22
31
  */
23
- export interface Validator<T, TC = undefined> {
32
+ export interface Validator<T, TC = unknown> {
24
33
  /**
25
34
  * {@link Validation.ValidatorTraits | Traits} describing this validation.
26
35
  */
@@ -87,5 +96,13 @@ export interface Validator<T, TC = undefined> {
87
96
  * @param brand - The brand to be applied.
88
97
  */
89
98
  withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
99
+ /**
100
+ * Creates a new {@link Validation.Validator | in-place validator} which
101
+ * is derived from this one but which returns an error message supplied
102
+ * by the provided formatter if an error occurs.
103
+ * @param formatter - The error message formatter to be applied.
104
+ * @returns A new {@link Validation.Validator | Validator}.
105
+ */
106
+ withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC>;
90
107
  }
91
108
  //# sourceMappingURL=validator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/validator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE5D;;;GAGG;AAEH,MAAM,WAAW,gBAAgB,CAAC,EAAE;IAClC,cAAc,CAAC,EAAE,EAAE,CAAC;CACrB;AAED;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7D;;;;GAIG;AAEH,MAAM,WAAW,SAAS,CAAC,CAAC,EAAE,EAAE,GAAG,SAAS;IAC1C;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IAEjC;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAEnC;;;;;;;OAOG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEjD;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEhD;;;;;;;OAOG;IACH,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAErE;;;;OAIG;IACH,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAA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IAE7B;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAEnC;;;;;;;OAOG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEjD;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEhD;;;;;;;OAOG;IACH,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAErE;;;;OAIG;IACH,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,IAAI,IAAI,CAAC,CAAC;IAE9C;;;OAGG;IACH,QAAQ,IAAI,SAAS,CAAC,CAAC,GAAG,SAAS,EAAE,EAAE,CAAC,CAAC;IAEzC;;;;;;;OAOG;IACH,cAAc,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAErF;;;;OAIG;IACH,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAElE;;;;;;OAMG;IACH,kBAAkB,CAAC,SAAS,EAAE,wBAAwB,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;CAC/E"}
@@ -1 +1 @@
1
- {"version":3,"file":"validator.js","sourceRoot":"","sources":["../../../src/packlets/validation/validator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Brand, Failure, Result } from '../base';\nimport { ConstraintTrait, ValidatorTraits } from './traits';\n\n/**\n * Options that apply to any {@link Validation.Validator | Validator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ValidatorOptions<TC> {\n defaultContext?: TC;\n}\n\n/**\n * A {@link Validation.Constraint | Constraint<T>} function returns\n * `true` if the supplied value meets the constraint. Can return\n * {@link Failure} with an error message or simply return `false`\n * for a default message.\n * @public\n */\nexport type Constraint<T> = (val: T) => boolean | Failure<T>;\n\n/**\n * In-place validation that a supplied unknown matches some\n * required characteristics (type, values, etc).\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface Validator<T, TC = undefined> {\n /**\n * {@link Validation.ValidatorTraits | Traits} describing this validation.\n */\n readonly traits: ValidatorTraits;\n\n /**\n * Indicates whether this element is explicitly optional.\n */\n readonly isOptional: boolean;\n\n /**\n * The brand for a branded type.\n */\n readonly brand: string | undefined;\n\n /**\n * Tests to see if a supplied `unknown` value matches this validation. All\n * validate calls are guaranteed to return the entity passed in on Success.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, validated value,\n * or {@link Failure} with an error message if validation fails.\n */\n validate(from: unknown, context?: TC): Result<T>;\n\n /**\n * Tests to see if a supplied 'unknown' value matches this validation. In\n * contrast to {@link Validator.validate | validate}, makes no guarantees\n * about the identity of the returned value.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, conversion value,\n * or {@link Failure} with an error message if conversion fails.\n */\n convert(from: unknown, context?: TC): Result<T>;\n\n /**\n * Tests to see if a supplied `unknown` value matches this\n * validation. Accepts `undefined`.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, validated value,\n * or {@link Failure} with an error message if validation fails.\n */\n validateOptional(from: unknown, context?: TC): Result<T | undefined>;\n\n /**\n * Non-throwing type guard\n * @param from - The value to be tested.\n * @param context - Optional validation context.\n */\n guard(from: unknown, context?: TC): from is T;\n\n /**\n * Creates an {@link Validation.Validator | in-place validator}\n * which is derived from this one but which also matches `undefined`.\n */\n optional(): Validator<T | undefined, TC>;\n\n /**\n * Creates an {@link Validation.Validator | in-place validator}\n * which is derived from this one but which applies additional constraints.\n * @param constraint - the constraint to be applied\n * @param trait - As optional {@link Validation.ConstraintTrait | ConstraintTrait}\n * to be applied to the resulting {@link Validation.Validator | Validator}.\n * @returns A new {@link Validation.Validator | Validator}.\n */\n withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC>;\n\n /**\n * Creates a new {@link Validation.Validator | in-place validator} which\n * is derived from this one but which matches a branded result.\n * @param brand - The brand to be applied.\n */\n withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;\n}\n"]}
1
+ {"version":3,"file":"validator.js","sourceRoot":"","sources":["../../../src/packlets/validation/validator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Brand, Failure, Result } from '../base';\nimport { ConstraintTrait, ValidatorTraits } from './traits';\n\n/**\n * Options that apply to any {@link Validation.Validator | Validator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ValidatorOptions<TC> {\n defaultContext?: TC;\n}\n\n/**\n * A {@link Validation.Constraint | Constraint<T>} function returns\n * `true` if the supplied value meets the constraint. Can return\n * {@link Failure} with an error message or simply return `false`\n * for a default message.\n * @public\n */\nexport type Constraint<T> = (val: T) => boolean | Failure<T>;\n\n/**\n * Formats an incoming error message and value that failed validation.\n * @param val - The value that failed validation.\n * @param message - The default error message, if any.\n * @param context - Optional validation context.\n * @returns The formatted error message.\n * @public\n */\nexport type ValidationErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;\n\n/**\n * In-place validation that a supplied unknown matches some\n * required characteristics (type, values, etc).\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface Validator<T, TC = unknown> {\n /**\n * {@link Validation.ValidatorTraits | Traits} describing this validation.\n */\n readonly traits: ValidatorTraits;\n\n /**\n * Indicates whether this element is explicitly optional.\n */\n readonly isOptional: boolean;\n\n /**\n * The brand for a branded type.\n */\n readonly brand: string | undefined;\n\n /**\n * Tests to see if a supplied `unknown` value matches this validation. All\n * validate calls are guaranteed to return the entity passed in on Success.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, validated value,\n * or {@link Failure} with an error message if validation fails.\n */\n validate(from: unknown, context?: TC): Result<T>;\n\n /**\n * Tests to see if a supplied 'unknown' value matches this validation. In\n * contrast to {@link Validator.validate | validate}, makes no guarantees\n * about the identity of the returned value.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, conversion value,\n * or {@link Failure} with an error message if conversion fails.\n */\n convert(from: unknown, context?: TC): Result<T>;\n\n /**\n * Tests to see if a supplied `unknown` value matches this\n * validation. Accepts `undefined`.\n * @param from - The `unknown` value to be tested.\n * @param context - Optional validation context.\n * @returns {@link Success} with the typed, validated value,\n * or {@link Failure} with an error message if validation fails.\n */\n validateOptional(from: unknown, context?: TC): Result<T | undefined>;\n\n /**\n * Non-throwing type guard\n * @param from - The value to be tested.\n * @param context - Optional validation context.\n */\n guard(from: unknown, context?: TC): from is T;\n\n /**\n * Creates an {@link Validation.Validator | in-place validator}\n * which is derived from this one but which also matches `undefined`.\n */\n optional(): Validator<T | undefined, TC>;\n\n /**\n * Creates an {@link Validation.Validator | in-place validator}\n * which is derived from this one but which applies additional constraints.\n * @param constraint - the constraint to be applied\n * @param trait - As optional {@link Validation.ConstraintTrait | ConstraintTrait}\n * to be applied to the resulting {@link Validation.Validator | Validator}.\n * @returns A new {@link Validation.Validator | Validator}.\n */\n withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC>;\n\n /**\n * Creates a new {@link Validation.Validator | in-place validator} which\n * is derived from this one but which matches a branded result.\n * @param brand - The brand to be applied.\n */\n withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;\n\n /**\n * Creates a new {@link Validation.Validator | in-place validator} which\n * is derived from this one but which returns an error message supplied\n * by the provided formatter if an error occurs.\n * @param formatter - The error message formatter to be applied.\n * @returns A new {@link Validation.Validator | Validator}.\n */\n withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC>;\n}\n"]}
@@ -8,7 +8,7 @@ export type ValidatorBaseConstructorParams<T, TC> = Omit<GenericValidatorConstru
8
8
  * Abstract base helper class for specific validator implementations
9
9
  * @internal
10
10
  */
11
- export declare abstract class ValidatorBase<T, TC = undefined> extends GenericValidator<T, TC> {
11
+ export declare abstract class ValidatorBase<T, TC = unknown> extends GenericValidator<T, TC> {
12
12
  /**
13
13
  * Inner constructor
14
14
  * @param params - Initialization params.
@@ -1 +1 @@
1
- {"version":3,"file":"validatorBase.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/validatorBase.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,gBAAgB,EAAE,iCAAiC,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;GAEG;AACH,MAAM,MAAM,8BAA8B,CAAC,CAAC,EAAE,EAAE,IAAI,IAAI,CACtD,iCAAiC,CAAC,CAAC,EAAE,EAAE,CAAC,EACxC,WAAW,CACZ,CAAC;AAEF;;;GAGG;AACH,8BAAsB,aAAa,CAAC,CAAC,EAAE,EAAE,GAAG,SAAS,CAAE,SAAQ,gBAAgB,CAAC,CAAC,EAAE,EAAE,CAAC;IACpF;;;;OAIG;IACH,SAAS,aAAa,MAAM,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAO5E;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CAChF"}
1
+ {"version":3,"file":"validatorBase.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/validatorBase.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,gBAAgB,EAAE,iCAAiC,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;GAEG;AACH,MAAM,MAAM,8BAA8B,CAAC,CAAC,EAAE,EAAE,IAAI,IAAI,CACtD,iCAAiC,CAAC,CAAC,EAAE,EAAE,CAAC,EACxC,WAAW,CACZ,CAAC;AAEF;;;GAGG;AACH,8BAAsB,aAAa,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,CAAE,SAAQ,gBAAgB,CAAC,CAAC,EAAE,EAAE,CAAC;IAClF;;;;OAIG;IACH,SAAS,aAAa,MAAM,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAO5E;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CAChF"}
@@ -1 +1 @@
1
- {"version":3,"file":"validatorBase.js","sourceRoot":"","sources":["../../../src/packlets/validation/validatorBase.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,yDAAyF;AAYzF;;;GAGG;AACH,MAAsB,aAAiC,SAAQ,mCAAuB;IACpF;;;;OAIG;IACH,YAAsB,MAAsD;QAC1E,KAAK,iBACH,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,IACxD,MAAM,EACT,CAAC;IACL,CAAC;CAWF;AAtBD,sCAsBC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';\n\nimport { Failure } from '../base';\n\n/**\n * @internal\n */\nexport type ValidatorBaseConstructorParams<T, TC> = Omit<\n GenericValidatorConstructorParams<T, TC>,\n 'validator'\n>;\n\n/**\n * Abstract base helper class for specific validator implementations\n * @internal\n */\nexport abstract class ValidatorBase<T, TC = undefined> extends GenericValidator<T, TC> {\n /**\n * Inner constructor\n * @param params - Initialization params.\n * @internal\n */\n protected constructor(params: Partial<ValidatorBaseConstructorParams<T, TC>>) {\n super({\n validator: (from, context) => this._validate(from, context),\n ...params\n });\n }\n\n /**\n * Abstract validation method to me implemented by derived classes.\n * @param from - Value to be converted.\n * @param context - Optional validation context.\n * @returns `true` if `from` is valid, {@link Failure | Failure<T>}\n * with an error message if `from` is invalid.\n * @internal\n */\n protected abstract _validate(from: unknown, context?: TC): boolean | Failure<T>;\n}\n"]}
1
+ {"version":3,"file":"validatorBase.js","sourceRoot":"","sources":["../../../src/packlets/validation/validatorBase.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,yDAAyF;AAYzF;;;GAGG;AACH,MAAsB,aAA+B,SAAQ,mCAAuB;IAClF;;;;OAIG;IACH,YAAsB,MAAsD;QAC1E,KAAK,iBACH,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,IACxD,MAAM,EACT,CAAC;IACL,CAAC;CAWF;AAtBD,sCAsBC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';\n\nimport { Failure } from '../base';\n\n/**\n * @internal\n */\nexport type ValidatorBaseConstructorParams<T, TC> = Omit<\n GenericValidatorConstructorParams<T, TC>,\n 'validator'\n>;\n\n/**\n * Abstract base helper class for specific validator implementations\n * @internal\n */\nexport abstract class ValidatorBase<T, TC = unknown> extends GenericValidator<T, TC> {\n /**\n * Inner constructor\n * @param params - Initialization params.\n * @internal\n */\n protected constructor(params: Partial<ValidatorBaseConstructorParams<T, TC>>) {\n super({\n validator: (from, context) => this._validate(from, context),\n ...params\n });\n }\n\n /**\n * Abstract validation method to me implemented by derived classes.\n * @param from - Value to be converted.\n * @param context - Optional validation context.\n * @returns `true` if `from` is valid, {@link Failure | Failure<T>}\n * with an error message if `from` is invalid.\n * @internal\n */\n protected abstract _validate(from: unknown, context?: TC): boolean | Failure<T>;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-utils.d.ts",