Comparing version 0.9.8 to 1.0.0
@@ -16,2 +16,8 @@ # Changelog | ||
# 1.0.0 | ||
* **Breaking Change** | ||
* upgrade to `fp-ts@1.0.0` | ||
* see https://github.com/gcanti/io-ts/pull/112 (@gcanti) | ||
# 0.9.8 | ||
@@ -18,0 +24,0 @@ |
@@ -20,15 +20,18 @@ import { Either } from 'fp-ts/lib/Either'; | ||
export declare type Validation<A> = Either<Errors, A>; | ||
export declare type Is<A> = (v: mixed) => v is A; | ||
export declare type Validate<S, A> = (s: S, context: Context) => Validation<A>; | ||
export declare type Serialize<S, A> = (a: A) => S; | ||
export declare type Any = Type<mixed, any>; | ||
export declare type TypeOf<RT extends Type<any, any>> = RT['_A']; | ||
export declare type InputOf<RT extends Type<any, any>> = RT['_S']; | ||
export interface Decoder<S, A> { | ||
export declare type Is<A> = (m: mixed) => m is A; | ||
export declare type Validate<I, A> = (i: I, context: Context) => Validation<A>; | ||
export declare type Decode<I, A> = (i: I) => Validation<A>; | ||
export declare type Encode<A, O> = (a: A) => O; | ||
export declare type Any = Type<any, any, any>; | ||
export declare type Mixed = Type<any, any, mixed>; | ||
export declare type TypeOf<RT extends Any> = RT['_A']; | ||
export declare type InputOf<RT extends Any> = RT['_I']; | ||
export declare type OutputOf<RT extends Any> = RT['_O']; | ||
export interface Decoder<I, A> { | ||
readonly name: string; | ||
readonly validate: Validate<S, A>; | ||
readonly decode: (s: S) => Validation<A>; | ||
readonly validate: Validate<I, A>; | ||
readonly decode: Decode<I, A>; | ||
} | ||
export interface Encoder<S, A> { | ||
readonly serialize: Serialize<S, A>; | ||
export interface Encoder<A, O> { | ||
readonly encode: Encode<A, O>; | ||
} | ||
@@ -42,3 +45,3 @@ /** | ||
*/ | ||
export declare class Type<S, A> implements Decoder<S, A>, Encoder<S, A> { | ||
export declare class Type<A, O = A, I = mixed> implements Decoder<I, A>, Encoder<A, O> { | ||
/** a unique name for this runtime type */ | ||
@@ -48,8 +51,9 @@ readonly name: string; | ||
readonly is: Is<A>; | ||
/** succeeds if a value of type S can be decoded to a value of type A */ | ||
readonly validate: Validate<S, A>; | ||
/** converts a value of type A to a value of type S */ | ||
readonly serialize: Serialize<S, A>; | ||
/** succeeds if a value of type I can be decoded to a value of type A */ | ||
readonly validate: Validate<I, A>; | ||
/** converts a value of type A to a value of type O */ | ||
readonly encode: Encode<A, O>; | ||
readonly '_A': A; | ||
readonly '_S': S; | ||
readonly '_O': O; | ||
readonly '_I': I; | ||
constructor( | ||
@@ -60,11 +64,11 @@ /** a unique name for this runtime type */ | ||
is: Is<A>, | ||
/** succeeds if a value of type S can be decoded to a value of type A */ | ||
validate: Validate<S, A>, | ||
/** converts a value of type A to a value of type S */ | ||
serialize: Serialize<S, A>); | ||
pipe<B>(ab: Type<A, B>, name?: string): Type<S, B>; | ||
asDecoder(): Decoder<S, A>; | ||
asEncoder(): Encoder<S, A>; | ||
/** succeeds if a value of type I can be decoded to a value of type A */ | ||
validate: Validate<I, A>, | ||
/** converts a value of type A to a value of type O */ | ||
encode: Encode<A, O>); | ||
pipe<B>(ab: Type<B, A, A>, name?: string): Type<B, O, I>; | ||
asDecoder(): Decoder<I, A>; | ||
asEncoder(): Encoder<A, O>; | ||
/** a version of `validate` with a default context */ | ||
decode(s: S): Validation<A>; | ||
decode(i: I): Validation<A>; | ||
} | ||
@@ -80,5 +84,3 @@ export declare const identity: <A>(a: A) => A; | ||
export declare const success: <T>(value: T) => Either<ValidationError[], T>; | ||
/** @deprecated use `type.decode(value)` instead */ | ||
export declare const validate: <S, A>(value: S, type: Decoder<S, A>) => Either<ValidationError[], A>; | ||
export declare class NullType extends Type<mixed, null> { | ||
export declare class NullType extends Type<null> { | ||
readonly _tag: 'NullType'; | ||
@@ -89,3 +91,3 @@ constructor(); | ||
export declare const nullType: NullType; | ||
export declare class UndefinedType extends Type<mixed, undefined> { | ||
export declare class UndefinedType extends Type<undefined> { | ||
readonly _tag: 'UndefinedType'; | ||
@@ -95,3 +97,3 @@ constructor(); | ||
declare const undefinedType: UndefinedType; | ||
export declare class AnyType extends Type<any, any> { | ||
export declare class AnyType extends Type<any, any, any> { | ||
readonly _tag: 'AnyType'; | ||
@@ -101,3 +103,3 @@ constructor(); | ||
export declare const any: AnyType; | ||
export declare class NeverType extends Type<mixed, never> { | ||
export declare class NeverType extends Type<never> { | ||
readonly _tag: 'NeverType'; | ||
@@ -107,3 +109,3 @@ constructor(); | ||
export declare const never: NeverType; | ||
export declare class StringType extends Type<mixed, string> { | ||
export declare class StringType extends Type<string> { | ||
readonly _tag: 'StringType'; | ||
@@ -113,3 +115,3 @@ constructor(); | ||
export declare const string: StringType; | ||
export declare class NumberType extends Type<mixed, number> { | ||
export declare class NumberType extends Type<number> { | ||
readonly _tag: 'NumberType'; | ||
@@ -119,3 +121,3 @@ constructor(); | ||
export declare const number: NumberType; | ||
export declare class BooleanType extends Type<mixed, boolean> { | ||
export declare class BooleanType extends Type<boolean> { | ||
readonly _tag: 'BooleanType'; | ||
@@ -125,3 +127,3 @@ constructor(); | ||
export declare const boolean: BooleanType; | ||
export declare class AnyArrayType extends Type<mixed, Array<mixed>> { | ||
export declare class AnyArrayType extends Type<Array<mixed>> { | ||
readonly _tag: 'AnyArrayType'; | ||
@@ -131,3 +133,3 @@ constructor(); | ||
declare const arrayType: AnyArrayType; | ||
export declare class AnyDictionaryType extends Type<mixed, { | ||
export declare class AnyDictionaryType extends Type<{ | ||
[key: string]: mixed; | ||
@@ -139,3 +141,3 @@ }> { | ||
export declare const Dictionary: AnyDictionaryType; | ||
export declare class ObjectType extends Type<mixed, object> { | ||
export declare class ObjectType extends Type<object> { | ||
readonly _tag: 'ObjectType'; | ||
@@ -145,3 +147,3 @@ constructor(); | ||
export declare const object: ObjectType; | ||
export declare class FunctionType extends Type<mixed, Function> { | ||
export declare class FunctionType extends Type<Function> { | ||
readonly _tag: 'FunctionType'; | ||
@@ -151,125 +153,140 @@ constructor(); | ||
export declare const Function: FunctionType; | ||
export declare class RefinementType<RT extends Type<any, any>, S, A> extends Type<S, A> { | ||
export declare class RefinementType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly type: RT; | ||
readonly predicate: Predicate<A>; | ||
readonly _tag: 'RefinementType'; | ||
constructor(name: string, is: RefinementType<RT, S, A>['is'], validate: RefinementType<RT, S, A>['validate'], serialize: RefinementType<RT, S, A>['serialize'], type: RT, predicate: Predicate<A>); | ||
constructor(name: string, is: RefinementType<RT, A, O, I>['is'], validate: RefinementType<RT, A, O, I>['validate'], serialize: RefinementType<RT, A, O, I>['encode'], type: RT, predicate: Predicate<A>); | ||
} | ||
export declare const refinement: <RT extends Type<any, any>>(type: RT, predicate: Predicate<RT["_A"]>, name?: string) => RefinementType<RT, RT["_S"], RT["_A"]>; | ||
export declare const Integer: RefinementType<NumberType, mixed, number>; | ||
export declare class LiteralType<V extends string | number | boolean> extends Type<mixed, V> { | ||
export declare const refinement: <RT extends Type<any, any, any>>(type: RT, predicate: Predicate<RT["_A"]>, name?: string) => RefinementType<RT, RT["_A"], RT["_O"], RT["_I"]>; | ||
export declare const Integer: RefinementType<NumberType, number, number, mixed>; | ||
export declare class LiteralType<V extends string | number | boolean> extends Type<V> { | ||
readonly value: V; | ||
readonly _tag: 'LiteralType'; | ||
constructor(name: string, is: LiteralType<V>['is'], validate: LiteralType<V>['validate'], serialize: LiteralType<V>['serialize'], value: V); | ||
constructor(name: string, is: LiteralType<V>['is'], validate: LiteralType<V>['validate'], serialize: LiteralType<V>['encode'], value: V); | ||
} | ||
export declare const literal: <V extends string | number | boolean>(value: V, name?: string) => LiteralType<V>; | ||
export declare class KeyofType<D extends { | ||
[key: string]: any; | ||
}> extends Type<mixed, keyof D> { | ||
[key: string]: mixed; | ||
}> extends Type<keyof D> { | ||
readonly keys: D; | ||
readonly _tag: 'KeyofType'; | ||
constructor(name: string, is: KeyofType<D>['is'], validate: KeyofType<D>['validate'], serialize: KeyofType<D>['serialize'], keys: D); | ||
constructor(name: string, is: KeyofType<D>['is'], validate: KeyofType<D>['validate'], serialize: KeyofType<D>['encode'], keys: D); | ||
} | ||
export declare const keyof: <D extends { | ||
[key: string]: any; | ||
[key: string]: mixed; | ||
}>(keys: D, name?: string) => KeyofType<D>; | ||
export declare class RecursiveType<RT extends Any, A> extends Type<mixed, A> { | ||
export declare class RecursiveType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
private runDefinition; | ||
readonly _tag: 'RecursiveType'; | ||
constructor(name: string, is: RecursiveType<RT, A>['is'], validate: RecursiveType<RT, A>['validate'], serialize: RecursiveType<RT, A>['serialize'], runDefinition: () => RT); | ||
constructor(name: string, is: RecursiveType<RT, A, O, I>['is'], validate: RecursiveType<RT, A, O, I>['validate'], serialize: RecursiveType<RT, A, O, I>['encode'], runDefinition: () => RT); | ||
readonly type: RT; | ||
} | ||
export declare const recursion: <A, RT extends Type<mixed, A> = Type<mixed, A>>(name: string, definition: (self: RT) => RT) => RecursiveType<RT, A>; | ||
export declare class ArrayType<RT extends Any, A> extends Type<mixed, A> { | ||
export declare const recursion: <A, O = A, I = mixed, RT extends Type<A, O, I> = Type<A, O, I>>(name: string, definition: (self: RT) => RT) => RecursiveType<RT, A, O, I>; | ||
export declare class ArrayType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly type: RT; | ||
readonly _tag: 'ArrayType'; | ||
constructor(name: string, is: ArrayType<RT, A>['is'], validate: ArrayType<RT, A>['validate'], serialize: ArrayType<RT, A>['serialize'], type: RT); | ||
constructor(name: string, is: ArrayType<RT, A, O, I>['is'], validate: ArrayType<RT, A, O, I>['validate'], serialize: ArrayType<RT, A, O, I>['encode'], type: RT); | ||
} | ||
export declare const array: <RT extends Type<mixed, any>>(type: RT, name?: string) => ArrayType<RT, RT["_A"][]>; | ||
export declare type Props = { | ||
export declare const array: <RT extends Type<any, any, mixed>>(type: RT, name?: string) => ArrayType<RT, RT["_A"][], RT["_O"][], mixed>; | ||
export declare class InterfaceType<P extends AnyProps, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly props: P; | ||
readonly _tag: 'InterfaceType'; | ||
constructor(name: string, is: InterfaceType<P, A, O, I>['is'], validate: InterfaceType<P, A, O, I>['validate'], serialize: InterfaceType<P, A, O, I>['encode'], props: P); | ||
} | ||
export interface AnyProps { | ||
[key: string]: Any; | ||
}; | ||
export declare type InterfaceOf<P extends Props> = { | ||
} | ||
export declare type TypeOfProps<P extends AnyProps> = { | ||
[K in keyof P]: TypeOf<P[K]>; | ||
}; | ||
export declare class InterfaceType<P extends Props, A> extends Type<mixed, A> { | ||
readonly props: P; | ||
readonly _tag: 'InterfaceType'; | ||
constructor(name: string, is: InterfaceType<P, A>['is'], validate: InterfaceType<P, A>['validate'], serialize: InterfaceType<P, A>['serialize'], props: P); | ||
export declare type OutputOfProps<P extends AnyProps> = { | ||
[K in keyof P]: OutputOf<P[K]>; | ||
}; | ||
export interface Props { | ||
[key: string]: Mixed; | ||
} | ||
/** @alias `interface` */ | ||
export declare const type: <P extends Props>(props: P, name?: string) => InterfaceType<P, InterfaceOf<P>>; | ||
export declare type PartialOf<P extends Props> = { | ||
[K in keyof P]?: TypeOf<P[K]>; | ||
}; | ||
export declare class PartialType<P extends Props, A> extends Type<mixed, A> { | ||
export declare const type: <P extends Props>(props: P, name?: string) => InterfaceType<P, { [K in keyof P]: P[K]["_A"]; }, { [K in keyof P]: P[K]["_O"]; }, mixed>; | ||
export declare class PartialType<P extends AnyProps, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly props: P; | ||
readonly _tag: 'PartialType'; | ||
constructor(name: string, is: PartialType<P, A>['is'], validate: PartialType<P, A>['validate'], serialize: PartialType<P, A>['serialize'], props: P); | ||
constructor(name: string, is: PartialType<P, A, O, I>['is'], validate: PartialType<P, A, O, I>['validate'], serialize: PartialType<P, A, O, I>['encode'], props: P); | ||
} | ||
export declare const partial: <P extends Props>(props: P, name?: string) => PartialType<P, PartialOf<P>>; | ||
export declare class DictionaryType<D extends Any, C extends Any, A> extends Type<mixed, A> { | ||
export declare type TypeOfPartialProps<P extends AnyProps> = { | ||
[K in keyof P]?: TypeOf<P[K]>; | ||
}; | ||
export declare type InputOfPartialProps<P extends AnyProps> = { | ||
[K in keyof P]?: OutputOf<P[K]>; | ||
}; | ||
export declare const partial: <P extends Props>(props: P, name?: string) => PartialType<P, { [K in keyof P]?: P[K]["_A"] | undefined; }, { [K in keyof P]?: P[K]["_O"] | undefined; }, mixed>; | ||
export declare class DictionaryType<D extends Any, C extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly domain: D; | ||
readonly codomain: C; | ||
readonly _tag: 'DictionaryType'; | ||
constructor(name: string, is: DictionaryType<D, C, A>['is'], validate: DictionaryType<D, C, A>['validate'], serialize: DictionaryType<D, C, A>['serialize'], domain: D, codomain: C); | ||
constructor(name: string, is: DictionaryType<D, C, A, O, I>['is'], validate: DictionaryType<D, C, A, O, I>['validate'], serialize: DictionaryType<D, C, A, O, I>['encode'], domain: D, codomain: C); | ||
} | ||
export declare const dictionary: <D extends Type<mixed, any>, C extends Type<mixed, any>>(domain: D, codomain: C, name?: string) => DictionaryType<D, C, { [K in D["_A"]]: C["_A"]; }>; | ||
export declare class UnionType<RTS extends Array<Any>, A> extends Type<mixed, A> { | ||
export declare type TypeOfDictionary<D extends Any, C extends Any> = { | ||
[K in TypeOf<D>]: TypeOf<C>; | ||
}; | ||
export declare type OutputOfDictionary<D extends Any, C extends Any> = { | ||
[K in OutputOf<D>]: OutputOf<C>; | ||
}; | ||
export declare const dictionary: <D extends Type<any, any, mixed>, C extends Type<any, any, mixed>>(domain: D, codomain: C, name?: string) => DictionaryType<D, C, { [K in D["_A"]]: C["_A"]; }, { [K in D["_O"]]: C["_O"]; }, mixed>; | ||
export declare class UnionType<RTS extends Array<Any>, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly types: RTS; | ||
readonly _tag: 'UnionType'; | ||
constructor(name: string, is: UnionType<RTS, A>['is'], validate: UnionType<RTS, A>['validate'], serialize: UnionType<RTS, A>['serialize'], types: RTS); | ||
constructor(name: string, is: UnionType<RTS, A, O, I>['is'], validate: UnionType<RTS, A, O, I>['validate'], serialize: UnionType<RTS, A, O, I>['encode'], types: RTS); | ||
} | ||
export declare const union: <RTS extends Type<mixed, any>[]>(types: RTS, name?: string) => UnionType<RTS, RTS["_A"]["_A"]>; | ||
export declare class IntersectionType<RTS extends Array<Any>, A> extends Type<mixed, A> { | ||
export declare const union: <RTS extends Type<any, any, mixed>[]>(types: RTS, name?: string) => UnionType<RTS, RTS["_A"]["_A"], RTS["_A"]["_O"], mixed>; | ||
export declare class IntersectionType<RTS extends Array<Any>, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly types: RTS; | ||
readonly _tag: 'IntersectionType'; | ||
constructor(name: string, is: IntersectionType<RTS, A>['is'], validate: IntersectionType<RTS, A>['validate'], serialize: IntersectionType<RTS, A>['serialize'], types: RTS); | ||
constructor(name: string, is: IntersectionType<RTS, A, O, I>['is'], validate: IntersectionType<RTS, A, O, I>['validate'], serialize: IntersectionType<RTS, A, O, I>['encode'], types: RTS); | ||
} | ||
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, B extends Any, C extends Any, D extends Any>(types: [A, B, C, D], name?: string): IntersectionType<[A, B, C, D], TypeOf<A> & TypeOf<B> & TypeOf<C> & TypeOf<D>>; | ||
export declare function intersection<A extends Any, B extends Any, C extends Any>(types: [A, B, C], name?: string): IntersectionType<[A, B, C], TypeOf<A> & TypeOf<B> & TypeOf<C>>; | ||
export declare function intersection<A extends Any, B extends Any>(types: [A, B], name?: string): IntersectionType<[A, B], TypeOf<A> & TypeOf<B>>; | ||
export declare function intersection<A extends Any>(types: [A], name?: string): IntersectionType<[A], TypeOf<A>>; | ||
export declare class TupleType<RTS extends Array<Any>, A> extends Type<mixed, A> { | ||
export declare function intersection<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed, E extends Mixed>(types: [A, B, C, D, E], name?: string): IntersectionType<[A, B, C, D, E], TypeOf<A> & TypeOf<B> & TypeOf<C> & TypeOf<D> & TypeOf<E>, OutputOf<A> & OutputOf<B> & OutputOf<C> & OutputOf<D> & OutputOf<E>, mixed>; | ||
export declare function intersection<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed>(types: [A, B, C, D], name?: string): IntersectionType<[A, B, C, D], TypeOf<A> & TypeOf<B> & TypeOf<C> & TypeOf<D>, OutputOf<A> & OutputOf<B> & OutputOf<C> & OutputOf<D>, mixed>; | ||
export declare function intersection<A extends Mixed, B extends Mixed, C extends Mixed>(types: [A, B, C], name?: string): IntersectionType<[A, B, C], TypeOf<A> & TypeOf<B> & TypeOf<C>, OutputOf<A> & OutputOf<B> & OutputOf<C>, mixed>; | ||
export declare function intersection<A extends Mixed, B extends Mixed>(types: [A, B], name?: string): IntersectionType<[A, B], TypeOf<A> & TypeOf<B>, OutputOf<A> & OutputOf<B>, mixed>; | ||
export declare function intersection<A extends Mixed>(types: [A], name?: string): IntersectionType<[A], TypeOf<A>, OutputOf<A>, mixed>; | ||
export declare class TupleType<RTS extends Array<Any>, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly types: RTS; | ||
readonly _tag: 'TupleType'; | ||
constructor(name: string, is: TupleType<RTS, A>['is'], validate: TupleType<RTS, A>['validate'], serialize: TupleType<RTS, A>['serialize'], types: RTS); | ||
constructor(name: string, is: TupleType<RTS, A, O, I>['is'], validate: TupleType<RTS, A, O, I>['validate'], serialize: TupleType<RTS, A, O, I>['encode'], types: RTS); | ||
} | ||
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, B extends Any, C extends Any, D extends Any>(types: [A, B, C, D], name?: string): TupleType<[A, B, C, D], [TypeOf<A>, TypeOf<B>, TypeOf<C>, TypeOf<D>]>; | ||
export declare function tuple<A extends Any, B extends Any, C extends Any>(types: [A, B, C], name?: string): TupleType<[A, B, C], [TypeOf<A>, TypeOf<B>, TypeOf<C>]>; | ||
export declare function tuple<A extends Any, B extends Any>(types: [A, B], name?: string): TupleType<[A, B], [TypeOf<A>, TypeOf<B>]>; | ||
export declare function tuple<A extends Any>(types: [A], name?: string): TupleType<[A], [TypeOf<A>]>; | ||
export declare class ReadonlyType<RT extends Any, A> extends Type<mixed, A> { | ||
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed, E extends Mixed>(types: [A, B, C, D, E], name?: string): TupleType<[A, B, C, D, E], [TypeOf<A>, TypeOf<B>, TypeOf<C>, TypeOf<D>, TypeOf<E>], [OutputOf<A>, OutputOf<B>, OutputOf<C>, OutputOf<D>, OutputOf<E>], mixed>; | ||
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed>(types: [A, B, C, D], name?: string): TupleType<[A, B, C, D], [TypeOf<A>, TypeOf<B>, TypeOf<C>, TypeOf<D>], [OutputOf<A>, OutputOf<B>, OutputOf<C>, OutputOf<D>], mixed>; | ||
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed>(types: [A, B, C], name?: string): TupleType<[A, B, C], [TypeOf<A>, TypeOf<B>, TypeOf<C>], [OutputOf<A>, OutputOf<B>, OutputOf<C>], mixed>; | ||
export declare function tuple<A extends Mixed, B extends Mixed>(types: [A, B], name?: string): TupleType<[A, B], [TypeOf<A>, TypeOf<B>], [OutputOf<A>, OutputOf<B>], mixed>; | ||
export declare function tuple<A extends Mixed>(types: [A], name?: string): TupleType<[A], [TypeOf<A>], [OutputOf<A>], mixed>; | ||
export declare class ReadonlyType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly type: RT; | ||
readonly _tag: 'ReadonlyType'; | ||
constructor(name: string, is: ReadonlyType<RT, A>['is'], validate: ReadonlyType<RT, A>['validate'], serialize: ReadonlyType<RT, A>['serialize'], type: RT); | ||
constructor(name: string, is: ReadonlyType<RT, A, O, I>['is'], validate: ReadonlyType<RT, A, O, I>['validate'], serialize: ReadonlyType<RT, A, O, I>['encode'], type: RT); | ||
} | ||
export declare const readonly: <RT extends Type<mixed, any>>(type: RT, name?: string) => ReadonlyType<RT, Readonly<RT["_A"]>>; | ||
export declare class ReadonlyArrayType<RT extends Any, A> extends Type<mixed, A> { | ||
export declare const readonly: <RT extends Type<any, any, mixed>>(type: RT, name?: string) => ReadonlyType<RT, Readonly<RT["_A"]>, Readonly<RT["_O"]>, mixed>; | ||
export declare class ReadonlyArrayType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly type: RT; | ||
readonly _tag: 'ReadonlyArrayType'; | ||
constructor(name: string, is: ReadonlyArrayType<RT, A>['is'], validate: ReadonlyArrayType<RT, A>['validate'], serialize: ReadonlyArrayType<RT, A>['serialize'], type: RT); | ||
constructor(name: string, is: ReadonlyArrayType<RT, A, O, I>['is'], validate: ReadonlyArrayType<RT, A, O, I>['validate'], serialize: ReadonlyArrayType<RT, A, O, I>['encode'], type: RT); | ||
} | ||
export declare const readonlyArray: <RT extends Type<mixed, any>>(type: RT, name?: string) => ReadonlyArrayType<RT, ReadonlyArray<RT["_A"]>>; | ||
export declare class StrictType<P extends Props, A> extends Type<mixed, A> { | ||
export declare const readonlyArray: <RT extends Type<any, any, mixed>>(type: RT, name?: string) => ReadonlyArrayType<RT, ReadonlyArray<RT["_A"]>, ReadonlyArray<RT["_O"]>, mixed>; | ||
export declare class StrictType<P extends AnyProps, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
readonly props: P; | ||
readonly _tag: 'StrictType'; | ||
constructor(name: string, is: StrictType<P, A>['is'], validate: StrictType<P, A>['validate'], serialize: StrictType<P, A>['serialize'], props: P); | ||
constructor(name: string, is: StrictType<P, A, O, I>['is'], validate: StrictType<P, A, O, I>['validate'], serialize: StrictType<P, A, O, I>['encode'], props: P); | ||
} | ||
/** Specifies that only the given interface properties are allowed */ | ||
export declare const strict: <P extends Props>(props: P, name?: string) => StrictType<P, InterfaceOf<P>>; | ||
export declare const strict: <P extends Props>(props: P, name?: string) => StrictType<P, { [K in keyof P]: P[K]["_A"]; }, { [K in keyof P]: P[K]["_O"]; }, mixed>; | ||
export declare type TaggedProps<Tag extends string> = { | ||
[K in Tag]: LiteralType<any>; | ||
}; | ||
export interface TaggedRefinement<Tag extends string, A> extends RefinementType<Tagged<Tag>, mixed, A> { | ||
export interface TaggedRefinement<Tag extends string, A, O = A> extends RefinementType<Tagged<Tag>, A, O> { | ||
} | ||
export interface TaggedUnion<Tag extends string, A> extends UnionType<Array<Tagged<Tag>>, A> { | ||
export interface TaggedUnion<Tag extends string, A, O = A> extends UnionType<Array<Tagged<Tag>>, A, O> { | ||
} | ||
export declare type TaggedIntersectionArgument<Tag extends string> = [Tagged<Tag>] | [Tagged<Tag>, Any] | [Any, Tagged<Tag>] | [Tagged<Tag>, Any, Any] | [Any, Tagged<Tag>, Any] | [Any, Any, Tagged<Tag>] | [Tagged<Tag>, Any, Any, Any] | [Any, Tagged<Tag>, Any, Any] | [Any, Any, Tagged<Tag>, Any] | [Any, Any, Any, Tagged<Tag>] | [Tagged<Tag>, Any, Any, Any, Any] | [Any, Tagged<Tag>, Any, Any, Any] | [Any, Any, Tagged<Tag>, Any, Any] | [Any, Any, Any, Tagged<Tag>, Any] | [Any, Any, Any, Any, Tagged<Tag>]; | ||
export interface TaggedIntersection<Tag extends string, A> extends IntersectionType<TaggedIntersectionArgument<Tag>, A> { | ||
export declare type TaggedIntersectionArgument<Tag extends string> = [Tagged<Tag>] | [Tagged<Tag>, Mixed] | [Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Mixed, Tagged<Tag>]; | ||
export interface TaggedIntersection<Tag extends string, A, O = A> extends IntersectionType<TaggedIntersectionArgument<Tag>, A, O> { | ||
} | ||
export declare type Tagged<Tag extends string, A = any> = InterfaceType<TaggedProps<Tag>, A> | StrictType<TaggedProps<Tag>, A> | TaggedRefinement<Tag, A> | TaggedUnion<Tag, A> | TaggedIntersection<Tag, A>; | ||
export declare const taggedUnion: <Tag extends string, RTS extends Tagged<Tag, any>[]>(tag: Tag, types: RTS, name?: string) => UnionType<RTS, RTS["_A"]["_A"]>; | ||
export declare type Tagged<Tag extends string, A = any, O = A> = InterfaceType<TaggedProps<Tag>, A, O> | StrictType<TaggedProps<Tag>, A, O> | TaggedRefinement<Tag, A, O> | TaggedUnion<Tag, A, O> | TaggedIntersection<Tag, A, O>; | ||
export declare const taggedUnion: <Tag extends string, RTS extends Tagged<Tag, any, any>[]>(tag: Tag, types: RTS, name?: string) => UnionType<RTS, RTS["_A"]["_A"], RTS["_A"]["_O"], mixed>; | ||
export { nullType as null, undefinedType as undefined, arrayType as Array, type as interface }; |
134
lib/index.js
@@ -35,16 +35,14 @@ "use strict"; | ||
is, | ||
/** succeeds if a value of type S can be decoded to a value of type A */ | ||
/** succeeds if a value of type I can be decoded to a value of type A */ | ||
validate, | ||
/** converts a value of type A to a value of type S */ | ||
serialize) { | ||
/** converts a value of type A to a value of type O */ | ||
encode) { | ||
this.name = name; | ||
this.is = is; | ||
this.validate = validate; | ||
this.serialize = serialize; | ||
this.encode = encode; | ||
} | ||
Type.prototype.pipe = function (ab, name) { | ||
var _this = this; | ||
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)); }); | ||
return new Type(name || "pipe(" + this.name + ", " + ab.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)); }); | ||
}; | ||
@@ -58,4 +56,4 @@ Type.prototype.asDecoder = function () { | ||
/** a version of `validate` with a default context */ | ||
Type.prototype.decode = function (s) { | ||
return this.validate(s, exports.getDefaultContext(this)); | ||
Type.prototype.decode = function (i) { | ||
return this.validate(i, exports.getDefaultContext(this)); | ||
}; | ||
@@ -86,6 +84,2 @@ return Type; | ||
exports.success = function (value) { return new Either_1.Right(value); }; | ||
/** @deprecated use `type.decode(value)` instead */ | ||
exports.validate = function (value, type) { | ||
return type.validate(value, exports.getDefaultContext(type)); | ||
}; | ||
var pushAll = function (xs, ys) { return Array.prototype.push.apply(xs, ys); }; | ||
@@ -98,3 +92,3 @@ // | ||
function NullType() { | ||
var _this = _super.call(this, 'null', function (v) { return v === null; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'null', function (m) { return m === null; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'NullType'; | ||
@@ -112,3 +106,3 @@ return _this; | ||
function UndefinedType() { | ||
var _this = _super.call(this, 'undefined', function (v) { return v === void 0; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'undefined', function (m) { return m === void 0; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'UndefinedType'; | ||
@@ -136,3 +130,3 @@ return _this; | ||
function NeverType() { | ||
var _this = _super.call(this, 'never', function (_) { return false; }, function (s, c) { return exports.failure(s, c); }, function () { | ||
var _this = _super.call(this, 'never', function (_) { return false; }, function (m, c) { return exports.failure(m, c); }, function () { | ||
throw new Error('cannot serialize never'); | ||
@@ -150,3 +144,3 @@ }) || this; | ||
function StringType() { | ||
var _this = _super.call(this, 'string', function (v) { return typeof v === 'string'; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'string', function (m) { return typeof m === 'string'; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'StringType'; | ||
@@ -162,3 +156,3 @@ return _this; | ||
function NumberType() { | ||
var _this = _super.call(this, 'number', function (v) { return typeof v === 'number'; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'number', function (m) { return typeof m === 'number'; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'NumberType'; | ||
@@ -174,3 +168,3 @@ return _this; | ||
function BooleanType() { | ||
var _this = _super.call(this, 'boolean', function (v) { return typeof v === 'boolean'; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'boolean', function (m) { return typeof m === 'boolean'; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'BooleanType'; | ||
@@ -186,3 +180,3 @@ return _this; | ||
function AnyArrayType() { | ||
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; | ||
var _this = _super.call(this, 'Array', Array.isArray, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'AnyArrayType'; | ||
@@ -199,3 +193,3 @@ return _this; | ||
function AnyDictionaryType() { | ||
var _this = _super.call(this, 'Dictionary', function (v) { return v !== null && typeof v === 'object'; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'Dictionary', function (m) { return m !== null && typeof m === 'object'; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'AnyDictionaryType'; | ||
@@ -222,3 +216,3 @@ return _this; | ||
function FunctionType() { | ||
var _this = _super.call(this, 'Function', function (v) { return typeof v === 'function'; }, function (s, c) { return (_this.is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity) || this; | ||
var _this = _super.call(this, 'Function', function (m) { return typeof m === 'function'; }, function (m, c) { return (_this.is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity) || this; | ||
_this._tag = 'FunctionType'; | ||
@@ -248,3 +242,3 @@ return _this; | ||
if (name === void 0) { name = "(" + type.name + " | " + exports.getFunctionName(predicate) + ")"; } | ||
return new RefinementType(name, function (v) { return type.is(v) && predicate(v); }, function (s, c) { return type.validate(s, c).chain(function (a) { return (predicate(a) ? exports.success(a) : exports.failure(a, c)); }); }, type.serialize, type, predicate); | ||
return new RefinementType(name, function (m) { return type.is(m) && predicate(m); }, function (i, c) { return type.validate(i, c).chain(function (a) { return (predicate(a) ? exports.success(a) : exports.failure(a, c)); }); }, type.encode, type, predicate); | ||
}; | ||
@@ -268,4 +262,4 @@ exports.Integer = exports.refinement(exports.number, function (n) { return n % 1 === 0; }, 'Integer'); | ||
if (name === void 0) { name = JSON.stringify(value); } | ||
var is = function (v) { return v === value; }; | ||
return new LiteralType(name, is, function (s, c) { return (is(s) ? exports.success(value) : exports.failure(s, c)); }, exports.identity, value); | ||
var is = function (m) { return m === value; }; | ||
return new LiteralType(name, is, function (m, c) { return (is(m) ? exports.success(value) : exports.failure(m, c)); }, exports.identity, value); | ||
}; | ||
@@ -288,4 +282,4 @@ // | ||
if (name === void 0) { name = "(keyof " + JSON.stringify(Object.keys(keys)) + ")"; } | ||
var is = function (v) { return exports.string.is(v) && keys.hasOwnProperty(v); }; | ||
return new KeyofType(name, is, function (s, c) { return (is(s) ? exports.success(s) : exports.failure(s, c)); }, exports.identity, keys); | ||
var is = function (m) { return exports.string.is(m) && keys.hasOwnProperty(m); }; | ||
return new KeyofType(name, is, function (m, c) { return (is(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity, keys); | ||
}; | ||
@@ -321,3 +315,3 @@ // | ||
}; | ||
var Self = new RecursiveType(name, function (m) { return runDefinition().is(m); }, function (m, c) { return runDefinition().validate(m, c); }, function (a) { return runDefinition().serialize(a); }, runDefinition); | ||
var Self = new RecursiveType(name, function (m) { return runDefinition().is(m); }, function (m, c) { return runDefinition().validate(m, c); }, function (a) { return runDefinition().encode(a); }, runDefinition); | ||
return Self; | ||
@@ -341,4 +335,4 @@ }; | ||
if (name === void 0) { name = "Array<" + type.name + ">"; } | ||
return new ArrayType(name, function (v) { return arrayType.is(v) && v.every(type.is); }, function (s, c) { | ||
return arrayType.validate(s, c).chain(function (xs) { | ||
return new ArrayType(name, function (m) { return arrayType.is(m) && m.every(type.is); }, function (m, c) { | ||
return arrayType.validate(m, c).chain(function (xs) { | ||
var len = xs.length; | ||
@@ -364,4 +358,7 @@ var a = xs; | ||
}); | ||
}, type.serialize === exports.identity ? exports.identity : function (a) { return a.map(type.serialize); }, type); | ||
}, type.encode === exports.identity ? exports.identity : function (a) { return a.map(type.encode); }, type); | ||
}; | ||
// | ||
// interfaces | ||
// | ||
var InterfaceType = /** @class */ (function (_super) { | ||
@@ -385,3 +382,3 @@ __extends(InterfaceType, _super); | ||
for (var k in props) { | ||
if (props[k].serialize !== exports.identity) { | ||
if (props[k].encode !== exports.identity) { | ||
return false; | ||
@@ -395,8 +392,8 @@ } | ||
if (name === void 0) { name = getNameFromProps(props); } | ||
return new InterfaceType(name, function (v) { | ||
if (!exports.Dictionary.is(v)) { | ||
return new InterfaceType(name, function (m) { | ||
if (!exports.Dictionary.is(m)) { | ||
return false; | ||
} | ||
for (var k in props) { | ||
if (!props[k].is(v[k])) { | ||
if (!props[k].is(m[k])) { | ||
return false; | ||
@@ -406,4 +403,4 @@ } | ||
return true; | ||
}, function (s, c) { | ||
return exports.Dictionary.validate(s, c).chain(function (o) { | ||
}, function (m, c) { | ||
return exports.Dictionary.validate(m, c).chain(function (o) { | ||
var a = o; | ||
@@ -434,3 +431,3 @@ var errors = []; | ||
for (var k in props) { | ||
s[k] = props[k].serialize(a[k]); | ||
s[k] = props[k].encode(a[k]); | ||
} | ||
@@ -441,2 +438,5 @@ return s; | ||
exports.interface = exports.type; | ||
// | ||
// partials | ||
// | ||
var PartialType = /** @class */ (function (_super) { | ||
@@ -467,3 +467,3 @@ __extends(PartialType, _super); | ||
if (ak !== undefined) { | ||
s[k] = props[k].serialize(ak); | ||
s[k] = props[k].encode(ak); | ||
} | ||
@@ -491,6 +491,6 @@ } | ||
if (name === void 0) { name = "{ [K in " + domain.name + "]: " + codomain.name + " }"; } | ||
return new DictionaryType(name, function (v) { | ||
return exports.Dictionary.is(v) && Object.keys(v).every(function (k) { return domain.is(k) && codomain.is(v[k]); }); | ||
}, function (s, c) { | ||
return exports.Dictionary.validate(s, c).chain(function (o) { | ||
return new DictionaryType(name, function (m) { | ||
return exports.Dictionary.is(m) && Object.keys(m).every(function (k) { return domain.is(k) && codomain.is(m[k]); }); | ||
}, function (m, c) { | ||
return exports.Dictionary.validate(m, c).chain(function (o) { | ||
var a = {}; | ||
@@ -517,3 +517,3 @@ var errors = []; | ||
}); | ||
}, domain.serialize === exports.identity && codomain.serialize === exports.identity | ||
}, domain.encode === exports.identity && codomain.encode === exports.identity | ||
? exports.identity | ||
@@ -523,3 +523,3 @@ : function (a) { | ||
for (var k in a) { | ||
s[String(domain.serialize(k))] = codomain.serialize(a[k]); | ||
s[String(domain.encode(k))] = codomain.encode(a[k]); | ||
} | ||
@@ -546,7 +546,7 @@ return s; | ||
var len = types.length; | ||
return new UnionType(name, function (v) { return types.some(function (type) { return type.is(v); }); }, function (s, c) { | ||
return new UnionType(name, function (m) { return types.some(function (type) { return type.is(m); }); }, function (m, c) { | ||
var errors = []; | ||
for (var i = 0; i < len; i++) { | ||
var type_2 = types[i]; | ||
var validation = type_2.validate(s, exports.appendContext(c, String(i), type_2)); | ||
var validation = type_2.validate(m, exports.appendContext(c, String(i), type_2)); | ||
if (validation.isRight()) { | ||
@@ -560,3 +560,3 @@ return validation; | ||
return exports.failures(errors); | ||
}, types.every(function (type) { return type.serialize === exports.identity; }) | ||
}, types.every(function (type) { return type.encode === exports.identity; }) | ||
? exports.identity | ||
@@ -568,6 +568,6 @@ : function (a) { | ||
if (type_3.is(a)) { | ||
return type_3.serialize(a); | ||
return type_3.encode(a); | ||
} | ||
} | ||
return types[i].serialize(a); | ||
return types[i].encode(a); | ||
}, types); | ||
@@ -592,4 +592,4 @@ }; | ||
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; | ||
return new IntersectionType(name, function (m) { return types.every(function (type) { return type.is(m); }); }, function (m, c) { | ||
var a = m; | ||
var errors = []; | ||
@@ -602,3 +602,3 @@ for (var i = 0; i < len; i++) { | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}, types.every(function (type) { return type.serialize === exports.identity; }) | ||
}, types.every(function (type) { return type.encode === exports.identity; }) | ||
? exports.identity | ||
@@ -609,3 +609,3 @@ : function (a) { | ||
var type_5 = types[i]; | ||
s = type_5.serialize(s); | ||
s = type_5.encode(s); | ||
} | ||
@@ -633,4 +633,4 @@ return s; | ||
var len = types.length; | ||
return new TupleType(name, function (v) { return arrayType.is(v) && v.length === len && types.every(function (type, i) { return type.is(v[i]); }); }, function (s, c) { | ||
return arrayType.validate(s, c).chain(function (as) { | ||
return new TupleType(name, function (m) { return arrayType.is(m) && m.length === len && types.every(function (type, i) { return type.is(m[i]); }); }, function (m, c) { | ||
return arrayType.validate(m, c).chain(function (as) { | ||
var t = as; | ||
@@ -659,3 +659,3 @@ var errors = []; | ||
}); | ||
}, types.every(function (type) { return type.serialize === exports.identity; }) ? exports.identity : function (a) { return types.map(function (type, i) { return type.serialize(a[i]); }); }, types); | ||
}, types.every(function (type) { return type.encode === exports.identity; }) ? exports.identity : function (a) { return types.map(function (type, i) { return type.encode(a[i]); }); }, types); | ||
} | ||
@@ -679,4 +679,4 @@ exports.tuple = tuple; | ||
if (name === void 0) { name = "Readonly<" + type.name + ">"; } | ||
return new ReadonlyType(name, type.is, function (s, c) { | ||
return type.validate(s, c).map(function (x) { | ||
return new ReadonlyType(name, type.is, function (m, c) { | ||
return type.validate(m, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -687,3 +687,3 @@ return Object.freeze(x); | ||
}); | ||
}, type.serialize === exports.identity ? exports.identity : type.serialize, type); | ||
}, type.encode === exports.identity ? exports.identity : type.encode, type); | ||
}; | ||
@@ -707,4 +707,4 @@ // | ||
var arrayType = exports.array(type); | ||
return new ReadonlyArrayType(name, arrayType.is, function (s, c) { | ||
return arrayType.validate(s, c).map(function (x) { | ||
return new ReadonlyArrayType(name, arrayType.is, function (m, c) { | ||
return arrayType.validate(m, c).map(function (x) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -717,3 +717,3 @@ return Object.freeze(x); | ||
}); | ||
}, arrayType.serialize, type); | ||
}, arrayType.encode, type); | ||
}; | ||
@@ -738,4 +738,4 @@ // | ||
var loose = exports.type(props); | ||
return new StrictType(name, function (v) { return loose.is(v) && Object.getOwnPropertyNames(v).every(function (k) { return props.hasOwnProperty(k); }); }, function (s, c) { | ||
return loose.validate(s, c).chain(function (o) { | ||
return new StrictType(name, function (m) { return loose.is(m) && Object.getOwnPropertyNames(m).every(function (k) { return props.hasOwnProperty(k); }); }, function (m, c) { | ||
return loose.validate(m, c).chain(function (o) { | ||
var keys = Object.getOwnPropertyNames(o); | ||
@@ -752,3 +752,3 @@ var len = keys.length; | ||
}); | ||
}, loose.serialize, props); | ||
}, loose.encode, props); | ||
}; | ||
@@ -829,4 +829,4 @@ var isTagged = function (tag) { | ||
}); | ||
}, types.every(function (type) { return type.serialize === exports.identity; }) ? exports.identity : function (a) { return types[tagValue2Index[a[tag]]].serialize(a); }, types); | ||
}, types.every(function (type) { return type.encode === exports.identity; }) ? exports.identity : function (a) { return types[tagValue2Index[a[tag]]].encode(a); }, types); | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "io-ts", | ||
"version": "0.9.8", | ||
"version": "1.0.0", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -12,3 +12,3 @@ "files": ["lib"], | ||
"typings-checker --allow-expect-error --project typings-checker/tsconfig.json typings-checker/index.ts", | ||
"mocha": "mocha -r ts-node/register test/*.ts", | ||
"mocha": "TS_NODE_CACHE=false mocha -r ts-node/register test/*.ts", | ||
"prettier": | ||
@@ -18,9 +18,5 @@ "prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test}/**/*.ts\"", | ||
"prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test,examples,exercises}/**/*.ts\"", | ||
"flow-copy-definition-files": "cp src/*.js.flow lib", | ||
"flow-test": "flow status", | ||
"flow-fix-prettier": | ||
"prettier --no-semi --single-quote --print-width 120 --parser flow --write \"{src,test,examples,exercises}/**/*.js.flow\"", | ||
"test": "npm run prettier && npm run lint && npm run typings-checker && npm run mocha", | ||
"clean": "rm -rf lib/*", | ||
"build": "npm run clean && tsc && npm run flow-copy-definition-files", | ||
"build": "npm run clean && tsc", | ||
"perf": "node perf/index" | ||
@@ -39,3 +35,3 @@ }, | ||
"dependencies": { | ||
"fp-ts": "^0.6.4" | ||
"fp-ts": "^1.0.0" | ||
}, | ||
@@ -42,0 +38,0 @@ "devDependencies": { |
150
README.md
@@ -7,8 +7,8 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts) | ||
A value of type `Type<S, A>` (called "runtime type") is the runtime representation of the static type `A`. | ||
A value of type `Type<A, O, I>` (called "runtime type") is the runtime representation of the static type `A`. | ||
Also a runtime type can | ||
* decode inputs (through `decode`) | ||
* encode outputs (through `serialize`) | ||
* decode inputs of type `I` (through `decode`) | ||
* encode outputs of type `O` (through `encode`) | ||
* be used as a custom type guard (through `is`) | ||
@@ -19,5 +19,6 @@ | ||
class Type<S, A> { | ||
class Type<A, O = A, I = mixed> { | ||
readonly _A: A | ||
readonly _S: S | ||
readonly _O: O | ||
readonly _I: I | ||
constructor( | ||
@@ -29,8 +30,8 @@ /** a unique name for this runtime type */ | ||
/** succeeds if a value of type S can be decoded to a value of type A */ | ||
readonly validate: (input: S, context: Context) => Either<Errors, A>, | ||
readonly validate: (input: I, context: Context) => Either<Errors, A>, | ||
/** converts a value of type A to a value of type S */ | ||
readonly serialize: (output: A) => S | ||
readonly encode: (a: A) => O | ||
) {} | ||
/** a version of `validate` with a default context */ | ||
decode(s: S): Either<Errors, A> | ||
decode(i: I): Either<Errors, A> | ||
} | ||
@@ -49,8 +50,9 @@ ``` | ||
export class StringType extends Type<mixed, string> { | ||
export class StringType extends Type<string> { // equivalent to Type<string, string, mixed> as per type parameter defaults | ||
readonly _tag: 'StringType' = 'StringType' | ||
constructor() { | ||
super( | ||
'string', | ||
(v): v is string => typeof v === 'string', | ||
(s, c) => (this.is(s) ? success(s) : failure(s, c)), | ||
(m): m is string => typeof m === 'string', | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
a => a | ||
@@ -65,3 +67,3 @@ ) | ||
```js | ||
const Person = t.interface({ | ||
const Person = t.type({ | ||
name: t.string, | ||
@@ -141,4 +143,40 @@ age: t.number | ||
## Recursive types | ||
# Implemented types / combinators | ||
```js | ||
import * as t from 'io-ts' | ||
``` | ||
| Type | TypeScript | Flow | Runtime type / combinator | | ||
| --------------------- | --------------------------------------- | --------------------------------------- | ------------------------------------------------------------ | | ||
| null | `null` | `null` | `t.null` or `t.nullType` | | ||
| undefined | `undefined` | `void` | `t.undefined` | | ||
| string | `string` | `string` | `t.string` | | ||
| number | `number` | `number` | `t.number` | | ||
| boolean | `boolean` | `boolean` | `t.boolean` | | ||
| any | `any` | `any` | `t.any` | | ||
| never | `never` | `empty` | `t.never` | | ||
| object | `object` | ✘ | `t.object` | | ||
| integer | ✘ | ✘ | `t.Integer` | | ||
| array of any | `Array<mixed>` | `Array<mixed>` | `t.Array` | | ||
| array of type | `Array<A>` | `Array<A>` | `t.array(A)` | | ||
| dictionary of any | `{ [key: string]: mixed }` | `{ [key: string]: mixed }` | `t.Dictionary` | | ||
| dictionary of type | `{ [K in A]: B }` | `{ [key: A]: B }` | `t.dictionary(A, B)` | | ||
| function | `Function` | `Function` | `t.Function` | | ||
| literal | `'s'` | `'s'` | `t.literal('s')` | | ||
| partial | `Partial<{ name: string }>` | `$Shape<{ name: string }>` | `t.partial({ name: t.string })` | | ||
| readonly | `Readonly<T>` | `ReadOnly<T>` | `t.readonly(T)` | | ||
| readonly array | `ReadonlyArray<number>` | `ReadOnlyArray<number>` | `t.readonlyArray(t.number)` | | ||
| interface | `interface A { name: string }` | `interface A { name: string }` | `t.type({ name: t.string })` or `t.type({ name: t.string })` | | ||
| interface inheritance | `interface B extends A {}` | `interface B extends A {}` | `t.intersection([ A, t.type({}) ])` | | ||
| tuple | `[ A, B ]` | `[ A, B ]` | `t.tuple([ A, B ])` | | ||
| union | `A \| B` | `A \| B` | `t.union([ A, B ])` or `t.taggedUnion(tag, [ A, B ])` | | ||
| intersection | `A & B` | `A & B` | `t.intersection([ A, B ])` | | ||
| keyof | `keyof M` | `$Keys<M>` | `t.keyof(M)` | | ||
| recursive types | see [Recursive types](#recursive-types) | see [Recursive types](#recursive-types) | `t.recursion(name, definition)` | | ||
| refinement | ✘ | ✘ | `t.refinement(A, predicate)` | | ||
| strict/exact types | ✘ | `$Exact<{{ name: t.string }}>` | `t.strict({ name: t.string })` | | ||
# Recursive types | ||
Recursive types can't be inferred by TypeScript so you must provide the static type as a hint | ||
@@ -158,3 +196,3 @@ | ||
self => | ||
t.interface({ | ||
t.type({ | ||
name: t.string, | ||
@@ -165,38 +203,2 @@ categories: t.array(self) | ||
# Implemented types / combinators | ||
```js | ||
import * as t from 'io-ts' | ||
``` | ||
| Type | TypeScript | Flow | Runtime type / combinator | | ||
| --------------------- | --------------------------------------- | --------------------------------------- | ----------------------------------------------------------------- | | ||
| null | `null` | `null` | `t.null` or `t.nullType` | | ||
| undefined | `undefined` | `void` | `t.undefined` | | ||
| string | `string` | `string` | `t.string` | | ||
| number | `number` | `number` | `t.number` | | ||
| boolean | `boolean` | `boolean` | `t.boolean` | | ||
| any | `any` | `any` | `t.any` | | ||
| never | `never` | `empty` | `t.never` | | ||
| object | `object` | ✘ | `t.object` | | ||
| integer | ✘ | ✘ | `t.Integer` | | ||
| array of any | `Array<mixed>` | `Array<mixed>` | `t.Array` | | ||
| array of type | `Array<A>` | `Array<A>` | `t.array(A)` | | ||
| dictionary of any | `{ [key: string]: mixed }` | `{ [key: string]: mixed }` | `t.Dictionary` | | ||
| dictionary of type | `{ [K in A]: B }` | `{ [key: A]: B }` | `t.dictionary(A, B)` | | ||
| function | `Function` | `Function` | `t.Function` | | ||
| literal | `'s'` | `'s'` | `t.literal('s')` | | ||
| partial | `Partial<{ name: string }>` | `$Shape<{ name: string }>` | `t.partial({ name: t.string })` | | ||
| readonly | `Readonly<T>` | `ReadOnly<T>` | `t.readonly(T)` | | ||
| readonly array | `ReadonlyArray<number>` | `ReadOnlyArray<number>` | `t.readonlyArray(t.number)` | | ||
| interface | `interface A { name: string }` | `interface A { name: string }` | `t.interface({ name: t.string })` or `t.type({ name: t.string })` | | ||
| interface inheritance | `interface B extends A {}` | `interface B extends A {}` | `t.intersection([ A, t.interface({}) ])` | | ||
| tuple | `[ A, B ]` | `[ A, B ]` | `t.tuple([ A, B ])` | | ||
| union | `A \| B` | `A \| B` | `t.union([ A, B ])` or `t.taggedUnion(tag, [ A, B ])` | | ||
| intersection | `A & B` | `A & B` | `t.intersection([ A, B ])` | | ||
| keyof | `keyof M` | `$Keys<M>` | `t.keyof(M)` | | ||
| recursive types | see [Recursive types](#recursive-types) | see [Recursive types](#recursive-types) | `t.recursion(name, definition)` | | ||
| refinement | ✘ | ✘ | `t.refinement(A, predicate)` | | ||
| strict/exact types | ✘ | `$Exact<{{ name: t.string }}>` | `t.strict({ name: t.string })` | | ||
# Tagged unions | ||
@@ -237,3 +239,3 @@ | ||
```ts | ||
const Person = t.interface({ | ||
const Person = t.type({ | ||
name: t.string, | ||
@@ -254,3 +256,3 @@ age: t.number | ||
```ts | ||
const A = t.interface({ | ||
const A = t.type({ | ||
foo: t.string | ||
@@ -277,9 +279,12 @@ }) | ||
```ts | ||
export function interfaceWithOptionals<R extends t.Props, O extends t.Props>( | ||
required: R, | ||
optional: O, | ||
export function interfaceWithOptionals<RequiredProps extends t.Props, OptionalProps extends t.Props>( | ||
required: RequiredProps, | ||
optional: OptionalProps, | ||
name?: string | ||
): t.IntersectionType< | ||
[t.InterfaceType<R, t.InterfaceOf<R>>, t.PartialType<O, t.PartialOf<O>>], | ||
t.InterfaceOf<R> & t.PartialOf<O> | ||
[ | ||
t.InterfaceType<RequiredProps, t.TypeOfProps<RequiredProps>>, | ||
t.PartialType<OptionalProps, t.TypeOfPartialProps<OptionalProps>> | ||
], | ||
t.TypeOfProps<RequiredProps> & t.TypeOfPartialProps<OptionalProps> | ||
> { | ||
@@ -300,7 +305,7 @@ return t.intersection([t.interface(required), t.partial(optional)], name) | ||
// represents a Date from an ISO string | ||
const DateFromString = new t.Type<t.mixed, Date>( | ||
const DateFromString = new t.Type<Date, string>( | ||
'DateFromString', | ||
(v): v is Date => v instanceof Date, | ||
(v, c) => | ||
t.string.validate(v, c).chain(s => { | ||
(m): m is Date => m instanceof Date, | ||
(m, c) => | ||
t.string.validate(m, c).chain(s => { | ||
const d = new Date(s) | ||
@@ -332,3 +337,6 @@ return isNaN(d.getTime()) ? t.failure(s, c) : t.success(d) | ||
```ts | ||
export function maybe<RT extends t.Any>(type: RT, name?: string): t.UnionType<[RT, t.NullType], t.TypeOf<RT> | null> { | ||
export function maybe<RT extends t.Any>( | ||
type: RT, | ||
name?: string | ||
): t.UnionType<[RT, t.NullType], t.TypeOf<RT> | null, t.OutputOf<RT> | null, t.InputOf<RT> | null> { | ||
return t.union<[RT, t.NullType]>([type, t.null], name) | ||
@@ -343,6 +351,6 @@ } | ||
```ts | ||
const pluck = <F extends string, U extends t.UnionType<Array<t.InterfaceType<{ [K in F]: t.Any }, any>>, any>>( | ||
const pluck = <F extends string, U extends t.UnionType<Array<t.InterfaceType<{ [K in F]: t.Mixed }>>>>( | ||
union: U, | ||
field: F | ||
): t.Type<t.mixed, t.TypeOf<U>[F]> => { | ||
): t.Type<t.TypeOf<U>[F]> => { | ||
return t.union(union.types.map(type => type.props[field])) | ||
@@ -352,11 +360,11 @@ } | ||
export const Action = t.union([ | ||
t.interface({ | ||
t.type({ | ||
type: t.literal('Action1'), | ||
payload: t.interface({ | ||
payload: t.type({ | ||
foo: t.string | ||
}) | ||
}), | ||
t.interface({ | ||
t.type({ | ||
type: t.literal('Action2'), | ||
payload: t.interface({ | ||
payload: t.type({ | ||
bar: t.string | ||
@@ -367,3 +375,3 @@ }) | ||
// ActionType: t.Type<t.mixed, "Action1" | "Action2"> | ||
// ActionType: t.Type<"Action1" | "Action2", "Action1" | "Action2", t.mixed> | ||
const ActionType = pluck(Action, 'type') | ||
@@ -402,4 +410,4 @@ ``` | ||
```ts | ||
const NestedInterface = t.interface({ | ||
foo: t.interface({ | ||
const NestedInterface = t.type({ | ||
foo: t.type({ | ||
bar: t.string | ||
@@ -406,0 +414,0 @@ }) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1118
0
419
102449
16
+ Addedfp-ts@1.19.5(transitive)
- Removedfp-ts@0.6.8(transitive)
Updatedfp-ts@^1.0.0