Last active
April 9, 2018 06:15
-
-
Save riston/a545bc1772809bbbd3d46039fb26b3fa to your computer and use it in GitHub Desktop.
io-ts library testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import t, { | |
array, type, string, number, mixed, boolean, union, | |
null as Null, undefined as Undefined, Type, literal, | |
success, failure, | |
} from 'io-ts'; | |
import { ThrowReporter } from 'io-ts/lib/ThrowReporter' | |
import { PathReporter } from 'io-ts/lib/PathReporter' | |
import { reporter } from 'io-ts-reporters'; | |
const Name = string; | |
const validationResult = Name.decode('Juku'); | |
if (validationResult.isRight()) { | |
console.log(validationResult.value); | |
} | |
console.log(validationResult.isRight()); | |
/***/ | |
// Combined type | |
const PersonType = type({ | |
name: string, | |
age: number | |
}); | |
// Extract the static type | |
type Person = t.TypeOf<typeof PersonType>; | |
const payload = { name: 'Miku', age: '30' }; | |
const personResult = PersonType.decode(payload); | |
try { | |
ThrowReporter.report(personResult); | |
} catch (e) { | |
console.log('Error captured', e); | |
} | |
// Using path reporter | |
console.log(PathReporter.report(personResult)); | |
// Simple external reporter [ 'Expecting number at age but instead got: '30'.' ] | |
console.log(reporter(personResult)); | |
/***/ | |
// Additional helper functions | |
const DateFromString = new Type<Date, string>( | |
'DateFromString', | |
(m): m is Date => m instanceof Date, | |
(m, c) => | |
string.validate(m, c).chain(s => { | |
const d = new Date(s) | |
return isNaN(d.getTime()) ? failure(s, c) : success(d) | |
}), | |
a => a.toISOString() | |
); | |
function maybe<RT extends t.Any>( | |
type: RT, | |
name?: string | |
): t.UnionType<[RT, t.UndefinedType], t.TypeOf<RT> | undefined, t.OutputOf<RT> | undefined, t.InputOf<RT> | undefined> { | |
return union<[RT, t.UndefinedType]>([type, Undefined], name) | |
} | |
const AddonType = type({ | |
amount: number, | |
description: string, | |
never_expires: maybe(boolean), | |
}); | |
type Addon = t.TypeOf<typeof AddonType>; | |
const KindType = union([literal('yearly'), literal('monthly')]); | |
type Kind = t.TypeOf<typeof KindType>; | |
const SubscriptionType = type({ | |
kind: KindType, | |
next_billing_date: DateFromString, | |
failure_count: number, | |
addons: array(AddonType), | |
}); | |
type Subscription = t.TypeOf<typeof SubscriptionType>; | |
const subPayload= { | |
kind: 'blabla', | |
next_billing_date: '1973-11-29T23:00:00.000Z', | |
addons: [{ | |
amount: 10, | |
description: 'Monthly referral bonus', | |
never_expires: false, | |
}, { | |
amount: 15, | |
description: 'Friend referral', | |
}] | |
}; | |
const subResult = SubscriptionType.decode(subPayload); | |
console.log(PathReporter.report(subResult)); | |
console.log(reporter(subResult)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Juku | |
true | |
Error captured Invalid value "30" supplied to : { name: string, age: number }/age: number | |
[ 'Invalid value "30" supplied to : { name: string, age: number }/age: number' ] | |
[ 'Expecting number at age but instead got: "30".' ] | |
[ { value: 'blabla', context: [ [Object], [Object], [Object] ] }, | |
{ value: 'blabla', context: [ [Object], [Object], [Object] ] }, | |
{ value: undefined, context: [ [Object], [Object] ] } ] | |
[ 'Invalid value "blabla" supplied to : { kind: ("yearly" | "monthly"), next_billing_date: DateFromString, failure_count: number, addons: Array<{ amount: number, description: string, never_expires: (boolean | undefined) }> }/kind: ("yearly" | "monthly")/0: "yearly"', | |
'Invalid value "blabla" supplied to : { kind: ("yearly" | "monthly"), next_billing_date: DateFromString, failure_count: number, addons: Array<{ amount: number, description: string, never_expires: (boolean | undefined) }> }/kind: ("yearly" | "monthly")/1: "monthly"', | |
'Invalid value undefined supplied to : { kind: ("yearly" | "monthly"), next_billing_date: DateFromString, failure_count: number, addons: Array<{ amount: number, description: string, never_expires: (boolean | undefined) }> }/failure_count: number' ] | |
[ 'Expecting "yearly" at kind.0 but instead got: "blabla".', | |
'Expecting "monthly" at kind.1 but instead got: "blabla".', | |
'Expecting number at failure_count but instead got: undefined.' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment