Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

io-ts

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io-ts - npm Package Compare versions

Comparing version 0.6.2 to 0.7.0

5

CHANGELOG.md

@@ -15,2 +15,7 @@ # Changelog

# 0.7.0
- **Breaking Change**
- upgrade to latest fp-ts (0.5.1) (@gcanti)
# 0.6.2

@@ -17,0 +22,0 @@

215

lib/index.d.ts

@@ -28,92 +28,149 @@ import { Either } from 'fp-ts/lib/Either';

export declare const _A: never;
export declare function getFunctionName(f: any): string;
export declare function failure<T>(value: any, context: Context): Validation<T>;
export declare function success<T>(value: T): Validation<T>;
export declare function validate<T>(value: any, type: Type<T>): Validation<T>;
export declare function is<T>(value: any, type: Type<T>): value is T;
export interface MapType<RT extends Any, A> extends Type<A> {
export declare const getFunctionName: (f: any) => string;
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: <T>(value: any, type: Type<T>) => Either<ValidationError[], T>;
export declare const is: <T>(value: any, type: Type<T>) => value is T;
export declare class MapType<RT extends Any, A> implements Type<A> {
readonly type: RT;
readonly f: (a: TypeOf<RT>) => A;
readonly name: string;
readonly _tag: 'MapType';
readonly _A: A;
readonly type: RT;
readonly f: (a: TypeOf<RT>) => A;
readonly validate: Validate<A>;
constructor(type: RT, f: (a: TypeOf<RT>) => A, name: string);
}
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 interface NullType extends Type<null> {
export declare const map: <RT extends Type<any>, B>(f: (a: RT["_A"]) => B, type: RT) => MapType<RT, B>;
export declare const mapWithName: <RT extends Type<any>, B>(f: (a: RT["_A"]) => B, type: RT, name: string) => MapType<RT, B>;
export declare class NullType implements Type<null> {
readonly _tag: 'NullType';
readonly _A: null;
readonly name: 'null';
readonly validate: Validate<null>;
}
/** An alias of `null` */
export declare const nullType: NullType;
export interface UndefinedType extends Type<undefined> {
export declare class UndefinedType implements Type<undefined> {
readonly _tag: 'UndefinedType';
readonly _A: undefined;
readonly name: 'undefined';
readonly validate: Validate<undefined>;
}
declare const undefinedType: UndefinedType;
export interface AnyType extends Type<any> {
export declare class AnyType implements Type<any> {
readonly _tag: 'AnyType';
readonly _A: any;
readonly name: 'any';
readonly validate: Validate<any>;
}
export declare const any: AnyType;
export interface NeverType extends Type<never> {
export declare class NeverType implements Type<never> {
readonly _tag: 'NeverType';
readonly _A: never;
readonly name: 'never';
readonly validate: Validate<never>;
}
export declare const never: NeverType;
export interface StringType extends Type<string> {
export declare class StringType implements Type<string> {
readonly _tag: 'StringType';
readonly _A: string;
readonly name: 'string';
readonly validate: Validate<string>;
}
export declare const string: StringType;
export interface NumberType extends Type<number> {
export declare class NumberType implements Type<number> {
readonly _tag: 'NumberType';
readonly _A: number;
readonly name: 'number';
readonly validate: Validate<number>;
}
export declare const number: NumberType;
export interface BooleanType extends Type<boolean> {
export declare class BooleanType implements Type<boolean> {
readonly _tag: 'BooleanType';
readonly _A: boolean;
readonly name: 'boolean';
readonly validate: Validate<boolean>;
}
export declare const boolean: BooleanType;
export interface AnyArrayType extends Type<Array<any>> {
export declare class AnyArrayType implements Type<Array<any>> {
readonly _tag: 'AnyArrayType';
readonly _A: Array<any>;
readonly name: 'Array';
readonly validate: Validate<Array<any>>;
}
declare const arrayType: AnyArrayType;
export interface AnyDictionaryType extends Type<{
export declare class AnyDictionaryType implements Type<{
[key: string]: any;
}> {
readonly _tag: 'AnyDictionaryType';
readonly _A: {
[key: string]: any;
};
readonly name: 'Dictionary';
readonly validate: Validate<{
[key: string]: any;
}>;
}
export declare const Dictionary: AnyDictionaryType;
export interface FunctionType extends Type<Function> {
export declare class FunctionType implements Type<Function> {
readonly _tag: 'FunctionType';
readonly _A: Function;
readonly name: 'Function';
readonly validate: Validate<Function>;
}
declare const functionType: FunctionType;
export interface RefinementType<RT extends Any> extends Type<TypeOf<RT>> {
readonly _tag: 'RefinementType';
export declare class RefinementType<RT extends Any> implements Type<TypeOf<RT>> {
readonly type: RT;
readonly predicate: Predicate<TypeOf<RT>>;
readonly name: string;
readonly _tag: 'RefinementType';
readonly _A: TypeOf<RT>;
readonly validate: Validate<TypeOf<RT>>;
constructor(type: RT, predicate: Predicate<TypeOf<RT>>, name?: string);
}
export declare function refinement<RT extends Any>(type: RT, predicate: Predicate<TypeOf<RT>>, name?: string): RefinementType<RT>;
export declare const refinement: <RT extends Type<any>>(type: RT, predicate: Predicate<RT["_A"]>, name?: string | undefined) => RefinementType<RT>;
export declare const Integer: RefinementType<NumberType>;
export declare type GetOption<S, A> = (s: S) => Option<A>;
export interface PrismType<RT extends Any, B> extends Type<B> {
export declare class PrismType<RT extends Any, A> implements Type<A> {
readonly type: RT;
readonly getOption: GetOption<TypeOf<RT>, A>;
readonly name: string;
readonly _tag: 'PrismType';
readonly type: RT;
readonly getOption: GetOption<TypeOf<RT>, B>;
readonly _A: A;
readonly validate: Validate<A>;
constructor(type: RT, getOption: GetOption<TypeOf<RT>, A>, name?: string);
}
export declare function prism<RT extends Any, B>(type: RT, getOption: GetOption<TypeOf<RT>, B>, name?: string): PrismType<RT, B>;
export interface LiteralType<T> extends Type<T> {
export declare const prism: <RT extends Type<any>, A>(type: RT, getOption: GetOption<RT["_A"], A>, name?: string | undefined) => PrismType<RT, A>;
export declare class LiteralType<V extends string | number | boolean> implements Type<V> {
readonly value: V;
readonly name: string;
readonly _tag: 'LiteralType';
readonly value: T;
readonly _A: V;
readonly validate: Validate<V>;
constructor(value: V, name?: string);
}
export declare function literal<T extends string | number | boolean>(value: T): LiteralType<T>;
export interface KeyofType<D extends {
export declare const literal: <V extends string | number | boolean>(value: V, name?: string | undefined) => LiteralType<V>;
export declare class KeyofType<D extends {
[key: string]: any;
}> extends Type<keyof D> {
}> implements Type<keyof D> {
readonly keys: D;
readonly name: string;
readonly _tag: 'KeyofType';
readonly keys: D;
readonly _A: keyof D;
readonly validate: Validate<keyof D>;
constructor(keys: D, name?: string);
}
export declare function keyof<D extends {
export declare const keyof: <D extends {
[key: string]: any;
}>(keys: D, name?: string): KeyofType<D>;
export declare function recursion<T>(name: string, definition: (self: Any) => Any): Type<T>;
export interface ArrayType<RT extends Any> extends Type<Array<TypeOf<RT>>> {
}>(keys: D, name?: string | undefined) => KeyofType<D>;
export declare const recursion: <T>(name: string, definition: (self: Type<any>) => Type<any>) => Type<T>;
export declare class ArrayType<RT extends Any> implements Type<Array<TypeOf<RT>>> {
readonly type: RT;
readonly name: string;
readonly _tag: 'ArrayType';
readonly type: RT;
readonly _A: Array<TypeOf<RT>>;
readonly validate: Validate<Array<TypeOf<RT>>>;
constructor(type: RT, name?: string);
}
export declare function array<RT extends Any>(type: RT, name?: string): ArrayType<RT>;
export declare const array: <RT extends Type<any>>(type: RT, name?: string | undefined) => ArrayType<RT>;
export declare type Props = {

@@ -125,8 +182,12 @@ [key: string]: Any;

};
export interface InterfaceType<P extends Props> extends Type<InterfaceOf<P>> {
export declare class InterfaceType<P extends Props> implements Type<InterfaceOf<P>> {
readonly props: P;
readonly name: string;
readonly _tag: 'InterfaceType';
readonly props: P;
readonly _A: InterfaceOf<P>;
readonly validate: Validate<InterfaceOf<P>>;
constructor(props: P, name?: string);
}
/** An alias of `interface` */
export declare function type<P extends Props>(props: P, name?: string): InterfaceType<P>;
export declare const type: <P extends Props>(props: P, name?: string | undefined) => InterfaceType<P>;
export declare type PartialOf<P extends Props> = {

@@ -138,23 +199,43 @@ [K in keyof P]?: TypeOf<P[K]>;

};
export interface PartialType<P extends Props> extends Type<PartialOf<P>> {
export declare class PartialType<P extends Props> implements Type<PartialOf<P>> {
readonly props: P;
readonly _tag: 'PartialType';
readonly props: PartialPropsOf<P>;
readonly _A: PartialOf<P>;
readonly validate: Validate<PartialOf<P>>;
readonly name: string;
constructor(props: P, name?: string);
}
export declare function partial<P extends Props>(props: P, name?: string): PartialType<P>;
export interface DictionaryType<D extends Type<string>, C extends Any> extends Type<{
export declare const partial: <P extends Props>(props: P, name?: string | undefined) => PartialType<P>;
export declare class DictionaryType<D extends Type<string>, C extends Any> implements Type<{
[key: string]: TypeOf<C>;
}> {
readonly _tag: 'DictionaryType';
readonly domain: D;
readonly codomain: C;
readonly name: string;
readonly _tag: 'DictionaryType';
readonly _A: {
[key: string]: TypeOf<C>;
};
readonly validate: Validate<{
[key: string]: TypeOf<C>;
}>;
constructor(domain: D, codomain: C, name?: string);
}
export declare function dictionary<D extends Type<string>, C extends Any>(domain: D, codomain: C, name?: string): DictionaryType<D, C>;
export interface UnionType<RTS extends [Any], U = TypeOf<RTS['_A']>> extends Type<U> {
export declare const dictionary: <D extends Type<string>, C extends Type<any>>(domain: D, codomain: C, name?: string | undefined) => DictionaryType<D, C>;
export declare class UnionType<RTS extends [Any], U = TypeOf<RTS['_A']>> implements Type<U> {
readonly types: RTS;
readonly name: string;
readonly _tag: 'UnionType';
readonly _A: U;
readonly validate: Validate<U>;
constructor(types: RTS, name?: string);
}
export declare const union: <RTS extends [Type<any>]>(types: RTS, name?: string | undefined) => UnionType<RTS, RTS["_A"]["_A"]>;
export declare class IntersectionType<RTS extends Array<Any>, I> implements Type<I> {
readonly types: RTS;
}
export declare function union<RTS extends [Any]>(types: RTS, name?: string): UnionType<RTS>;
export interface IntersectionType<RTS extends Array<Any>, I> extends Type<I> {
readonly name: string;
readonly _tag: 'IntersectionType';
readonly types: RTS;
readonly _A: I;
readonly validate: Validate<I>;
constructor(types: RTS, name?: string);
}

@@ -166,5 +247,9 @@ export declare function intersection<A extends Any, B extends Any, C extends Any, D extends Any, E extends Any>(types: [A, B, C, D, E], name?: string): IntersectionType<[A, B, C, D, E], TypeOf<A> & TypeOf<B> & TypeOf<C> & TypeOf<D> & TypeOf<E>>;

export declare function intersection<A extends Any>(types: [A], name?: string): IntersectionType<[A], TypeOf<A>>;
export interface TupleType<RTS extends Array<Any>, I> extends Type<I> {
export declare class TupleType<RTS extends Array<Any>, I> implements Type<I> {
readonly types: RTS;
readonly name: string;
readonly _tag: 'TupleType';
readonly types: RTS;
readonly _A: I;
readonly validate: Validate<I>;
constructor(types: RTS, name?: string);
}

@@ -176,12 +261,20 @@ export declare function tuple<A extends Any, B extends Any, C extends Any, D extends Any, E extends Any>(types: [A, B, C, D, E], name?: string): TupleType<[A, B, C, D, E], [TypeOf<A>, TypeOf<B>, TypeOf<C>, TypeOf<D>, TypeOf<E>]>;

export declare function tuple<A extends Any>(types: [A], name?: string): TupleType<[A], [TypeOf<A>]>;
export interface ReadonlyType<RT extends Any> extends Type<Readonly<TypeOf<RT>>> {
export declare class ReadonlyType<RT extends Any> implements Type<Readonly<TypeOf<RT>>> {
readonly type: RT;
readonly name: string;
readonly _tag: 'ReadonlyType';
readonly _A: Readonly<TypeOf<RT>>;
readonly validate: Validate<Readonly<TypeOf<RT>>>;
constructor(type: RT, name?: string);
}
export declare const readonly: <RT extends Type<any>>(type: RT, name?: string | undefined) => ReadonlyType<RT>;
export declare class ReadonlyArrayType<RT extends Any> implements Type<ReadonlyArray<TypeOf<RT>>> {
readonly type: RT;
}
export declare function readonly<RT extends Any>(type: RT, name?: string): ReadonlyType<RT>;
export interface ReadonlyArrayType<RT extends Any> extends Type<ReadonlyArray<TypeOf<RT>>> {
readonly name: string;
readonly _tag: 'ReadonlyArrayType';
readonly type: RT;
readonly _A: ReadonlyArray<TypeOf<RT>>;
readonly validate: Validate<ReadonlyArray<TypeOf<RT>>>;
constructor(type: RT, name?: string);
}
export declare function readonlyArray<RT extends Any>(type: RT, name?: string): ReadonlyArrayType<RT>;
export declare const readonlyArray: <RT extends Type<any>>(type: RT, name?: string | undefined) => ReadonlyArrayType<RT>;
export { nullType as null, undefinedType as undefined, arrayType as Array, functionType as Function, type as interface };

@@ -12,162 +12,209 @@ "use strict";

var Either_1 = require("fp-ts/lib/Either");
exports._A = null;
function getFunctionName(f) {
return f.displayName || f.name || "<function" + f.length + ">";
}
exports.getFunctionName = getFunctionName;
function getContextEntry(key, type) {
return { key: key, type: type };
}
function getValidationError(value, context) {
return { value: value, context: context };
}
function pushAll(xs, ys) {
Array.prototype.push.apply(xs, ys);
}
function failure(value, context) {
exports._A = undefined;
exports.getFunctionName = function (f) { return f.displayName || f.name || "<function" + f.length + ">"; };
var getContextEntry = function (key, type) { return ({ key: key, type: type }); };
var getValidationError = function (value, context) { return ({ value: value, context: context }); };
var pushAll = function (xs, ys) { return Array.prototype.push.apply(xs, ys); };
exports.failure = function (value, context) {
return new Either_1.Left([getValidationError(value, context)]);
}
exports.failure = failure;
function success(value) {
return new Either_1.Right(value);
}
exports.success = success;
function getDefaultContext(type) {
return [{ key: '', type: type }];
}
function validate(value, type) {
return type.validate(value, getDefaultContext(type));
}
exports.validate = validate;
function is(value, type) {
return Either_1.isRight(validate(value, type));
}
exports.is = is;
function map(f, type) {
return mapWithName(f, type, "(" + type.name + " => ?)");
}
exports.map = map;
function mapWithName(f, type, name) {
return {
_A: exports._A,
_tag: 'MapType',
name: name,
validate: function (v, c) { return type.validate(v, c).map(f); },
type: type,
f: f
};
}
exports.mapWithName = mapWithName;
};
exports.success = function (value) { return new Either_1.Right(value); };
var getDefaultContext = function (type) { return [{ key: '', type: type }]; };
exports.validate = function (value, type) { return type.validate(value, getDefaultContext(type)); };
exports.is = function (value, type) { return Either_1.isRight(exports.validate(value, type)); };
var MapType = /** @class */ (function () {
function MapType(type, f, name) {
this.type = type;
this.f = f;
this.name = name;
this._tag = 'MapType';
this.validate = function (v, c) { return type.validate(v, c).map(f); };
}
return MapType;
}());
exports.MapType = MapType;
exports.map = function (f, type) {
return exports.mapWithName(f, type, "(" + type.name + " => ?)");
};
exports.mapWithName = function (f, type, name) {
return new MapType(type, f, name);
};
//
// basic types
//
var NullType = /** @class */ (function () {
function NullType() {
this._tag = 'NullType';
this.name = 'null';
this.validate = function (v, c) { return (v === null ? exports.success(v) : exports.failure(v, c)); };
}
return NullType;
}());
exports.NullType = NullType;
/** An alias of `null` */
exports.nullType = {
_A: exports._A,
_tag: 'NullType',
name: 'null',
validate: function (v, c) { return (v === null ? success(v) : failure(v, c)); }
};
exports.nullType = new NullType();
exports.null = exports.nullType;
var undefinedType = {
_A: exports._A,
_tag: 'UndefinedType',
name: 'undefined',
validate: function (v, c) { return (v === void 0 ? success(v) : failure(v, c)); }
};
var UndefinedType = /** @class */ (function () {
function UndefinedType() {
this._tag = 'UndefinedType';
this.name = 'undefined';
this.validate = function (v, c) { return (v === void 0 ? exports.success(v) : exports.failure(v, c)); };
}
return UndefinedType;
}());
exports.UndefinedType = UndefinedType;
var undefinedType = new UndefinedType();
exports.undefined = undefinedType;
exports.any = {
_A: exports._A,
_tag: 'AnyType',
name: 'any',
validate: function (v, _) { return success(v); }
};
exports.never = {
_A: exports._A,
_tag: 'NeverType',
name: 'never',
validate: function (v, c) { return failure(v, c); }
};
exports.string = {
_A: exports._A,
_tag: 'StringType',
name: 'string',
validate: function (v, c) { return (typeof v === 'string' ? success(v) : failure(v, c)); }
};
exports.number = {
_A: exports._A,
_tag: 'NumberType',
name: 'number',
validate: function (v, c) { return (typeof v === 'number' ? success(v) : failure(v, c)); }
};
exports.boolean = {
_A: exports._A,
_tag: 'BooleanType',
name: 'boolean',
validate: function (v, c) { return (typeof v === 'boolean' ? success(v) : failure(v, c)); }
};
var arrayType = {
_A: exports._A,
_tag: 'AnyArrayType',
name: 'Array',
validate: function (v, c) { return (Array.isArray(v) ? success(v) : failure(v, c)); }
};
var AnyType = /** @class */ (function () {
function AnyType() {
this._tag = 'AnyType';
this.name = 'any';
this.validate = function (v, _) { return exports.success(v); };
}
return AnyType;
}());
exports.AnyType = AnyType;
exports.any = new AnyType();
var NeverType = /** @class */ (function () {
function NeverType() {
this._tag = 'NeverType';
this.name = 'never';
this.validate = function (v, c) { return exports.failure(v, c); };
}
return NeverType;
}());
exports.NeverType = NeverType;
exports.never = new NeverType();
var StringType = /** @class */ (function () {
function StringType() {
this._tag = 'StringType';
this.name = 'string';
this.validate = function (v, c) { return (typeof v === 'string' ? exports.success(v) : exports.failure(v, c)); };
}
return StringType;
}());
exports.StringType = StringType;
exports.string = new StringType();
var NumberType = /** @class */ (function () {
function NumberType() {
this._tag = 'NumberType';
this.name = 'number';
this.validate = function (v, c) { return (typeof v === 'number' ? exports.success(v) : exports.failure(v, c)); };
}
return NumberType;
}());
exports.NumberType = NumberType;
exports.number = new NumberType();
var BooleanType = /** @class */ (function () {
function BooleanType() {
this._tag = 'BooleanType';
this.name = 'boolean';
this.validate = function (v, c) { return (typeof v === 'boolean' ? exports.success(v) : exports.failure(v, c)); };
}
return BooleanType;
}());
exports.BooleanType = BooleanType;
exports.boolean = new BooleanType();
var AnyArrayType = /** @class */ (function () {
function AnyArrayType() {
this._tag = 'AnyArrayType';
this.name = 'Array';
this.validate = function (v, c) { return (Array.isArray(v) ? exports.success(v) : exports.failure(v, c)); };
}
return AnyArrayType;
}());
exports.AnyArrayType = AnyArrayType;
var arrayType = new AnyArrayType();
exports.Array = arrayType;
exports.Dictionary = {
_A: exports._A,
_tag: 'AnyDictionaryType',
name: 'Dictionary',
validate: function (v, c) { return (v !== null && typeof v === 'object' ? success(v) : failure(v, c)); }
var AnyDictionaryType = /** @class */ (function () {
function AnyDictionaryType() {
this._tag = 'AnyDictionaryType';
this.name = 'Dictionary';
this.validate = function (v, c) {
return v !== null && typeof v === 'object' ? exports.success(v) : exports.failure(v, c);
};
}
return AnyDictionaryType;
}());
exports.AnyDictionaryType = AnyDictionaryType;
exports.Dictionary = new AnyDictionaryType();
var FunctionType = /** @class */ (function () {
function FunctionType() {
this._tag = 'FunctionType';
this.name = 'Function';
this.validate = function (v, c) { return (typeof v === 'function' ? exports.success(v) : exports.failure(v, c)); };
}
return FunctionType;
}());
exports.FunctionType = FunctionType;
var functionType = new FunctionType();
exports.Function = functionType;
//
// refinements
//
var RefinementType = /** @class */ (function () {
function RefinementType(type, predicate, name) {
if (name === void 0) { name = "(" + type.name + " | " + exports.getFunctionName(predicate) + ")"; }
this.type = type;
this.predicate = predicate;
this.name = name;
this._tag = 'RefinementType';
this.validate = function (v, c) { return type.validate(v, c).chain(function (t) { return (predicate(t) ? exports.success(t) : exports.failure(v, c)); }); };
}
return RefinementType;
}());
exports.RefinementType = RefinementType;
exports.refinement = function (type, predicate, name) { return new RefinementType(type, predicate, name); };
exports.Integer = exports.refinement(exports.number, function (n) { return n % 1 === 0; }, 'Integer');
var PrismType = /** @class */ (function () {
function PrismType(type, getOption, name) {
if (name === void 0) { name = "Prism<" + type.name + ", ?>"; }
this.type = type;
this.getOption = getOption;
this.name = name;
this._tag = 'PrismType';
this.validate = function (v, c) { return type.validate(v, c).chain(function (a) { return getOption(a).fold(function () { return exports.failure(a, c); }, function (b) { return exports.success(b); }); }); };
}
return PrismType;
}());
exports.PrismType = PrismType;
exports.prism = function (type, getOption, name) { return new PrismType(type, getOption, name); };
//
// literal types
//
var LiteralType = /** @class */ (function () {
function LiteralType(value, name) {
if (name === void 0) { name = JSON.stringify(value); }
this.value = value;
this.name = name;
this._tag = 'LiteralType';
this.validate = function (v, c) { return (v === value ? exports.success(value) : exports.failure(v, c)); };
}
return LiteralType;
}());
exports.LiteralType = LiteralType;
exports.literal = function (value, name) {
return new LiteralType(value, name);
};
var functionType = {
_A: exports._A,
_tag: 'FunctionType',
name: 'Function',
validate: function (v, c) { return (typeof v === 'function' ? success(v) : failure(v, c)); }
//
// keyof types
//
var KeyofType = /** @class */ (function () {
function KeyofType(keys, name) {
if (name === void 0) { name = "(keyof " + JSON.stringify(Object.keys(keys)) + ")"; }
this.keys = keys;
this.name = name;
this._tag = 'KeyofType';
this.validate = function (v, c) { return (keys.hasOwnProperty(v) ? exports.success(v) : exports.failure(v, c)); };
}
return KeyofType;
}());
exports.KeyofType = KeyofType;
exports.keyof = function (keys, name) {
return new KeyofType(keys, name);
};
exports.Function = functionType;
function refinement(type, predicate, name) {
return {
_A: exports._A,
_tag: 'RefinementType',
name: name || "(" + type.name + " | " + getFunctionName(predicate) + ")",
validate: function (v, c) { return type.validate(v, c).chain(function (t) { return (predicate(t) ? success(t) : failure(v, c)); }); },
type: type,
predicate: predicate
};
}
exports.refinement = refinement;
exports.Integer = refinement(exports.number, function (n) { return n % 1 === 0; }, 'Integer');
function prism(type, getOption, name) {
return {
_A: exports._A,
_tag: 'PrismType',
name: name || "Prism<" + type.name + ", ?>",
validate: 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); }); }); },
type: type,
getOption: getOption
};
}
exports.prism = prism;
function literal(value) {
return {
_A: exports._A,
_tag: 'LiteralType',
name: JSON.stringify(value),
validate: function (v, c) { return (v === value ? success(value) : failure(v, c)); },
value: value
};
}
exports.literal = literal;
function keyof(keys, name) {
return {
_A: exports._A,
_tag: 'KeyofType',
name: name || "(keyof " + JSON.stringify(Object.keys(keys)) + ")",
validate: function (v, c) { return (keys.hasOwnProperty(v) ? success(v) : failure(v, c)); },
keys: keys
};
}
exports.keyof = keyof;
//
// recursive types
//
function recursion(name, definition) {
exports.recursion = function (name, definition) {
var Self = { name: name, validate: function (v, c) { return Result.validate(v, c); } };

@@ -177,10 +224,13 @@ var Result = definition(Self);

return Result;
}
exports.recursion = recursion;
function array(type, name) {
return {
_A: exports._A,
_tag: 'ArrayType',
name: name || "Array<" + type.name + ">",
validate: function (v, c) {
};
//
// arrays
//
var ArrayType = /** @class */ (function () {
function ArrayType(type, name) {
if (name === void 0) { name = "Array<" + type.name + ">"; }
this.type = type;
this.name = name;
this._tag = 'ArrayType';
this.validate = function (v, c) {
return arrayType.validate(v, c).chain(function (as) {

@@ -201,16 +251,17 @@ var t = [];

}
return errors.length ? new Either_1.Left(errors) : success(changed ? t : as);
return errors.length ? new Either_1.Left(errors) : exports.success(changed ? t : as);
});
},
type: type
};
}
exports.array = array;
/** An alias of `interface` */
function type(props, name) {
return {
_A: exports._A,
_tag: 'InterfaceType',
name: name || "{ " + Object.keys(props).map(function (k) { return k + ": " + props[k].name; }).join(', ') + " }",
validate: function (v, c) {
};
}
return ArrayType;
}());
exports.ArrayType = ArrayType;
exports.array = function (type, name) { return new ArrayType(type, name); };
var InterfaceType = /** @class */ (function () {
function InterfaceType(props, name) {
if (name === void 0) { name = "{ " + Object.keys(props).map(function (k) { return k + ": " + props[k].name; }).join(', ') + " }"; }
this.props = props;
this.name = name;
this._tag = 'InterfaceType';
this.validate = function (v, c) {
return exports.Dictionary.validate(v, c).chain(function (o) {

@@ -232,31 +283,39 @@ var t = __assign({}, o);

}
return errors.length ? new Either_1.Left(errors) : success((changed ? t : o));
return errors.length ? new Either_1.Left(errors) : exports.success((changed ? t : o));
});
},
props: props
};
}
exports.type = type;
exports.interface = type;
function partial(props, name) {
var partials = {};
for (var k in props) {
partials[k] = union([props[k], undefinedType]);
};
}
var partial = type(partials);
return {
_A: exports._A,
_tag: 'PartialType',
name: name || partial.name,
validate: function (v, c) { return partial.validate(v, c); },
props: partials
};
}
exports.partial = partial;
function dictionary(domain, codomain, name) {
return {
_A: exports._A,
_tag: 'DictionaryType',
name: name || "{ [key: " + domain.name + "]: " + codomain.name + " }",
validate: function (v, c) {
return InterfaceType;
}());
exports.InterfaceType = InterfaceType;
/** An alias of `interface` */
exports.type = function (props, name) { return new InterfaceType(props, name); };
exports.interface = exports.type;
var PartialType = /** @class */ (function () {
function PartialType(props, name) {
this.props = props;
this._tag = 'PartialType';
var partials = {};
for (var k in props) {
partials[k] = exports.union([props[k], undefinedType]);
}
var partial = exports.type(partials);
this.name = name || partial.name;
this.validate = function (v, c) { return partial.validate(v, c); };
}
return PartialType;
}());
exports.PartialType = PartialType;
exports.partial = function (props, name) { return new PartialType(props, name); };
//
// dictionaries
//
var DictionaryType = /** @class */ (function () {
function DictionaryType(domain, codomain, name) {
if (name === void 0) { name = "{ [key: " + domain.name + "]: " + codomain.name + " }"; }
this.domain = domain;
this.codomain = codomain;
this.name = name;
this._tag = 'DictionaryType';
this.validate = function (v, c) {
return exports.Dictionary.validate(v, c).chain(function (o) {

@@ -282,16 +341,20 @@ var t = {};

}
return errors.length ? new Either_1.Left(errors) : success((changed ? t : o));
return errors.length ? new Either_1.Left(errors) : exports.success((changed ? t : o));
});
},
domain: domain,
codomain: codomain
};
}
exports.dictionary = dictionary;
function union(types, name) {
return {
_A: exports._A,
_tag: 'UnionType',
name: name || "(" + types.map(function (type) { return type.name; }).join(' | ') + ")",
validate: function (v, c) {
};
}
return DictionaryType;
}());
exports.DictionaryType = DictionaryType;
exports.dictionary = function (domain, codomain, name) { return new DictionaryType(domain, codomain, name); };
//
// unions
//
var UnionType = /** @class */ (function () {
function UnionType(types, name) {
if (name === void 0) { name = "(" + types.map(function (type) { return type.name; }).join(' | ') + ")"; }
this.types = types;
this.name = name;
this._tag = 'UnionType';
this.validate = function (v, c) {
for (var i = 0; i < types.length; i++) {

@@ -303,14 +366,19 @@ var validation = types[i].validate(v, c);

}
return failure(v, c);
},
types: types
};
}
exports.union = union;
function intersection(types, name) {
return {
_A: exports._A,
_tag: 'IntersectionType',
name: name || "(" + types.map(function (type) { return type.name; }).join(' & ') + ")",
validate: function (v, c) {
return exports.failure(v, c);
};
}
return UnionType;
}());
exports.UnionType = UnionType;
exports.union = function (types, name) { return new UnionType(types, name); };
//
// intersections
//
var IntersectionType = /** @class */ (function () {
function IntersectionType(types, name) {
if (name === void 0) { name = "(" + types.map(function (type) { return type.name; }).join(' & ') + ")"; }
this.types = types;
this.name = name;
this._tag = 'IntersectionType';
this.validate = function (v, c) {
var t = v;

@@ -327,14 +395,22 @@ var changed = false;

}
return errors.length ? new Either_1.Left(errors) : success(changed ? t : v);
},
types: types
};
return errors.length ? new Either_1.Left(errors) : exports.success(changed ? t : v);
};
}
return IntersectionType;
}());
exports.IntersectionType = IntersectionType;
function intersection(types, name) {
return new IntersectionType(types, name);
}
exports.intersection = intersection;
function tuple(types, name) {
return {
_A: exports._A,
_tag: 'TupleType',
name: name || "[" + types.map(function (type) { return type.name; }).join(', ') + "]",
validate: function (v, c) {
//
// tuples
//
var TupleType = /** @class */ (function () {
function TupleType(types, name) {
if (name === void 0) { name = "[" + types.map(function (type) { return type.name; }).join(', ') + "]"; }
this.types = types;
this.name = name;
this._tag = 'TupleType';
this.validate = function (v, c) {
return arrayType.validate(v, c).chain(function (as) {

@@ -356,15 +432,23 @@ var t = [];

}
return errors.length ? new Either_1.Left(errors) : success(changed ? t : as);
return errors.length ? new Either_1.Left(errors) : exports.success((changed ? t : as));
});
},
types: types
};
};
}
return TupleType;
}());
exports.TupleType = TupleType;
function tuple(types, name) {
return new TupleType(types, name);
}
exports.tuple = tuple;
function readonly(type, name) {
return {
_A: exports._A,
_tag: 'ReadonlyType',
name: name || "Readonly<" + type.name + ">",
validate: function (v, c) {
//
// readonly
//
var ReadonlyType = /** @class */ (function () {
function ReadonlyType(type, name) {
if (name === void 0) { name = "Readonly<" + type.name + ">"; }
this.type = type;
this.name = name;
this._tag = 'ReadonlyType';
this.validate = function (v, c) {
return type.validate(v, c).map(function (x) {

@@ -376,14 +460,19 @@ if (process.env.NODE_ENV !== 'production') {

});
},
type: type
};
}
exports.readonly = readonly;
function readonlyArray(type, name) {
var arrayType = array(type);
return {
_A: exports._A,
_tag: 'ReadonlyArrayType',
name: name || "ReadonlyArray<" + type.name + ">",
validate: function (v, c) {
};
}
return ReadonlyType;
}());
exports.ReadonlyType = ReadonlyType;
exports.readonly = function (type, name) { return new ReadonlyType(type, name); };
//
// readonlyArray
//
var ReadonlyArrayType = /** @class */ (function () {
function ReadonlyArrayType(type, name) {
if (name === void 0) { name = "ReadonlyArray<" + type.name + ">"; }
this.type = type;
this.name = name;
this._tag = 'ReadonlyArrayType';
var arrayType = exports.array(type);
this.validate = function (v, c) {
return arrayType.validate(v, c).map(function (x) {

@@ -395,7 +484,10 @@ if (process.env.NODE_ENV !== 'production') {

});
},
type: type
};
}
exports.readonlyArray = readonlyArray;
};
}
return ReadonlyArrayType;
}());
exports.ReadonlyArrayType = ReadonlyArrayType;
exports.readonlyArray = function (type, name) {
return new ReadonlyArrayType(type, name);
};
//# sourceMappingURL=index.js.map
{
"name": "io-ts",
"version": "0.6.2",
"version": "0.7.0",
"description": "TypeScript compatible runtime type system for IO validation",

@@ -28,3 +28,3 @@ "files": ["lib"],

"dependencies": {
"fp-ts": "^0.4.0"
"fp-ts": "^0.5.2"
},

@@ -39,3 +39,3 @@ "devDependencies": {

"tslint-config-standard": "4.0.0",
"typescript": "2.4.2",
"typescript": "^2.5.2",
"typings-checker": "1.1.2"

@@ -42,0 +42,0 @@ },

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc