io-ts
Advanced tools
Comparing version 0.9.1 to 0.9.2-dev.20171215
@@ -10,3 +10,3 @@ import { Either } from 'fp-ts/lib/Either'; | ||
readonly key: string; | ||
readonly type: Any | NeverType; | ||
readonly type: Decoder<any, any>; | ||
} | ||
@@ -26,2 +26,9 @@ export declare type Context = Array<ContextEntry>; | ||
export declare type InputOf<RT extends Any> = RT['_S']; | ||
export interface Decoder<S, A> { | ||
readonly name: string; | ||
readonly validate: Validate<S, A>; | ||
} | ||
export interface Encoder<S, A> { | ||
readonly serialize: Serialize<S, A>; | ||
} | ||
/** | ||
@@ -32,3 +39,3 @@ * Laws: | ||
*/ | ||
export declare class Type<S, A> { | ||
export declare class Type<S, A> implements Decoder<S, A>, Encoder<S, A> { | ||
readonly name: string; | ||
@@ -42,12 +49,15 @@ readonly is: Is<A>; | ||
pipe<B>(ab: Type<A, B>, name?: string): Type<S, B>; | ||
asDecoder(): Decoder<S, A>; | ||
asEncoder(): Encoder<S, A>; | ||
} | ||
export declare const identity: <A>(a: A) => A; | ||
export declare const getFunctionName: (f: any) => string; | ||
export declare const getContextEntry: (key: string, type: Type<any, any> | NeverType) => ContextEntry; | ||
export declare const getContextEntry: (key: string, type: Decoder<any, any>) => ContextEntry; | ||
export declare const getValidationError: (value: any, context: ContextEntry[]) => ValidationError; | ||
export declare const getDefaultContext: (type: Type<any, any>) => ContextEntry[]; | ||
export declare const getDefaultContext: (type: Decoder<any, any>) => ContextEntry[]; | ||
export declare const appendContext: (c: ContextEntry[], key: string, type: Decoder<any, any>) => ContextEntry[]; | ||
export declare const failures: <T>(errors: ValidationError[]) => Either<ValidationError[], T>; | ||
export declare const failure: <T>(value: any, context: ContextEntry[]) => Either<ValidationError[], T>; | ||
export declare const success: <T>(value: T) => Either<ValidationError[], T>; | ||
export declare const validate: <S, A>(value: S, type: Type<S, A>) => Either<ValidationError[], A>; | ||
export declare const validate: <S, A>(value: S, type: Decoder<S, A>) => Either<ValidationError[], A>; | ||
export declare class NullType extends Type<any, null> { | ||
@@ -54,0 +64,0 @@ readonly _tag: 'NullType'; |
104
lib/index.js
@@ -36,6 +36,12 @@ "use strict"; | ||
var _this = this; | ||
return new Type(name || "pipe(" + this.name + ", " + ab.name + ")", function (v) { return _this.is(v) && ab.is(v); }, function (s, c) { return _this.validate(s, c).chain(function (a) { return ab.validate(a, c); }); }, this.serialize === exports.identity && ab.serialize === exports.identity | ||
return new Type(name || "pipe(" + this.name + ", " + ab.name + ")", ab.is, function (s, c) { return _this.validate(s, c).chain(function (a) { return ab.validate(a, c); }); }, this.serialize === exports.identity && ab.serialize === exports.identity | ||
? exports.identity | ||
: function (b) { return _this.serialize(ab.serialize(b)); }); | ||
}; | ||
Type.prototype.asDecoder = function () { | ||
return this; | ||
}; | ||
Type.prototype.asEncoder = function () { | ||
return this; | ||
}; | ||
return Type; | ||
@@ -49,2 +55,11 @@ }()); | ||
exports.getDefaultContext = function (type) { return [{ key: '', type: type }]; }; | ||
exports.appendContext = function (c, key, type) { | ||
var len = c.length; | ||
var r = Array(len + 1); | ||
for (var i = 0; i < len; i++) { | ||
r[i] = c[i]; | ||
} | ||
r[len] = { key: key, type: type }; | ||
return r; | ||
}; | ||
exports.failures = function (errors) { return new Either_1.Left(errors); }; | ||
@@ -147,3 +162,3 @@ exports.failure = function (value, context) { | ||
function AnyArrayType() { | ||
var _this = _super.call(this, 'Array', function (v) { return Array.isArray(v); }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'Array', Array.isArray, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
_this._tag = 'AnyArrayType'; | ||
@@ -286,17 +301,21 @@ return _this; | ||
return arrayType.validate(s, c).chain(function (xs) { | ||
var a = []; | ||
var len = xs.length; | ||
var a = xs; | ||
var errors = []; | ||
var changed = false; | ||
var _loop_1 = function (i) { | ||
var x = xs[i]; | ||
var validation = type.validate(x, c.concat(exports.getContextEntry(String(i), type))); | ||
var validation = type.validate(x, exports.appendContext(c, String(i), type)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (vx) { | ||
changed = changed || vx !== x; | ||
a.push(vx); | ||
if (vx !== x) { | ||
if (a === xs) { | ||
a = xs.slice(); | ||
} | ||
a[i] = vx; | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < xs.length; i++) { | ||
for (var i = 0; i < len; i++) { | ||
_loop_1(i); | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success(changed ? a : xs); | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}); | ||
@@ -344,12 +363,15 @@ }, type.serialize === exports.identity ? exports.identity : function (a) { return a.map(type.serialize); }, type); | ||
return exports.Dictionary.validate(s, c).chain(function (o) { | ||
var a = __assign({}, o); | ||
var a = o; | ||
var errors = []; | ||
var changed = false; | ||
var _loop_2 = function (k) { | ||
var ok = o[k]; | ||
var type_1 = props[k]; | ||
var validation = type_1.validate(ok, c.concat(exports.getContextEntry(k, type_1))); | ||
var validation = type_1.validate(ok, exports.appendContext(c, k, type_1)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (vok) { | ||
changed = changed || vok !== ok; | ||
a[k] = vok; | ||
if (vok !== ok) { | ||
if (a === o) { | ||
a = __assign({}, o); | ||
} | ||
a[k] = vok; | ||
} | ||
}); | ||
@@ -360,3 +382,3 @@ }; | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success((changed ? a : o)); | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}); | ||
@@ -389,6 +411,6 @@ }, useIdentity(props) | ||
for (var k in props) { | ||
partials[k] = exports.union([props[k], undefinedType]); | ||
partials[k] = exports.union([undefinedType, props[k]]); | ||
} | ||
var partial = exports.type(partials); | ||
return new PartialType(name, function (v) { return partial.is(v); }, function (s, c) { return partial.validate(s, c); }, useIdentity(props) | ||
return new PartialType(name, partial.is, partial.validate, useIdentity(props) | ||
? exports.identity | ||
@@ -432,4 +454,4 @@ : function (a) { | ||
var ok = o[k]; | ||
var domainValidation = domain.validate(k, c.concat(exports.getContextEntry(k, domain))); | ||
var codomainValidation = codomain.validate(ok, c.concat(exports.getContextEntry(k, codomain))); | ||
var domainValidation = domain.validate(k, exports.appendContext(c, k, domain)); | ||
var codomainValidation = codomain.validate(ok, exports.appendContext(c, k, codomain)); | ||
domainValidation.fold(function (e) { return pushAll(errors, e); }, function (vk) { | ||
@@ -475,7 +497,8 @@ changed = changed || vk !== k; | ||
if (name === void 0) { name = "(" + types.map(function (type) { return type.name; }).join(' | ') + ")"; } | ||
var len = types.length; | ||
return new UnionType(name, function (v) { return types.some(function (type) { return type.is(v); }); }, function (s, c) { | ||
var errors = []; | ||
for (var i = 0; i < types.length; i++) { | ||
for (var i = 0; i < len; i++) { | ||
var type_2 = types[i]; | ||
var validation = type_2.validate(s, c.concat(exports.getContextEntry(String(i), type_2))); | ||
var validation = type_2.validate(s, exports.appendContext(c, String(i), type_2)); | ||
if (Either_1.isRight(validation)) { | ||
@@ -492,3 +515,4 @@ return validation; | ||
: function (a) { | ||
for (var i = 0; i < types.length; i++) { | ||
var i = 0; | ||
for (; i < len - 1; i++) { | ||
var type_3 = types[i]; | ||
@@ -499,3 +523,3 @@ if (type_3.is(a)) { | ||
} | ||
return a; | ||
return types[i].serialize(a); | ||
}, types); | ||
@@ -519,15 +543,12 @@ }; | ||
if (name === void 0) { name = "(" + types.map(function (type) { return type.name; }).join(' & ') + ")"; } | ||
var len = types.length; | ||
return new IntersectionType(name, function (v) { return types.every(function (type) { return type.is(v); }); }, function (s, c) { | ||
var a = s; | ||
var changed = false; | ||
var errors = []; | ||
for (var i = 0; i < types.length; i++) { | ||
for (var i = 0; i < len; i++) { | ||
var type_4 = types[i]; | ||
var validation = type_4.validate(a, c); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (va) { | ||
changed = changed || va !== a; | ||
a = va; | ||
}); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (va) { return (a = va); }); | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success(changed ? a : s); | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}, types.every(function (type) { return type.serialize === exports.identity; }) | ||
@@ -537,3 +558,3 @@ ? exports.identity | ||
var s = a; | ||
for (var i = 0; i < types.length; i++) { | ||
for (var i = 0; i < len; i++) { | ||
var type_5 = types[i]; | ||
@@ -565,12 +586,15 @@ s = type_5.serialize(s); | ||
return arrayType.validate(s, c).chain(function (as) { | ||
var t = []; | ||
var t = as; | ||
var errors = []; | ||
var changed = false; | ||
var _loop_4 = function (i) { | ||
var a = as[i]; | ||
var type_6 = types[i]; | ||
var validation = type_6.validate(a, c.concat(exports.getContextEntry(String(i), type_6))); | ||
var validation = type_6.validate(a, exports.appendContext(c, String(i), type_6)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (va) { | ||
changed = changed || va !== a; | ||
t.push(va); | ||
if (va !== a) { | ||
if (t === as) { | ||
t = as.slice(); | ||
} | ||
t[i] = va; | ||
} | ||
}); | ||
@@ -582,5 +606,5 @@ }; | ||
if (as.length > len) { | ||
errors.push(exports.getValidationError(as[len], c.concat(exports.getContextEntry(String(len), exports.never)))); | ||
errors.push(exports.getValidationError(as[len], exports.appendContext(c, String(len), exports.never))); | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success((changed ? t : as)); | ||
return errors.length ? exports.failures(errors) : exports.success(t); | ||
}); | ||
@@ -634,3 +658,3 @@ }, types.every(function (type) { return type.serialize === exports.identity; }) | ||
var arrayType = exports.array(type); | ||
return new ReadonlyArrayType(name, function (v) { return arrayType.is(v); }, function (s, c) { | ||
return new ReadonlyArrayType(name, arrayType.is, function (s, c) { | ||
return arrayType.validate(s, c).map(function (x) { | ||
@@ -673,3 +697,3 @@ if (process.env.NODE_ENV !== 'production') { | ||
if (!props.hasOwnProperty(key)) { | ||
errors.push(exports.getValidationError(o[key], c.concat(exports.getContextEntry(key, exports.never)))); | ||
errors.push(exports.getValidationError(o[key], exports.appendContext(c, key, exports.never))); | ||
} | ||
@@ -676,0 +700,0 @@ } |
{ | ||
"name": "io-ts", | ||
"version": "0.9.1", | ||
"version": "0.9.2-dev.20171215", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -23,3 +23,4 @@ "files": ["lib"], | ||
"clean": "rm -rf lib/*", | ||
"build": "npm run clean && tsc && npm run flow-copy-definition-files" | ||
"build": "npm run clean && tsc && npm run flow-copy-definition-files", | ||
"perf": "node perf/index" | ||
}, | ||
@@ -40,4 +41,6 @@ "repository": { | ||
"devDependencies": { | ||
"@types/benchmark": "1.0.31", | ||
"@types/mocha": "2.2.38", | ||
"@types/node": "7.0.4", | ||
"benchmark": "2.1.4", | ||
"mocha": "3.2.0", | ||
@@ -44,0 +47,0 @@ "prettier": "1.8.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
101157
970
11