@fgv/ts-utils 3.0.1-alpha.0 → 3.0.1-alpha.2

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 (31) hide show
  1. package/dist/ts-utils.d.ts +151 -183
  2. package/dist/tsdoc-metadata.json +1 -1
  3. package/lib/packlets/conversion/baseConverter.d.ts +0 -4
  4. package/lib/packlets/conversion/baseConverter.d.ts.map +1 -1
  5. package/lib/packlets/conversion/baseConverter.js +0 -6
  6. package/lib/packlets/conversion/baseConverter.js.map +1 -1
  7. package/lib/packlets/conversion/converter.d.ts +1 -10
  8. package/lib/packlets/conversion/converter.d.ts.map +1 -1
  9. package/lib/packlets/conversion/converter.js.map +1 -1
  10. package/lib/packlets/conversion/converters.js +13 -13
  11. package/lib/packlets/conversion/converters.js.map +1 -1
  12. package/lib/packlets/conversion/defaultingConverter.d.ts +0 -4
  13. package/lib/packlets/conversion/defaultingConverter.d.ts.map +1 -1
  14. package/lib/packlets/conversion/defaultingConverter.js +0 -6
  15. package/lib/packlets/conversion/defaultingConverter.js.map +1 -1
  16. package/lib/packlets/validation/genericValidator.d.ts +2 -2
  17. package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
  18. package/lib/packlets/validation/genericValidator.js +2 -2
  19. package/lib/packlets/validation/genericValidator.js.map +1 -1
  20. package/lib/packlets/validation/index.d.ts +0 -1
  21. package/lib/packlets/validation/index.d.ts.map +1 -1
  22. package/lib/packlets/validation/index.js +0 -1
  23. package/lib/packlets/validation/index.js.map +1 -1
  24. package/lib/packlets/validation/validator.d.ts +9 -4
  25. package/lib/packlets/validation/validator.d.ts.map +1 -1
  26. package/lib/packlets/validation/validator.js.map +1 -1
  27. package/package.json +13 -13
  28. package/lib/packlets/validation/convalidator.d.ts +0 -22
  29. package/lib/packlets/validation/convalidator.d.ts.map +0 -1
  30. package/lib/packlets/validation/convalidator.js +0 -24
  31. package/lib/packlets/validation/convalidator.js.map +0 -1
