io-ts-types
Version:
A collection of codecs and combinators for use with io-ts
49 lines (48 loc) • 1.75 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.withEncode = void 0;
/**
* @since 0.5.12
*/
var t = __importStar(require("io-ts"));
/**
* Returns a clone of the given codec which uses the given `encode` function
*
* @example
* import { withEncode } from 'io-ts-types/lib/withEncode'
* import * as t from 'io-ts'
* import { PathReporter } from 'io-ts/lib/PathReporter'
* import { right } from 'fp-ts/lib/Either'
*
* const T = withEncode(t.number, String)
*
* assert.deepStrictEqual(T.decode(1), right(1))
* assert.deepStrictEqual(T.encode(1), '1')
* assert.deepStrictEqual(PathReporter.report(T.decode('str')), ['Invalid value "str" supplied to : number'])
*
* @since 0.5.12
*/
function withEncode(codec, encode, name) {
if (name === void 0) { name = codec.name; }
return new t.Type(name, codec.is, codec.validate, encode);
}
exports.withEncode = withEncode;
;