Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary type + interface, dictionary combinators #20

Merged
merged 3 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

# 0.1.0

- **Breaking Change**
- upgrade to TypeScript 2.2.0, fix #6 (@gcanti)
- rename `object` to `record`
- rename `ObjectType` to `RecordType`
- rename `Object` to `Pojo`
- **New Feature**
- add support for jsnext

- **Breaking Changes**
- `t.Object` type. Renamed to `t.Dictionary`, now accepts arrays so is fully equivalent to `{ [key: string]: any }`.
- `t.instanceOf` combinator. Removed.
- `t.object` combinator. Renamed to `t.interface`. `ObjectType` to `InterfaceType`. Excess properties are now pruned.
- `mapping` combinator. Renamed to `dictionary`. `MappingType` to `DictionaryType`.
- `intersection` combinator. Due to the new excess property pruning in `t.interface` now only accept `InterfaceType`s.

# 0.0.2

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ import * as t from 'io-ts'
| number | `number` | `t.number` |
| boolean | `boolean` | `t.boolean` |
| generic array | `Array<any>` | `t.Array` |
| generic object | `{ [key: string]: any }` | `t.Pojo` |
| generic function | `Function` | `t.Function` |
| generic dictionary | `{ [key: string]: any }` | `t.Dictionary` |
| function | `Function` | `t.Function` |
| instance of `C` | `C` | `t.instanceOf(C)` |
| list | `Array<A>` | `t.array(A)` |
| arrays | `Array<A>` | `t.array(A)` |
| literal | `'s'` | `t.literal('s')` |
| maybe | `A | undefined | null` | `t.maybe(A)` |
| mapping | `{ [key: A]: B }` | `t.mapping(A, B)` |
| dictionaries | `{ [key: A]: B }` | `t.dictionary(A, B)` |
| refinement | ✘ | `t.refinement(A, predicate)` |
| record | `{ name: string }` | `t.record({ name: t.string })` |
| interface | `{ name: string }` | `t.interface({ name: t.string })` |
| tuple | `[A, B]` | `t.tuple([A, B])` |
| union | `A | B` | `t.union([A, B])` |
| intersection | `A & B` | `t.intersection([A, B])` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"typings": "lib/index.d.ts",
"scripts": {
"test": "mocha -r ts-node/register test/*.ts",
"build": "rm -rf lib/* && tsc && tsc -m es6 --outDir lib-jsnext"
"build": "rm -rf lib/* && rm -rf lib-jsnext/* && tsc && tsc -m es6 --outDir lib-jsnext"
},
"repository": {
"type": "git",
Expand Down
10 changes: 5 additions & 5 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export abstract class Either<L, A> extends HKT<HKT<'Either', L>, A> {

export class Left<L, A> extends Either<L, A> {
constructor(private value: L){ super() }
map<B>(f: (a: A) => B): Either<L, B> {
map<B>(_: (a: A) => B): Either<L, B> {
return this as any as Either<L, B>
}
ap<B>(fab: Either<L, (a: A) => B>): Either<L, B> {
ap<B>(_: Either<L, (a: A) => B>): Either<L, B> {
return this as any as Either<L, B>
}
chain<B>(f: (a: A) => Either<L, B>): Either<L, B> {
chain<B>(_: (a: A) => Either<L, B>): Either<L, B> {
return this as any as Either<L, B>
}
fold<B>(l: (l: L) => B, r: (a: A) => B): B {
fold<B>(l: (l: L) => B, _: (a: A) => B): B {
return l(this.value)
}
}
Expand All @@ -43,7 +43,7 @@ export class Right<L, A> extends Either<L, A> {
chain<B>(f: (a: A) => Either<L, B>): Either<L, B> {
return f(this.value)
}
fold<B>(l: (l: L) => B, r: (a: A) => B): B {
fold<B>(_: (l: L) => B, r: (a: A) => B): B {
return r(this.value)
}
}
Expand Down
160 changes: 68 additions & 92 deletions src/index.ts

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/reporters/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { Reporter } from './Reporter'
import { Context, getFunctionName, isFailure } from '../index'

Expand Down
10 changes: 5 additions & 5 deletions test/mapping.ts → test/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import {
number2
} from './helpers'

describe('mapping', () => {
describe('dictionary', () => {

it('should succeed validating a valid value', () => {
const T = t.mapping(t.refinement(t.string, s => s.length >= 2), t.number)
const T = t.dictionary(t.refinement(t.string, s => s.length >= 2), t.number)
assertSuccess(t.validate({}, T))
assertSuccess(t.validate({ aa: 1 }, T))
})

it('should return the same reference if validation succeeded if nothing changed', () => {
const T = t.mapping(t.refinement(t.string, s => s.length >= 2), t.number)
const T = t.dictionary(t.refinement(t.string, s => s.length >= 2), t.number)
const value = { aa: 1 }
assertStrictEqual(t.validate(value, T), value)
})

it('should return a new reference if validation succeeded and something changed', () => {
const T = t.mapping(t.refinement(t.string, s => s.length >= 2), number2)
const T = t.dictionary(t.refinement(t.string, s => s.length >= 2), number2)
const value = { aa: 1 }
assertDeepEqual(t.validate(value, T), { aa: 2 })
})

it('should fail validating an invalid value', () => {
const T = t.mapping(t.refinement(t.string, s => s.length >= 2), t.number)
const T = t.dictionary(t.refinement(t.string, s => s.length >= 2), t.number)
assertFailure(t.validate({ a: 1 }, T), [
'Invalid value "a" supplied to : { [key: (string | <function1>)]: number }/a: (string | <function1>)'
])
Expand Down
31 changes: 0 additions & 31 deletions test/instanceOf.ts

This file was deleted.

27 changes: 12 additions & 15 deletions test/record.ts → test/interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as assert from 'assert'
import * as t from '../src/index'
import {
assertSuccess,
Expand All @@ -7,32 +8,33 @@ import {
number2
} from './helpers'

describe('record', () => {
describe('interface', () => {

it('should succeed validating a valid value', () => {
const T = t.record({ a: t.string })
const T = t.interface({ a: t.string })
assertSuccess(t.validate({ a: 's' }, T))
})

it('should preserve additional props', () => {
const T = t.record({ a: t.string })
assertDeepEqual(t.validate({ a: 's', b: 1 }, T), { a: 's', b: 1 })
it('should remove unknown properties', () => {
const T = t.interface({ a: t.string })
const value = t.fromValidation({ a: 's', b: 1 }, T)
assert.deepEqual(value, { a: 's' })
})

it('should return the same reference if validation succeeded and nothing changed', () => {
const T = t.record({ a: t.string })
const T = t.interface({ a: t.string })
const value = { a: 's' }
assertStrictEqual(t.validate(value, T), value)
})

it('should return the a new reference if validation succeeded and something changed', () => {
const T = t.record({ a: number2, b: t.number })
const value = { a: 1, b: 2, c: 3 }
assertDeepEqual(t.validate(value, T), { a: 2, b: 2, c: 3 })
const T = t.interface({ a: number2, b: t.number })
const value = { a: 1, b: 2 }
assertDeepEqual(t.validate(value, T), { a: 2, b: 2 })
})

it('should fail validating an invalid value', () => {
const T = t.record({ a: t.string })
const T = t.interface({ a: t.string })
assertFailure(t.validate(1, T), [
'Invalid value 1 supplied to : { a: string }'
])
Expand All @@ -44,9 +46,4 @@ describe('record', () => {
])
})

it('should allow for additional props', () => {
const T = t.record({ a: t.string })
assertSuccess(t.validate({ a: 's', additional: 2 }, T))
})

})
18 changes: 12 additions & 6 deletions test/intersection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as assert from 'assert'
import * as t from '../src/index'
import {
assertSuccess,
Expand All @@ -10,27 +11,32 @@ import {
describe('intersection', () => {

it('should succeed validating a valid value', () => {
const T = t.intersection([t.record({ a: t.number }), t.record({ b: t.number })])
const T = t.intersection([t.interface({ a: t.number }), t.interface({ b: t.number })])
assertSuccess(t.validate({ a: 1, b: 2 }, T))
assertSuccess(t.validate({ a: 1, b: 2, c: 3 }, T))
})

it('should remove unknown properties', () => {
const T = t.intersection([t.interface({ a: t.number }), t.interface({ b: t.number })])
const value = t.fromValidation({ a: 1, b: 1, c: true }, T)
assert.deepEqual(value, { a: 1, b: 1 })
})

it('should return the same reference if validation succeeded and nothing changed', () => {
const T = t.intersection([t.record({ a: t.number }), t.record({ b: t.number })])
const T = t.intersection([t.interface({ a: t.number }), t.interface({ b: t.number })])
const value = { a: 1, b: 2 }
assertStrictEqual(t.validate(value, T), value)
})

it('should return a new reference if validation succeeded and something changed', () => {
const T = t.intersection([t.record({ a: number2 }), t.record({ b: t.number })])
const T = t.intersection([t.interface({ a: number2 }), t.interface({ b: t.number })])
const value = { a: 1, b: 2 }
assertDeepEqual(t.validate(value, T), { a: 2, b: 2 })
})

it('should fail validating an invalid value', () => {
const T = t.intersection([t.record({ a: t.number }), t.record({ b: t.number })])
const T = t.intersection([t.interface({ a: t.number }), t.interface({ b: t.number })])
assertFailure(t.validate({ a: 1 }, T), [
'Invalid value undefined supplied to : ({ a: number } & { b: number })/1: { b: number }/b: number'
'Invalid value undefined supplied to : ({ a: number } & { b: number })/b: number'
])
})

Expand Down
40 changes: 40 additions & 0 deletions test/irreducibles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as t from '../src/index'
import {
assertSuccess,
assertFailure
} from './helpers'

describe('Dictionary', () => {

it('should accept arrays', () => {
assertSuccess(t.validate([], t.Dictionary))
})

it('should accept objects', () => {
assertSuccess(t.validate({}, t.Dictionary))
})

it('should fail with primitives', () => {
const T = t.Dictionary
assertFailure(t.validate('s', T), [
'Invalid value "s" supplied to : Dictionary'
])
assertFailure(t.validate(1, T), [
'Invalid value 1 supplied to : Dictionary'
])
assertFailure(t.validate(true, T), [
'Invalid value true supplied to : Dictionary'
])
})

it('should fail with null and undefined', () => {
const T = t.Dictionary
assertFailure(t.validate(null, T), [
'Invalid value null supplied to : Dictionary'
])
assertFailure(t.validate(undefined, T), [
'Invalid value undefined supplied to : Dictionary'
])
})

})
2 changes: 1 addition & 1 deletion test/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('maybe', () => {
})

it('should return the same reference if validation succeeded', () => {
const T = t.maybe(t.Pojo)
const T = t.maybe(t.Dictionary)
const value = {}
assertStrictEqual(t.validate(value, T), value)
})
Expand Down
6 changes: 3 additions & 3 deletions test/recursion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
describe('recursion', () => {

it('should succeed validating a valid value', () => {
const T = t.recursion('T', self => t.record({
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
}))
Expand All @@ -17,7 +17,7 @@ describe('recursion', () => {
})

it('should return the same reference if validation succeeded', () => {
const T = t.recursion('T', self => t.record({
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
}))
Expand All @@ -26,7 +26,7 @@ describe('recursion', () => {
})

it('should fail validating an invalid value', () => {
const T = t.recursion('T', self => t.record({
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
}))
Expand Down
2 changes: 1 addition & 1 deletion test/refinement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('refinement', () => {
})

it('should return the same reference if validation succeeded', () => {
const T = t.refinement(t.Pojo, () => true)
const T = t.refinement(t.Dictionary, () => true)
const value = {}
assertStrictEqual(t.validate(value, T), value)
})
Expand Down
2 changes: 1 addition & 1 deletion test/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('union', () => {
})

it('should return the same reference if validation succeeded', () => {
const T = t.union([t.Pojo, t.number])
const T = t.union([t.Dictionary, t.number])
const value = {}
assertStrictEqual(t.validate(value, T), value)
})
Expand Down