@@ -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,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;IAChB,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,IAAa,EAAE,OAAY;QAC5C,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ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IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;oBAC3D,MAAM,gBAAgB,GAAG,UAAU,CAAC,IAAS,CAAC,CAAC;oBAC/C,IAAI,OAAO,gBAAgB,KAAK,SAAS,EAAE,CAAC;wBAC1C,OAAO,gBAAgB;4BACrB,CAAC,CAAC,IAAI;4BACN,CAAC,CAAC,IAAA,WAAI,EAAC,kBAAkB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;oBAClF,CAAC;oBACD,OAAO,gBAAgB,CAAC;gBAC1B,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;YACD,MAAM,EAAE,EAAE,WAAW,EAAE,CAAC,KAAK,CAAC,EAAE;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.convalidate}\n */\n public convalidate(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,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C,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,5 +1,4 @@
1
1
  export * from './common';
2
- export * from './convalidator';
3
2
  export * from './traits';
4
3
  export * from './validator';
5
4
  import * as Classes from './classes';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":"AAsBA,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAE5B,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAC;AAC3C,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":"AAsBA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAE5B,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAC;AAC3C,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -49,7 +49,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.Validators = exports.Classes = exports.Base = void 0;
51
51
  __exportStar(require("./common"), exports);
52
- __exportStar(require("./convalidator"), exports);
53
52
  __exportStar(require("./traits"), exports);
54
53
  __exportStar(require("./validator"), exports);
55
54
  const Classes = __importStar(require("./classes"));
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAAyB;AACzB,iDAA+B;AAC/B,2CAAyB;AACzB,8CAA4B;AAE5B,mDAAqC;AAItB,0BAAO;AAHtB,yDAA2C;AAGlC,oBAAI;AAFb,yDAA2C;AAEnB,gCAAU","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\nexport * from './common';\nexport * from './convalidator';\nexport * from './traits';\nexport * from './validator';\n\nimport * as Classes from './classes';\nimport * as Base from './genericValidator';\nimport * as Validators from './validators';\n\nexport { Base, Classes, Validators };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B;AAE5B,mDAAqC;AAItB,0BAAO;AAHtB,yDAA2C;AAGlC,oBAAI;AAFb,yDAA2C;AAEnB,gCAAU","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\nexport * from './common';\nexport * from './traits';\nexport * from './validator';\n\nimport * as Classes from './classes';\nimport * as Base from './genericValidator';\nimport * as Validators from './validators';\n\nexport { Base, Classes, Validators };\n"]}
@@ -1,5 +1,4 @@
1
1
  import { Brand, Failure, Result } from '../base';
2
- import { Convalidator } from './convalidator';
3
2
  import { ConstraintTrait, ValidatorTraits } from './traits';
4
3
  /**
5
4
  * Options that apply to any {@link Validation.Validator | Validator}.
@@ -21,7 +20,7 @@ export type Constraint<T> = (val: T) => boolean | Failure<T>;
21
20
  * required characteristics (type, values, etc).
22
21
  * @public
23
22
  */
24
- export interface Validator<T, TC = undefined> extends Convalidator<T, TC> {
23
+ export interface Validator<T, TC = undefined> {
25
24
  /**
26
25
  * {@link Validation.ValidatorTraits | Traits} describing this validation.
27
26
  */
@@ -44,9 +43,15 @@ export interface Validator<T, TC = undefined> extends Convalidator<T, TC> {
44
43
  */
45
44
  validate(from: unknown, context?: TC): Result<T>;
46
45
  /**
47
- * {@inheritdoc Validation.Convalidator.convalidate}
46
+ * Tests to see if a supplied 'unknown' value matches this validation. In
47
+ * contrast to {@link Validator.validate | validate}, makes no guarantees
48
+ * about the identity of the returned value.
49
+ * @param from - The `unknown` value to be tested.
50
+ * @param context - Optional validation context.
51
+ * @returns {@link Success} with the typed, conversion value,
52
+ * or {@link Failure} with an error message if conversion fails.
48
53
  */
49
- convalidate(from: unknown, context?: TC): Result<T>;
54
+ convert(from: unknown, context?: TC): Result<T>;
50
55
  /**
51
56
  * Tests to see if a supplied `unknown` value matches this
52
57
  * validation. Accepts `undefined`.
@@ -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,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,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,CAAE,SAAQ,YAAY,CAAC,CAAC,EAAE,EAAE,CAAC;IACvE;;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;;;;;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@ -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 { Convalidator } from './convalidator';\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> extends Convalidator<T, TC> {\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 * {@inheritdoc Validation.Convalidator.convalidate}\n */\n convalidate(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 * 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
- "version": "3.0.1-alpha.0",
3
+ "version": "3.0.1-alpha.2",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-utils.d.ts",
@@ -29,14 +29,14 @@
29
29
  "homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#readme",
30
30
  "devDependencies": {
31
31
  "@jest/expect-utils": "^29.7.0",
32
- "@microsoft/api-documenter": "^7.23.17",
33
- "@microsoft/api-extractor": "^7.39.1",
34
- "@types/jest": "^29.5.11",
35
- "@types/luxon": "^3.4.1",
32
+ "@microsoft/api-documenter": "^7.23.20",
33
+ "@microsoft/api-extractor": "^7.39.4",
34
+ "@types/jest": "^29.5.12",
35
+ "@types/luxon": "^3.4.2",
36
36
  "@types/mustache": "^4.2.5",
37
- "@types/node": "^20.11.5",
38
- "@typescript-eslint/eslint-plugin": "^6.19.0",
39
- "@typescript-eslint/parser": "^6.19.0",
37
+ "@types/node": "^20.11.16",
38
+ "@typescript-eslint/eslint-plugin": "^6.20.0",
39
+ "@typescript-eslint/parser": "^6.20.0",
40
40
  "eslint": "^8.56.0",
41
41
  "eslint-config-standard": "^17.1.0",
42
42
  "eslint-plugin-import": "^2.29.1",
@@ -46,16 +46,16 @@
46
46
  "jest-extended": "^4.0.2",
47
47
  "jest-matcher-utils": "^29.7.0",
48
48
  "rimraf": "^5.0.5",
49
- "ts-jest": "^29.1.1",
49
+ "ts-jest": "^29.1.2",
50
50
  "ts-node": "^10.9.2",
51
51
  "typescript": "^5.3.3",
52
52
  "eslint-plugin-n": "^16.6.2",
53
53
  "jest-snapshot": "~29.7.0",
54
- "@rushstack/heft": "~0.64.0",
55
- "@rushstack/heft-node-rig": "~2.4.0",
56
- "@rushstack/eslint-config": "~3.6.0",
54
+ "@rushstack/heft": "~0.64.3",
55
+ "@rushstack/heft-node-rig": "~2.4.5",
56
+ "@rushstack/eslint-config": "~3.6.2",
57
57
  "@types/heft-jest": "1.0.6",
58
- "@rushstack/heft-jest-plugin": "~0.11.0",
58
+ "@rushstack/heft-jest-plugin": "~0.11.3",
59
59
  "eslint-plugin-tsdoc": "~0.2.17"
60
60
  },
61
61
  "dependencies": {
@@ -1,22 +0,0 @@
1
- import { Result } from '../base';
2
- /**
3
- * Helper interface to make it easy to use validators and converters interchangeably.
4
- * @public
5
- */
6
- export interface Convalidator<T, TC = unknown> {
7
- /**
8
- * Indicates whether the corresponding property can be omitted.
9
- */
10
- readonly isOptional: boolean;
11
- /**
12
- * Converts or validates from `unknown` to `<T>`. For objects and arrays, makes no
13
- * guarantees wrt in-place validation or unrecognized properties.
14
- * @param from - The `unknown` to be converted or validated.
15
- * @param context - An optional conversion context of type `<TC>` to be used in
16
- * the operation.
17
- * @returns A {@link Result} with a {@link Success} and a value on success or an
18
- * {@link Failure} with a a message on failure.
19
- */
20
- convalidate(from: unknown, context?: TC): Result<T>;
21
- }
22
- //# sourceMappingURL=convalidator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"convalidator.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/convalidator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC;;;GAGG;AAEH,MAAM,WAAW,YAAY,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO;IAC3C;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B;;;;;;;;OAQG;IACH,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;CACrD"}
@@ -1,24 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2020 Erik Fortune
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining a copy
6
- * of this software and associated documentation files (the "Software"), to deal
7
- * in the Software without restriction, including without limitation the rights
8
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- * copies of the Software, and to permit persons to whom the Software is
10
- * furnished to do so, subject to the following conditions:
11
- *
12
- * The above copyright notice and this permission notice shall be included in all
13
- * copies or substantial portions of the Software.
14
- *
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- * SOFTWARE.
22
- */
23
- Object.defineProperty(exports, "__esModule", { value: true });
24
- //# sourceMappingURL=convalidator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"convalidator.js","sourceRoot":"","sources":["../../../src/packlets/validation/convalidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG","sourcesContent":["/*\n * Copyright (c) 2020 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 { Result } from '../base';\n\n/**\n * Helper interface to make it easy to use validators and converters interchangeably.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface Convalidator<T, TC = unknown> {\n /**\n * Indicates whether the corresponding property can be omitted.\n */\n readonly isOptional: boolean;\n\n /**\n * Converts or validates from `unknown` to `<T>`. For objects and arrays, makes no\n * guarantees wrt in-place validation or unrecognized properties.\n * @param from - The `unknown` to be converted or validated.\n * @param context - An optional conversion context of type `<TC>` to be used in\n * the operation.\n * @returns A {@link Result} with a {@link Success} and a value on success or an\n * {@link Failure} with a a message on failure.\n */\n convalidate(from: unknown, context?: TC): Result<T>;\n}\n"]}