io-ts
Advanced tools
Comparing version 1.10.4 to 2.0.0
@@ -17,2 +17,8 @@ # Changelog | ||
# 2.0.0 | ||
- **Breaking Change** | ||
- upgrade to `fp-ts@2.x` (@gcanti) | ||
- move `fp-ts` to `peerDependencies` (@gcanti) | ||
# 1.10.4 | ||
@@ -19,0 +25,0 @@ |
132
es6/index.js
@@ -25,3 +25,5 @@ var __extends = (this && this.__extends) || (function () { | ||
}; | ||
import { left, right } from 'fp-ts/lib/Either'; | ||
import { either, isLeft, left, right } from 'fp-ts/lib/Either'; | ||
var map = either.map; | ||
var chain = either.chain; | ||
/** | ||
@@ -49,3 +51,3 @@ * @since 1.0.0 | ||
if (name === void 0) { name = "pipe(" + this.name + ", " + ab.name + ")"; } | ||
return new Type(name, ab.is, function (i, c) { return _this.validate(i, c).chain(function (a) { return ab.validate(a, c); }); }, this.encode === identity && ab.encode === identity ? identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
return new Type(name, ab.is, function (i, c) { return chain(_this.validate(i, c), function (a) { return ab.validate(a, c); }); }, this.encode === identity && ab.encode === identity ? identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
}; | ||
@@ -424,9 +426,14 @@ Type.prototype.asDecoder = function () { | ||
return new ArrayType(name, function (u) { return UnknownArray.is(u) && u.every(codec.is); }, function (u, c) { | ||
return UnknownArray.validate(u, c).chain(function (us) { | ||
return chain(UnknownArray.validate(u, c), function (us) { | ||
var len = us.length; | ||
var as = us; | ||
var errors = []; | ||
var _loop_1 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var ui = us[i]; | ||
codec.validate(ui, appendContext(c, String(i), codec, ui)).fold(function (e) { return pushAll(errors, e); }, function (ai) { | ||
var result = codec.validate(ui, appendContext(c, String(i), codec, ui)); | ||
if (isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var ai = result.right; | ||
if (ai !== ui) { | ||
@@ -438,6 +445,3 @@ if (as === us) { | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_1(i); | ||
} | ||
} | ||
@@ -499,6 +503,6 @@ return errors.length > 0 ? failures(errors) : success(as); | ||
}, function (u, c) { | ||
return UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(UnknownRecord.validate(u, c), function (o) { | ||
var a = o; | ||
var errors = []; | ||
var _loop_2 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
@@ -513,3 +517,8 @@ if (!hasOwnProperty.call(a, k)) { | ||
var type_1 = types[i]; | ||
type_1.validate(ak, appendContext(c, k, type_1, ak)).fold(function (e) { return pushAll(errors, e); }, function (vak) { | ||
var result = type_1.validate(ak, appendContext(c, k, type_1, ak)); | ||
if (isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var vak = result.right; | ||
if (vak !== ak) { | ||
@@ -522,6 +531,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_2(i); | ||
} | ||
} | ||
@@ -582,14 +588,17 @@ return errors.length > 0 ? failures(errors) : success(a); | ||
}, function (u, c) { | ||
return UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(UnknownRecord.validate(u, c), function (o) { | ||
var a = o; | ||
var errors = []; | ||
var _loop_3 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
var ak = a[k]; | ||
var type_2 = props[k]; | ||
type_2.validate(ak, appendContext(c, k, type_2, ak)).fold(function (e) { | ||
var result = type_2.validate(ak, appendContext(c, k, type_2, ak)); | ||
if (isLeft(result)) { | ||
if (ak !== undefined) { | ||
pushAll(errors, e); | ||
pushAll(errors, result.left); | ||
} | ||
}, function (vak) { | ||
} | ||
else { | ||
var vak = result.right; | ||
if (vak !== ak) { | ||
@@ -602,6 +611,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_3(i); | ||
} | ||
} | ||
@@ -654,3 +660,3 @@ return errors.length > 0 ? failures(errors) : success(a); | ||
}, function (u, c) { | ||
return UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(UnknownRecord.validate(u, c), function (o) { | ||
if (!isUnknownCodec(codomain) && !isAnyCodec(codomain) && !isObject(o)) { | ||
@@ -664,16 +670,23 @@ return failure(u, c); | ||
var changed = false; | ||
var _loop_4 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
var ok = o[k]; | ||
domain.validate(k, appendContext(c, k, domain, k)).fold(function (e) { return pushAll(errors, e); }, function (vk) { | ||
var domainResult = domain.validate(k, appendContext(c, k, domain, k)); | ||
if (isLeft(domainResult)) { | ||
pushAll(errors, domainResult.left); | ||
} | ||
else { | ||
var vk = domainResult.right; | ||
changed = changed || vk !== k; | ||
k = vk; | ||
codomain.validate(ok, appendContext(c, k, codomain, ok)).fold(function (e) { return pushAll(errors, e); }, function (vok) { | ||
var codomainResult = codomain.validate(ok, appendContext(c, k, codomain, ok)); | ||
if (isLeft(codomainResult)) { | ||
pushAll(errors, codomainResult.left); | ||
} | ||
else { | ||
var vok = codomainResult.right; | ||
changed = changed || vok !== ok; | ||
a[k] = vok; | ||
}); | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_4(i); | ||
} | ||
} | ||
} | ||
@@ -737,3 +750,3 @@ return errors.length > 0 ? failures(errors) : success((changed ? a : o)); | ||
}, function (u, c) { | ||
return UnknownRecord.validate(u, c).chain(function (r) { | ||
return chain(UnknownRecord.validate(u, c), function (r) { | ||
var i = find_1(r[tag_1]); | ||
@@ -764,8 +777,9 @@ if (i === undefined) { | ||
var codec = codecs[i]; | ||
var r = codec | ||
.validate(u, appendContext(c, String(i), codec, u)) | ||
.fold(function (e) { return pushAll(errors, e); }, success); | ||
if (r !== undefined) { | ||
return r; | ||
var result = codec.validate(u, appendContext(c, String(i), codec, u)); | ||
if (isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
return success(result.right); | ||
} | ||
} | ||
@@ -830,3 +844,9 @@ return failures(errors); | ||
var codec = codecs[i]; | ||
codec.validate(u, appendContext(c, String(i), codec, u)).fold(function (e) { return pushAll(errors, e); }, function (a) { return us.push(a); }); | ||
var result = codec.validate(u, appendContext(c, String(i), codec, u)); | ||
if (isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
us.push(result.right); | ||
} | ||
} | ||
@@ -854,9 +874,14 @@ return errors.length > 0 ? failures(errors) : success(mergeAll(u, us)); | ||
return new TupleType(name, function (u) { return UnknownArray.is(u) && u.length === len && codecs.every(function (type, i) { return type.is(u[i]); }); }, function (u, c) { | ||
return UnknownArray.validate(u, c).chain(function (us) { | ||
return chain(UnknownArray.validate(u, c), function (us) { | ||
var as = us.length > len ? us.slice(0, len) : us; // strip additional components | ||
var errors = []; | ||
var _loop_5 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var a = us[i]; | ||
var type_3 = codecs[i]; | ||
type_3.validate(a, appendContext(c, String(i), type_3, a)).fold(function (e) { return pushAll(errors, e); }, function (va) { | ||
var result = type_3.validate(a, appendContext(c, String(i), type_3, a)); | ||
if (isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var va = result.right; | ||
if (va !== a) { | ||
@@ -869,6 +894,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_5(i); | ||
} | ||
} | ||
@@ -899,3 +921,3 @@ return errors.length > 0 ? failures(errors) : success(as); | ||
return new ReadonlyType(name, codec.is, function (u, c) { | ||
return codec.validate(u, c).map(function (x) { | ||
return map(codec.validate(u, c), function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -929,3 +951,3 @@ return Object.freeze(x); | ||
return new ReadonlyArrayType(name, arrayType.is, function (u, c) { | ||
return arrayType.validate(u, c).map(function (x) { | ||
return map(arrayType.validate(u, c), function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -1045,5 +1067,3 @@ return Object.freeze(x); | ||
var props = getProps(codec); | ||
return new ExactType(name, codec.is, function (u, c) { | ||
return UnknownRecord.validate(u, c).chain(function () { return codec.validate(u, c).fold(left, function (a) { return success(stripKeys(a, props)); }); }); | ||
}, function (a) { return codec.encode(stripKeys(a, props)); }, codec); | ||
return new ExactType(name, codec.is, function (u, c) { return chain(UnknownRecord.validate(u, c), function () { return map(codec.validate(u, c), function (a) { return stripKeys(a, props); }); }); }, function (a) { return codec.encode(stripKeys(a, props)); }, codec); | ||
}; | ||
@@ -1157,3 +1177,3 @@ export { nullType as null }; | ||
if (name === void 0) { name = "(" + codec.name + " | " + getFunctionName(predicate) + ")"; } | ||
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) { return codec.validate(i, c).chain(function (a) { return (predicate(a) ? success(a) : failure(a, c)); }); }, codec.encode, codec, predicate); | ||
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) { return chain(codec.validate(i, c), function (a) { return (predicate(a) ? success(a) : failure(a, c)); }); }, codec.encode, codec, predicate); | ||
} | ||
@@ -1335,3 +1355,3 @@ /** | ||
var len = codecs.length; | ||
var _loop_6 = function (k) { | ||
var _loop_1 = function (k) { | ||
var all = tags[k].slice(); | ||
@@ -1361,3 +1381,3 @@ var index = [tags[k]]; | ||
var k = keys_1[_i]; | ||
var state_1 = _loop_6(k); | ||
var state_1 = _loop_1(k); | ||
if (typeof state_1 === "object") | ||
@@ -1364,0 +1384,0 @@ return state_1.value; |
import { getFunctionName } from '.'; | ||
import { fold } from 'fp-ts/lib/Either'; | ||
function stringify(v) { | ||
@@ -41,3 +42,3 @@ if (typeof v === 'function') { | ||
export var PathReporter = { | ||
report: function (validation) { return validation.fold(failure, success); } | ||
report: fold(failure, success) | ||
}; |
import { PathReporter } from './PathReporter'; | ||
import { isLeft } from 'fp-ts/lib/Either'; | ||
/** | ||
@@ -8,6 +9,6 @@ * @since 1.0.0 | ||
report: function (validation) { | ||
if (validation.isLeft()) { | ||
throw PathReporter.report(validation).join('\n'); | ||
if (isLeft(validation)) { | ||
throw new Error(PathReporter.report(validation).join('\n')); | ||
} | ||
} | ||
}; |
130
lib/index.js
@@ -28,2 +28,4 @@ "use strict"; | ||
var Either_1 = require("fp-ts/lib/Either"); | ||
var map = Either_1.either.map; | ||
var chain = Either_1.either.chain; | ||
/** | ||
@@ -51,3 +53,3 @@ * @since 1.0.0 | ||
if (name === void 0) { name = "pipe(" + this.name + ", " + ab.name + ")"; } | ||
return new Type(name, ab.is, function (i, c) { return _this.validate(i, c).chain(function (a) { return ab.validate(a, c); }); }, this.encode === exports.identity && ab.encode === exports.identity ? exports.identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
return new Type(name, ab.is, function (i, c) { return chain(_this.validate(i, c), function (a) { return ab.validate(a, c); }); }, this.encode === exports.identity && ab.encode === exports.identity ? exports.identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
}; | ||
@@ -430,9 +432,14 @@ Type.prototype.asDecoder = function () { | ||
return new ArrayType(name, function (u) { return exports.UnknownArray.is(u) && u.every(codec.is); }, function (u, c) { | ||
return exports.UnknownArray.validate(u, c).chain(function (us) { | ||
return chain(exports.UnknownArray.validate(u, c), function (us) { | ||
var len = us.length; | ||
var as = us; | ||
var errors = []; | ||
var _loop_1 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var ui = us[i]; | ||
codec.validate(ui, exports.appendContext(c, String(i), codec, ui)).fold(function (e) { return pushAll(errors, e); }, function (ai) { | ||
var result = codec.validate(ui, exports.appendContext(c, String(i), codec, ui)); | ||
if (Either_1.isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var ai = result.right; | ||
if (ai !== ui) { | ||
@@ -444,6 +451,3 @@ if (as === us) { | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_1(i); | ||
} | ||
} | ||
@@ -505,6 +509,6 @@ return errors.length > 0 ? exports.failures(errors) : exports.success(as); | ||
}, function (u, c) { | ||
return exports.UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(exports.UnknownRecord.validate(u, c), function (o) { | ||
var a = o; | ||
var errors = []; | ||
var _loop_2 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
@@ -519,3 +523,8 @@ if (!hasOwnProperty.call(a, k)) { | ||
var type_1 = types[i]; | ||
type_1.validate(ak, exports.appendContext(c, k, type_1, ak)).fold(function (e) { return pushAll(errors, e); }, function (vak) { | ||
var result = type_1.validate(ak, exports.appendContext(c, k, type_1, ak)); | ||
if (Either_1.isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var vak = result.right; | ||
if (vak !== ak) { | ||
@@ -528,6 +537,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_2(i); | ||
} | ||
} | ||
@@ -589,14 +595,17 @@ return errors.length > 0 ? exports.failures(errors) : exports.success(a); | ||
}, function (u, c) { | ||
return exports.UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(exports.UnknownRecord.validate(u, c), function (o) { | ||
var a = o; | ||
var errors = []; | ||
var _loop_3 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
var ak = a[k]; | ||
var type_2 = props[k]; | ||
type_2.validate(ak, exports.appendContext(c, k, type_2, ak)).fold(function (e) { | ||
var result = type_2.validate(ak, exports.appendContext(c, k, type_2, ak)); | ||
if (Either_1.isLeft(result)) { | ||
if (ak !== undefined) { | ||
pushAll(errors, e); | ||
pushAll(errors, result.left); | ||
} | ||
}, function (vak) { | ||
} | ||
else { | ||
var vak = result.right; | ||
if (vak !== ak) { | ||
@@ -609,6 +618,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_3(i); | ||
} | ||
} | ||
@@ -661,3 +667,3 @@ return errors.length > 0 ? exports.failures(errors) : exports.success(a); | ||
}, function (u, c) { | ||
return exports.UnknownRecord.validate(u, c).chain(function (o) { | ||
return chain(exports.UnknownRecord.validate(u, c), function (o) { | ||
if (!isUnknownCodec(codomain) && !isAnyCodec(codomain) && !isObject(o)) { | ||
@@ -671,16 +677,23 @@ return exports.failure(u, c); | ||
var changed = false; | ||
var _loop_4 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var k = keys[i]; | ||
var ok = o[k]; | ||
domain.validate(k, exports.appendContext(c, k, domain, k)).fold(function (e) { return pushAll(errors, e); }, function (vk) { | ||
var domainResult = domain.validate(k, exports.appendContext(c, k, domain, k)); | ||
if (Either_1.isLeft(domainResult)) { | ||
pushAll(errors, domainResult.left); | ||
} | ||
else { | ||
var vk = domainResult.right; | ||
changed = changed || vk !== k; | ||
k = vk; | ||
codomain.validate(ok, exports.appendContext(c, k, codomain, ok)).fold(function (e) { return pushAll(errors, e); }, function (vok) { | ||
var codomainResult = codomain.validate(ok, exports.appendContext(c, k, codomain, ok)); | ||
if (Either_1.isLeft(codomainResult)) { | ||
pushAll(errors, codomainResult.left); | ||
} | ||
else { | ||
var vok = codomainResult.right; | ||
changed = changed || vok !== ok; | ||
a[k] = vok; | ||
}); | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_4(i); | ||
} | ||
} | ||
} | ||
@@ -744,3 +757,3 @@ return errors.length > 0 ? exports.failures(errors) : exports.success((changed ? a : o)); | ||
}, function (u, c) { | ||
return exports.UnknownRecord.validate(u, c).chain(function (r) { | ||
return chain(exports.UnknownRecord.validate(u, c), function (r) { | ||
var i = find_1(r[tag_1]); | ||
@@ -771,8 +784,9 @@ if (i === undefined) { | ||
var codec = codecs[i]; | ||
var r = codec | ||
.validate(u, exports.appendContext(c, String(i), codec, u)) | ||
.fold(function (e) { return pushAll(errors, e); }, exports.success); | ||
if (r !== undefined) { | ||
return r; | ||
var result = codec.validate(u, exports.appendContext(c, String(i), codec, u)); | ||
if (Either_1.isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
return exports.success(result.right); | ||
} | ||
} | ||
@@ -837,3 +851,9 @@ return exports.failures(errors); | ||
var codec = codecs[i]; | ||
codec.validate(u, exports.appendContext(c, String(i), codec, u)).fold(function (e) { return pushAll(errors, e); }, function (a) { return us.push(a); }); | ||
var result = codec.validate(u, exports.appendContext(c, String(i), codec, u)); | ||
if (Either_1.isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
us.push(result.right); | ||
} | ||
} | ||
@@ -862,9 +882,14 @@ return errors.length > 0 ? exports.failures(errors) : exports.success(mergeAll(u, us)); | ||
return new TupleType(name, function (u) { return exports.UnknownArray.is(u) && u.length === len && codecs.every(function (type, i) { return type.is(u[i]); }); }, function (u, c) { | ||
return exports.UnknownArray.validate(u, c).chain(function (us) { | ||
return chain(exports.UnknownArray.validate(u, c), function (us) { | ||
var as = us.length > len ? us.slice(0, len) : us; // strip additional components | ||
var errors = []; | ||
var _loop_5 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var a = us[i]; | ||
var type_3 = codecs[i]; | ||
type_3.validate(a, exports.appendContext(c, String(i), type_3, a)).fold(function (e) { return pushAll(errors, e); }, function (va) { | ||
var result = type_3.validate(a, exports.appendContext(c, String(i), type_3, a)); | ||
if (Either_1.isLeft(result)) { | ||
pushAll(errors, result.left); | ||
} | ||
else { | ||
var va = result.right; | ||
if (va !== a) { | ||
@@ -877,6 +902,3 @@ /* istanbul ignore next */ | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_5(i); | ||
} | ||
} | ||
@@ -908,3 +930,3 @@ return errors.length > 0 ? exports.failures(errors) : exports.success(as); | ||
return new ReadonlyType(name, codec.is, function (u, c) { | ||
return codec.validate(u, c).map(function (x) { | ||
return map(codec.validate(u, c), function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -938,3 +960,3 @@ return Object.freeze(x); | ||
return new ReadonlyArrayType(name, arrayType.is, function (u, c) { | ||
return arrayType.validate(u, c).map(function (x) { | ||
return map(arrayType.validate(u, c), function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -1054,5 +1076,3 @@ return Object.freeze(x); | ||
var props = getProps(codec); | ||
return new ExactType(name, codec.is, function (u, c) { | ||
return exports.UnknownRecord.validate(u, c).chain(function () { return codec.validate(u, c).fold(Either_1.left, function (a) { return exports.success(stripKeys(a, props)); }); }); | ||
}, function (a) { return codec.encode(stripKeys(a, props)); }, codec); | ||
return new ExactType(name, codec.is, function (u, c) { return chain(exports.UnknownRecord.validate(u, c), function () { return map(codec.validate(u, c), function (a) { return stripKeys(a, props); }); }); }, function (a) { return codec.encode(stripKeys(a, props)); }, codec); | ||
}; | ||
@@ -1153,3 +1173,3 @@ /** | ||
if (name === void 0) { name = "(" + codec.name + " | " + exports.getFunctionName(predicate) + ")"; } | ||
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) { return codec.validate(i, c).chain(function (a) { return (predicate(a) ? exports.success(a) : exports.failure(a, c)); }); }, codec.encode, codec, predicate); | ||
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) { return chain(codec.validate(i, c), function (a) { return (predicate(a) ? exports.success(a) : exports.failure(a, c)); }); }, codec.encode, codec, predicate); | ||
} | ||
@@ -1335,3 +1355,3 @@ exports.refinement = refinement; | ||
var len = codecs.length; | ||
var _loop_6 = function (k) { | ||
var _loop_1 = function (k) { | ||
var all = tags[k].slice(); | ||
@@ -1361,3 +1381,3 @@ var index = [tags[k]]; | ||
var k = keys_1[_i]; | ||
var state_1 = _loop_6(k); | ||
var state_1 = _loop_1(k); | ||
if (typeof state_1 === "object") | ||
@@ -1364,0 +1384,0 @@ return state_1.value; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _1 = require("."); | ||
var Either_1 = require("fp-ts/lib/Either"); | ||
function stringify(v) { | ||
@@ -45,3 +46,3 @@ if (typeof v === 'function') { | ||
exports.PathReporter = { | ||
report: function (validation) { return validation.fold(failure, success); } | ||
report: Either_1.fold(failure, success) | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var PathReporter_1 = require("./PathReporter"); | ||
var Either_1 = require("fp-ts/lib/Either"); | ||
/** | ||
@@ -10,6 +11,6 @@ * @since 1.0.0 | ||
report: function (validation) { | ||
if (validation.isLeft()) { | ||
throw PathReporter_1.PathReporter.report(validation).join('\n'); | ||
if (Either_1.isLeft(validation)) { | ||
throw new Error(PathReporter_1.PathReporter.report(validation).join('\n')); | ||
} | ||
} | ||
}; |
{ | ||
"name": "io-ts", | ||
"version": "1.10.4", | ||
"version": "2.0.0", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -39,4 +39,4 @@ "files": [ | ||
"homepage": "https://github.com/gcanti/io-ts", | ||
"dependencies": { | ||
"fp-ts": "^1.0.0" | ||
"peerDependencies": { | ||
"fp-ts": "^2.0.0" | ||
}, | ||
@@ -51,2 +51,3 @@ "devDependencies": { | ||
"dtslint": "github:gcanti/dtslint", | ||
"fp-ts": "^2.0.0", | ||
"jest": "^24.8.0", | ||
@@ -97,3 +98,4 @@ "mocha": "^5.2.0", | ||
} | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -116,2 +116,3 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts) | ||
| ------------- | --------------------------- | | ||
| 2.x+ | 3.5.2+ | | ||
| 1.6.x+ | 3.2.2+ | | ||
@@ -171,4 +172,10 @@ | 1.5.3 | 3.0.1+ | | ||
```ts | ||
import { pipe } from 'fp-ts/lib/pipeable' | ||
import { fold } from 'fp-ts/lib/Either' | ||
const getPaths = <A>(v: t.Validation<A>): Array<string> => { | ||
return v.fold(errors => errors.map(error => error.context.map(({ key }) => key).join('.')), () => ['no errors']) | ||
return pipe( | ||
v, | ||
fold(errors => errors.map(error => error.context.map(({ key }) => key).join('.')), () => ['no errors']) | ||
) | ||
} | ||
@@ -186,2 +193,4 @@ | ||
```ts | ||
import { either } from 'fp-ts/lib/Either' | ||
const NumberFromString = new t.Type<number, string, unknown>( | ||
@@ -191,3 +200,3 @@ 'NumberFromString', | ||
(u, c) => | ||
t.string.validate(u, c).chain(s => { | ||
either.chain(t.string.validate(u, c), s => { | ||
const n = +s | ||
@@ -207,10 +216,11 @@ return isNaN(n) ? t.failure(u, c, 'cannot parse to a number') : t.success(n) | ||
- [io-ts-types](https://github.com/gcanti/io-ts-types) - A collection of codecs and combinators for use with | ||
io-ts | ||
- [io-ts-reporters](https://github.com/OliverJAsh/io-ts-reporters) - Error reporters for io-ts | ||
- [geojson-iots](https://github.com/pierremarc/geojson-iots) - codecs for GeoJSON as defined in rfc7946 made with | ||
io-ts | ||
- [graphql-to-io-ts](https://github.com/micimize/graphql-to-io-ts) - Generate typescript and cooresponding io-ts types from a graphql | ||
schema | ||
- [io-ts-promise](https://github.com/aeirola/io-ts-promise) - Convenience library for using io-ts with promise-based APIs | ||
- `io-ts@1.x` | ||
- [io-ts-types](https://github.com/gcanti/io-ts-types) - A collection of codecs and combinators for use with | ||
io-ts | ||
- [io-ts-reporters](https://github.com/OliverJAsh/io-ts-reporters) - Error reporters for io-ts | ||
- [geojson-iots](https://github.com/pierremarc/geojson-iots) - codecs for GeoJSON as defined in rfc7946 made with | ||
io-ts | ||
- [graphql-to-io-ts](https://github.com/micimize/graphql-to-io-ts) - Generate typescript and cooresponding io-ts types from a graphql | ||
schema | ||
- [io-ts-promise](https://github.com/aeirola/io-ts-promise) - Convenience library for using io-ts with promise-based APIs | ||
@@ -412,2 +422,4 @@ # TypeScript integration | ||
```ts | ||
import { either } from 'fp-ts/lib/Either' | ||
// represents a Date from an ISO string | ||
@@ -418,3 +430,3 @@ const DateFromString = new t.Type<Date, string, unknown>( | ||
(u, c) => | ||
t.string.validate(u, c).chain(s => { | ||
either.chain(t.string.validate(u, c), s => { | ||
const d = new Date(s) | ||
@@ -502,37 +514,2 @@ return isNaN(d.getTime()) ? t.failure(u, c) : t.success(d) | ||
## Is there a way to turn the checks off in production code? | ||
No, however you can define your own logic for that (if you _really_ trust the input) | ||
```ts | ||
import * as t from 'io-ts' | ||
import { Either, right } from 'fp-ts/lib/Either' | ||
const { NODE_ENV } = process.env | ||
export function unsafeDecode<A, O, I>(value: I, codec: t.Type<A, O, I>): Either<t.Errors, A> { | ||
if (NODE_ENV !== 'production' || codec.encode !== t.identity) { | ||
return codec.decode(value) | ||
} else { | ||
// unsafe cast | ||
return right(value as any) | ||
} | ||
} | ||
// or... | ||
import { failure } from 'io-ts/lib/PathReporter' | ||
export function unsafeGet<A, O, I>(value: I, codec: t.Type<A, O, I>): A { | ||
if (NODE_ENV !== 'production' || type.encode !== t.identity) { | ||
return codec.decode(value).getOrElseL(errors => { | ||
throw new Error(failure(errors).join('\n')) | ||
}) | ||
} else { | ||
// unsafe cast | ||
return value as any | ||
} | ||
} | ||
``` | ||
## Union of string literals | ||
@@ -539,0 +516,0 @@ |
179721
4833
17
542
+ Addedfp-ts@2.16.7(transitive)
- Removedfp-ts@^1.0.0
- Removedfp-ts@1.19.5(transitive)