Comparing version 0.3.0 to 0.3.1
@@ -15,2 +15,8 @@ # Changelog | ||
# 0.3.1 | ||
- **New Feature** | ||
- add mapWithName and Functor instance, fix #37 (@gcanti) | ||
- add prism combinator, fix #41 (@gcanti) | ||
# 0.3.0 | ||
@@ -17,0 +23,0 @@ |
import { Either } from 'fp-ts/lib/Either'; | ||
import { Option } from 'fp-ts/lib/Option'; | ||
import { Predicate } from 'fp-ts/lib/function'; | ||
export interface ContextEntry { | ||
@@ -15,8 +17,8 @@ readonly key: string; | ||
export declare type TypeOf<RT extends Any> = RT['t']; | ||
export declare class Type<T> { | ||
export declare class Type<A> { | ||
readonly name: string; | ||
readonly validate: Validate<T>; | ||
readonly t: T; | ||
constructor(name: string, validate: Validate<T>); | ||
is(x: any): x is T; | ||
readonly validate: Validate<A>; | ||
readonly t: A; | ||
constructor(name: string, validate: Validate<A>); | ||
is(x: any): x is A; | ||
} | ||
@@ -27,2 +29,23 @@ export declare function getFunctionName(f: any): string; | ||
export declare function validate<T>(value: any, type: Type<T>): Validation<T>; | ||
declare module 'fp-ts/lib/HKT' { | ||
interface HKT<A> { | ||
'io-ts/Type': Type<A>; | ||
} | ||
} | ||
export declare const URI = "io-ts/Type"; | ||
export declare type URI = typeof URI; | ||
export declare class MapType<RT extends Any, B> extends Type<B> { | ||
readonly type: RT; | ||
readonly f: (a: TypeOf<RT>) => B; | ||
constructor(name: string, type: RT, f: (a: TypeOf<RT>) => B); | ||
} | ||
export declare function map<RT extends Any, B>(f: (a: TypeOf<RT>) => B, type: RT): MapType<RT, B>; | ||
export declare function mapWithName<RT extends Any, B>(f: (a: TypeOf<RT>) => B, type: RT, name: string): MapType<RT, B>; | ||
export declare type GetOption<S, A> = (s: S) => Option<A>; | ||
export declare class PrismType<RT extends Any, B> extends Type<B> { | ||
readonly type: RT; | ||
readonly getOption: GetOption<TypeOf<RT>, B>; | ||
constructor(name: string, type: RT, getOption: GetOption<TypeOf<RT>, B>); | ||
} | ||
export declare function prism<RT extends Any, B>(type: RT, getOption: GetOption<TypeOf<RT>, B>, name?: string): PrismType<RT, B>; | ||
declare const nullType: Type<null>; | ||
@@ -40,7 +63,6 @@ declare const undefinedType: Type<undefined>; | ||
declare const functionType: Type<Function>; | ||
export declare type Predicate<T> = (value: T) => boolean; | ||
export declare class RefinementType<RT extends Any> extends Type<TypeOf<RT>> { | ||
readonly type: Type<any>; | ||
readonly type: RT; | ||
readonly predicate: Predicate<TypeOf<RT>>; | ||
constructor(name: string, validate: Validate<TypeOf<RT>>, type: Type<any>, predicate: Predicate<TypeOf<RT>>); | ||
constructor(name: string, validate: Validate<TypeOf<RT>>, type: RT, predicate: Predicate<TypeOf<RT>>); | ||
} | ||
@@ -57,4 +79,4 @@ export declare function refinement<RT extends Any>(type: RT, predicate: Predicate<TypeOf<RT>>, name?: string): RefinementType<RT>; | ||
}> extends Type<keyof D> { | ||
readonly map: D; | ||
constructor(name: string, validate: Validate<keyof D>, map: D); | ||
readonly keys: D; | ||
constructor(name: string, validate: Validate<keyof D>, keys: D); | ||
} | ||
@@ -61,0 +83,0 @@ export declare function keyof<D extends { |
@@ -33,5 +33,2 @@ var __extends = (this && this.__extends) || (function () { | ||
export { Type }; | ||
function getTypeName(type) { | ||
return type.name; | ||
} | ||
export function getFunctionName(f) { | ||
@@ -61,2 +58,34 @@ return f.displayName || f.name || "<function" + f.length + ">"; | ||
} | ||
export var URI = 'io-ts/Type'; | ||
var MapType = (function (_super) { | ||
__extends(MapType, _super); | ||
function MapType(name, type, f) { | ||
var _this = _super.call(this, name, function (v, c) { return type.validate(v, c).map(f); }) || this; | ||
_this.type = type; | ||
_this.f = f; | ||
return _this; | ||
} | ||
return MapType; | ||
}(Type)); | ||
export { MapType }; | ||
export function map(f, type) { | ||
return mapWithName(f, type, "(" + type.name + " => ?)"); | ||
} | ||
export function mapWithName(f, type, name) { | ||
return new MapType(name, type, f); | ||
} | ||
var PrismType = (function (_super) { | ||
__extends(PrismType, _super); | ||
function PrismType(name, type, getOption) { | ||
var _this = _super.call(this, name, function (v, c) { return type.validate(v, c).chain(function (a) { return getOption(a).fold(function () { return failure(a, c); }, function (b) { return success(b); }); }); }) || this; | ||
_this.type = type; | ||
_this.getOption = getOption; | ||
return _this; | ||
} | ||
return PrismType; | ||
}(Type)); | ||
export { PrismType }; | ||
export function prism(type, getOption, name) { | ||
return new PrismType(name || "Prism<" + type.name + ", ?>", type, getOption); | ||
} | ||
// | ||
@@ -75,2 +104,5 @@ // default types | ||
var functionType = new Type('Function', function (v, c) { return typeof v === 'function' ? success(v) : failure(v, c); }); | ||
// | ||
// refinements | ||
// | ||
var RefinementType = (function (_super) { | ||
@@ -88,3 +120,3 @@ __extends(RefinementType, _super); | ||
export function refinement(type, predicate, name) { | ||
return new RefinementType(name || "(" + getTypeName(type) + " | " + getFunctionName(predicate) + ")", function (v, c) { return type.validate(v, c).chain(function (t) { return predicate(t) ? success(t) : failure(v, c); }); }, type, predicate); | ||
return new RefinementType(name || "(" + type.name + " | " + getFunctionName(predicate) + ")", function (v, c) { return type.validate(v, c).chain(function (t) { return predicate(t) ? success(t) : failure(v, c); }); }, type, predicate); | ||
} | ||
@@ -113,5 +145,5 @@ export var Integer = refinement(number, function (n) { return n % 1 === 0; }, 'Integer'); | ||
__extends(KeyofType, _super); | ||
function KeyofType(name, validate, map) { | ||
function KeyofType(name, validate, keys) { | ||
var _this = _super.call(this, name, validate) || this; | ||
_this.map = map; | ||
_this.keys = keys; | ||
return _this; | ||
@@ -148,3 +180,3 @@ } | ||
export function array(type, name) { | ||
return new ArrayType(name || "Array<" + getTypeName(type) + ">", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
return new ArrayType(name || "Array<" + type.name + ">", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
var t = []; | ||
@@ -230,3 +262,3 @@ var errors = []; | ||
export function dictionary(domain, codomain, name) { | ||
return new DictionaryType(name || "{ [key: " + getTypeName(domain) + "]: " + getTypeName(codomain) + " }", function (v, c) { return Dictionary.validate(v, c).chain(function (o) { | ||
return new DictionaryType(name || "{ [key: " + domain.name + "]: " + codomain.name + " }", function (v, c) { return Dictionary.validate(v, c).chain(function (o) { | ||
var t = {}; | ||
@@ -282,3 +314,3 @@ var errors = []; | ||
export function union(types, name) { | ||
return new UnionType(name || "(" + types.map(getTypeName).join(' | ') + ")", function (v, c) { | ||
return new UnionType(name || "(" + types.map(function (type) { return type.name; }).join(' | ') + ")", function (v, c) { | ||
for (var i = 0, len = types.length; i < len; i++) { | ||
@@ -307,3 +339,3 @@ var validation = types[i].validate(v, c); | ||
export function intersection(types, name) { | ||
return new IntersectionType(name || "(" + types.map(getTypeName).join(' & ') + ")", function (v, c) { | ||
return new IntersectionType(name || "(" + types.map(function (type) { return type.name; }).join(' & ') + ")", function (v, c) { | ||
var t = v; | ||
@@ -337,3 +369,3 @@ var changed = false; | ||
export function tuple(types, name) { | ||
return new TupleType(name || "[" + types.map(getTypeName).join(', ') + "]", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
return new TupleType(name || "[" + types.map(function (type) { return type.name; }).join(', ') + "]", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
var t = []; | ||
@@ -371,3 +403,3 @@ var errors = []; | ||
export function readonly(type, name) { | ||
return new ReadonlyType(name || "Readonly<" + getTypeName(type) + ">", function (v, c) { return type.validate(v, c).map(function (x) { | ||
return new ReadonlyType(name || "Readonly<" + type.name + ">", function (v, c) { return type.validate(v, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -394,3 +426,3 @@ return Object.freeze(x); | ||
var arrayType = array(type); | ||
return new ReadonlyArrayType(name || "ReadonlyArray<" + getTypeName(type) + ">", function (v, c) { return arrayType.validate(v, c).map(function (x) { | ||
return new ReadonlyArrayType(name || "ReadonlyArray<" + type.name + ">", function (v, c) { return arrayType.validate(v, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -397,0 +429,0 @@ return Object.freeze(x); |
import { Either } from 'fp-ts/lib/Either'; | ||
import { Option } from 'fp-ts/lib/Option'; | ||
import { Predicate } from 'fp-ts/lib/function'; | ||
export interface ContextEntry { | ||
@@ -15,8 +17,8 @@ readonly key: string; | ||
export declare type TypeOf<RT extends Any> = RT['t']; | ||
export declare class Type<T> { | ||
export declare class Type<A> { | ||
readonly name: string; | ||
readonly validate: Validate<T>; | ||
readonly t: T; | ||
constructor(name: string, validate: Validate<T>); | ||
is(x: any): x is T; | ||
readonly validate: Validate<A>; | ||
readonly t: A; | ||
constructor(name: string, validate: Validate<A>); | ||
is(x: any): x is A; | ||
} | ||
@@ -27,2 +29,23 @@ export declare function getFunctionName(f: any): string; | ||
export declare function validate<T>(value: any, type: Type<T>): Validation<T>; | ||
declare module 'fp-ts/lib/HKT' { | ||
interface HKT<A> { | ||
'io-ts/Type': Type<A>; | ||
} | ||
} | ||
export declare const URI = "io-ts/Type"; | ||
export declare type URI = typeof URI; | ||
export declare class MapType<RT extends Any, B> extends Type<B> { | ||
readonly type: RT; | ||
readonly f: (a: TypeOf<RT>) => B; | ||
constructor(name: string, type: RT, f: (a: TypeOf<RT>) => B); | ||
} | ||
export declare function map<RT extends Any, B>(f: (a: TypeOf<RT>) => B, type: RT): MapType<RT, B>; | ||
export declare function mapWithName<RT extends Any, B>(f: (a: TypeOf<RT>) => B, type: RT, name: string): MapType<RT, B>; | ||
export declare type GetOption<S, A> = (s: S) => Option<A>; | ||
export declare class PrismType<RT extends Any, B> extends Type<B> { | ||
readonly type: RT; | ||
readonly getOption: GetOption<TypeOf<RT>, B>; | ||
constructor(name: string, type: RT, getOption: GetOption<TypeOf<RT>, B>); | ||
} | ||
export declare function prism<RT extends Any, B>(type: RT, getOption: GetOption<TypeOf<RT>, B>, name?: string): PrismType<RT, B>; | ||
declare const nullType: Type<null>; | ||
@@ -40,7 +63,6 @@ declare const undefinedType: Type<undefined>; | ||
declare const functionType: Type<Function>; | ||
export declare type Predicate<T> = (value: T) => boolean; | ||
export declare class RefinementType<RT extends Any> extends Type<TypeOf<RT>> { | ||
readonly type: Type<any>; | ||
readonly type: RT; | ||
readonly predicate: Predicate<TypeOf<RT>>; | ||
constructor(name: string, validate: Validate<TypeOf<RT>>, type: Type<any>, predicate: Predicate<TypeOf<RT>>); | ||
constructor(name: string, validate: Validate<TypeOf<RT>>, type: RT, predicate: Predicate<TypeOf<RT>>); | ||
} | ||
@@ -57,4 +79,4 @@ export declare function refinement<RT extends Any>(type: RT, predicate: Predicate<TypeOf<RT>>, name?: string): RefinementType<RT>; | ||
}> extends Type<keyof D> { | ||
readonly map: D; | ||
constructor(name: string, validate: Validate<keyof D>, map: D); | ||
readonly keys: D; | ||
constructor(name: string, validate: Validate<keyof D>, keys: D); | ||
} | ||
@@ -61,0 +83,0 @@ export declare function keyof<D extends { |
@@ -35,5 +35,2 @@ "use strict"; | ||
exports.Type = Type; | ||
function getTypeName(type) { | ||
return type.name; | ||
} | ||
function getFunctionName(f) { | ||
@@ -67,2 +64,37 @@ return f.displayName || f.name || "<function" + f.length + ">"; | ||
exports.validate = validate; | ||
exports.URI = 'io-ts/Type'; | ||
var MapType = (function (_super) { | ||
__extends(MapType, _super); | ||
function MapType(name, type, f) { | ||
var _this = _super.call(this, name, function (v, c) { return type.validate(v, c).map(f); }) || this; | ||
_this.type = type; | ||
_this.f = f; | ||
return _this; | ||
} | ||
return MapType; | ||
}(Type)); | ||
exports.MapType = MapType; | ||
function map(f, type) { | ||
return mapWithName(f, type, "(" + type.name + " => ?)"); | ||
} | ||
exports.map = map; | ||
function mapWithName(f, type, name) { | ||
return new MapType(name, type, f); | ||
} | ||
exports.mapWithName = mapWithName; | ||
var PrismType = (function (_super) { | ||
__extends(PrismType, _super); | ||
function PrismType(name, type, getOption) { | ||
var _this = _super.call(this, name, function (v, c) { return type.validate(v, c).chain(function (a) { return getOption(a).fold(function () { return failure(a, c); }, function (b) { return success(b); }); }); }) || this; | ||
_this.type = type; | ||
_this.getOption = getOption; | ||
return _this; | ||
} | ||
return PrismType; | ||
}(Type)); | ||
exports.PrismType = PrismType; | ||
function prism(type, getOption, name) { | ||
return new PrismType(name || "Prism<" + type.name + ", ?>", type, getOption); | ||
} | ||
exports.prism = prism; | ||
// | ||
@@ -85,2 +117,5 @@ // default types | ||
exports.Function = functionType; | ||
// | ||
// refinements | ||
// | ||
var RefinementType = (function (_super) { | ||
@@ -98,3 +133,3 @@ __extends(RefinementType, _super); | ||
function refinement(type, predicate, name) { | ||
return new RefinementType(name || "(" + getTypeName(type) + " | " + getFunctionName(predicate) + ")", function (v, c) { return type.validate(v, c).chain(function (t) { return predicate(t) ? success(t) : failure(v, c); }); }, type, predicate); | ||
return new RefinementType(name || "(" + type.name + " | " + getFunctionName(predicate) + ")", function (v, c) { return type.validate(v, c).chain(function (t) { return predicate(t) ? success(t) : failure(v, c); }); }, type, predicate); | ||
} | ||
@@ -125,5 +160,5 @@ exports.refinement = refinement; | ||
__extends(KeyofType, _super); | ||
function KeyofType(name, validate, map) { | ||
function KeyofType(name, validate, keys) { | ||
var _this = _super.call(this, name, validate) || this; | ||
_this.map = map; | ||
_this.keys = keys; | ||
return _this; | ||
@@ -162,3 +197,3 @@ } | ||
function array(type, name) { | ||
return new ArrayType(name || "Array<" + getTypeName(type) + ">", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
return new ArrayType(name || "Array<" + type.name + ">", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
var t = []; | ||
@@ -247,3 +282,3 @@ var errors = []; | ||
function dictionary(domain, codomain, name) { | ||
return new DictionaryType(name || "{ [key: " + getTypeName(domain) + "]: " + getTypeName(codomain) + " }", function (v, c) { return exports.Dictionary.validate(v, c).chain(function (o) { | ||
return new DictionaryType(name || "{ [key: " + domain.name + "]: " + codomain.name + " }", function (v, c) { return exports.Dictionary.validate(v, c).chain(function (o) { | ||
var t = {}; | ||
@@ -300,3 +335,3 @@ var errors = []; | ||
function union(types, name) { | ||
return new UnionType(name || "(" + types.map(getTypeName).join(' | ') + ")", function (v, c) { | ||
return new UnionType(name || "(" + types.map(function (type) { return type.name; }).join(' | ') + ")", function (v, c) { | ||
for (var i = 0, len = types.length; i < len; i++) { | ||
@@ -326,3 +361,3 @@ var validation = types[i].validate(v, c); | ||
function intersection(types, name) { | ||
return new IntersectionType(name || "(" + types.map(getTypeName).join(' & ') + ")", function (v, c) { | ||
return new IntersectionType(name || "(" + types.map(function (type) { return type.name; }).join(' & ') + ")", function (v, c) { | ||
var t = v; | ||
@@ -357,3 +392,3 @@ var changed = false; | ||
function tuple(types, name) { | ||
return new TupleType(name || "[" + types.map(getTypeName).join(', ') + "]", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
return new TupleType(name || "[" + types.map(function (type) { return type.name; }).join(', ') + "]", function (v, c) { return arrayType.validate(v, c).chain(function (as) { | ||
var t = []; | ||
@@ -392,3 +427,3 @@ var errors = []; | ||
function readonly(type, name) { | ||
return new ReadonlyType(name || "Readonly<" + getTypeName(type) + ">", function (v, c) { return type.validate(v, c).map(function (x) { | ||
return new ReadonlyType(name || "Readonly<" + type.name + ">", function (v, c) { return type.validate(v, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -416,3 +451,3 @@ return Object.freeze(x); | ||
var arrayType = array(type); | ||
return new ReadonlyArrayType(name || "ReadonlyArray<" + getTypeName(type) + ">", function (v, c) { return arrayType.validate(v, c).map(function (x) { | ||
return new ReadonlyArrayType(name || "ReadonlyArray<" + type.name + ">", function (v, c) { return arrayType.validate(v, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -419,0 +454,0 @@ return Object.freeze(x); |
{ | ||
"name": "io-ts", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -66,3 +66,3 @@ # The idea | ||
```js | ||
import { PathReporter, ThrowReporter } from 'io-ts/reporters/default' | ||
import { PathReporter, ThrowReporter } from 'io-ts/lib/reporters/default' | ||
@@ -78,2 +78,6 @@ const validation = t.validate({"name":"Giulio"}, Person) | ||
## Community error reporters | ||
- [io-ts-reporters](https://github.com/OliverJAsh/io-ts-reporters) | ||
# TypeScript integration | ||
@@ -147,6 +151,8 @@ | ||
| tuple | `[A, B]` | `t.tuple([A, B])` | | ||
| union | `A | B` | `t.union([A, B])` | | ||
| union | `A \| B` | `t.union([A, B])` | | ||
| intersection | `A & B` | `t.intersection([A, B])` | | ||
| keyof | `keyof M` | `t.keyof(M)` | | ||
| recursive types | | `t.recursion(name, definition)` | | ||
| map | ✘ | `t.map(f, type)` | | ||
| prism | ✘ | `t.prism(type, getOption)` | | ||
@@ -267,2 +273,24 @@ # Mixing required and optional props | ||
# Recipes | ||
## 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 { pathReporterFailure } from 'io-ts/lib/reporters/default' | ||
function unsafeValidate<T>(value: any, type: t.Type<T>): T { | ||
if (process.env.NODE_ENV !== 'production') { | ||
return t.validate(value, type) | ||
.fold( | ||
errors => { throw new Error(pathReporterFailure(errors).join('\n')) }, | ||
x => x | ||
) | ||
} | ||
return value as T | ||
} | ||
``` | ||
# Known issues | ||
@@ -269,0 +297,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
192618
1640
322