io-ts-types
Version:
A collection of codecs and combinators for use with io-ts
54 lines (53 loc) • 1.97 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withValidate = void 0;
/**
* @since 0.4.3
*/
var t = __importStar(require("io-ts"));
var clone_1 = require("./clone");
/**
* Returns a clone of the given codec which uses the given `validate` function
*
* @example
* import { withValidate } from 'io-ts-types/lib/withValidate'
* import * as t from 'io-ts'
* import { PathReporter } from 'io-ts/lib/PathReporter'
* import { either, right } from 'fp-ts/lib/Either'
*
* const T = withValidate(t.number, (u, c) => either.map(t.number.validate(u, c), n => n * 2))
*
* assert.deepStrictEqual(T.decode(1), right(2))
* assert.deepStrictEqual(PathReporter.report(T.decode(null)), ['Invalid value null supplied to : number'])
*
* @since 0.4.3
*/
function withValidate(codec, validate, name) {
if (name === void 0) { name = codec.name; }
var r = clone_1.clone(codec);
r.validate = validate;
// tslint:disable-next-line: deprecation
r.decode = function (i) { return validate(i, t.getDefaultContext(r)); };
r.name = name;
return r;
}
exports.withValidate = withValidate;
;