Skip to content

Commit 8cdf540

Browse files
committed
add laws
1 parent 379d61f commit 8cdf540

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/index.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Either, Left, Right, isRight } from 'fp-ts/lib/Either'
2-
import { Predicate, identity } from 'fp-ts/lib/function'
2+
import { Predicate } from 'fp-ts/lib/function'
33

44
declare global {
55
interface Array<T> {
@@ -27,6 +27,11 @@ export type Any = Type<any, any>
2727
export type TypeOf<RT extends Any> = RT['_A']
2828
export type InputOf<RT extends Any> = RT['_S']
2929

30+
/**
31+
* Laws:
32+
* 1. validate(x).fold(() => x, serialize) = x
33+
* 2. validate(serialize(x)) = Right(x)
34+
*/
3035
export class Type<S, A> {
3136
readonly _A: A
3237
readonly _S: S
@@ -46,13 +51,15 @@ export class Type<S, A> {
4651
}
4752
}
4853

54+
export const identity = <A>(a: A): A => a
55+
4956
export const getFunctionName = (f: any): string => f.displayName || f.name || `<function${f.length}>`
5057

5158
export const getContextEntry = (key: string, type: Any | NeverType): ContextEntry => ({ key, type })
5259

5360
export const getValidationError = (value: any, context: Context): ValidationError => ({ value, context })
5461

55-
const pushAll = <A>(xs: Array<A>, ys: Array<A>): void => Array.prototype.push.apply(xs, ys)
62+
export const getDefaultContext = (type: Any): Context => [{ key: '', type }]
5663

5764
export const failures = <T>(errors: Errors): Validation<T> => new Left(errors)
5865

@@ -61,11 +68,11 @@ export const failure = <T>(value: any, context: Context): Validation<T> =>
6168

6269
export const success = <T>(value: T): Validation<T> => new Right<Errors, T>(value)
6370

64-
const getDefaultContext = (type: Any): Context => [{ key: '', type }]
65-
6671
export const validate = <S, A>(value: S, type: Type<S, A>): Validation<A> =>
6772
type.validate(value, getDefaultContext(type))
6873

74+
const pushAll = <A>(xs: Array<A>, ys: Array<A>): void => Array.prototype.push.apply(xs, ys)
75+
6976
//
7077
// basic types
7178
//
@@ -77,7 +84,7 @@ export class NullType extends Type<any, null> {
7784
}
7885
}
7986

80-
/** An alias of `null` */
87+
/** @alias `null` */
8188
export const nullType: NullType = new NullType()
8289

8390
export class UndefinedType extends Type<any, undefined> {
@@ -339,7 +346,6 @@ export const array = <RT extends Any>(type: RT, name?: string): ArrayType<RT> =>
339346

340347
export type Props = { [key: string]: Any }
341348

342-
// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
343349
export type InterfaceOf<P extends Props> = { [K in keyof P]: TypeOf<P[K]> }
344350

345351
const getNameFromProps = (props: Props): string =>
@@ -402,16 +408,14 @@ export class InterfaceType<P extends Props> extends Type<any, InterfaceOf<P>> {
402408
}
403409
}
404410

405-
/** An alias of `interface` */
411+
/** @alias `interface` */
406412
export const type = <P extends Props>(props: P, name?: string): InterfaceType<P> => new InterfaceType(props, name)
407413

408414
//
409415
// partials
410416
//
411417

412-
// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
413418
export type PartialOf<P extends Props> = { [K in keyof P]?: TypeOf<P[K]> }
414-
// TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
415419
export type PartialPropsOf<P extends Props> = { [K in keyof P]: UnionType<[P[K], UndefinedType]> }
416420

417421
export class PartialType<P extends Props> extends Type<any, PartialOf<P>> {
@@ -731,11 +735,4 @@ export function strict<P extends Props>(props: P, name?: string): StrictType<P>
731735
return new StrictType(props, name)
732736
}
733737

734-
export {
735-
identity,
736-
nullType as null,
737-
undefinedType as undefined,
738-
arrayType as Array,
739-
functionType as Function,
740-
type as interface
741-
}
738+
export { nullType as null, undefinedType as undefined, arrayType as Array, functionType as Function, type as interface }

0 commit comments

Comments
 (0)