From 3bc3fdf678138956361f0a445a84f3bb0e3ae643 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 10 Nov 2017 09:28:27 +0100 Subject: [PATCH] add laws --- src/index.ts | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 29cea284b..0c0986862 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { Either, Left, Right, isRight } from 'fp-ts/lib/Either' -import { Predicate, identity } from 'fp-ts/lib/function' +import { Predicate } from 'fp-ts/lib/function' declare global { interface Array { @@ -27,6 +27,11 @@ export type Any = Type export type TypeOf = RT['_A'] export type InputOf = RT['_S'] +/** + * Laws: + * 1. validate(x).fold(() => x, serialize) = x + * 2. validate(serialize(x)) = Right(x) + */ export class Type { readonly _A: A readonly _S: S @@ -46,13 +51,15 @@ export class Type { } } +export const identity = (a: A): A => a + export const getFunctionName = (f: any): string => f.displayName || f.name || `` export const getContextEntry = (key: string, type: Any | NeverType): ContextEntry => ({ key, type }) export const getValidationError = (value: any, context: Context): ValidationError => ({ value, context }) -const pushAll = (xs: Array, ys: Array): void => Array.prototype.push.apply(xs, ys) +export const getDefaultContext = (type: Any): Context => [{ key: '', type }] export const failures = (errors: Errors): Validation => new Left(errors) @@ -61,11 +68,11 @@ export const failure = (value: any, context: Context): Validation => export const success = (value: T): Validation => new Right(value) -const getDefaultContext = (type: Any): Context => [{ key: '', type }] - export const validate = (value: S, type: Type): Validation => type.validate(value, getDefaultContext(type)) +const pushAll = (xs: Array, ys: Array): void => Array.prototype.push.apply(xs, ys) + // // basic types // @@ -77,7 +84,7 @@ export class NullType extends Type { } } -/** An alias of `null` */ +/** @alias `null` */ export const nullType: NullType = new NullType() export class UndefinedType extends Type { @@ -339,7 +346,6 @@ export const array = (type: RT, name?: string): ArrayType => export type Props = { [key: string]: Any } -// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed export type InterfaceOf

= { [K in keyof P]: TypeOf } const getNameFromProps = (props: Props): string => @@ -402,16 +408,14 @@ export class InterfaceType

extends Type> { } } -/** An alias of `interface` */ +/** @alias `interface` */ export const type =

(props: P, name?: string): InterfaceType

=> new InterfaceType(props, name) // // partials // -// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed export type PartialOf

= { [K in keyof P]?: TypeOf } -// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed export type PartialPropsOf

= { [K in keyof P]: UnionType<[P[K], UndefinedType]> } export class PartialType

extends Type> { @@ -731,11 +735,4 @@ export function strict

(props: P, name?: string): StrictType

return new StrictType(props, name) } -export { - identity, - nullType as null, - undefinedType as undefined, - arrayType as Array, - functionType as Function, - type as interface -} +export { nullType as null, undefinedType as undefined, arrayType as Array, functionType as Function, type as interface }