Skip to content

Commit

Permalink
add object type, closes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Nov 8, 2017
1 parent f60d6d4 commit 8fa3ccb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

# 0.8.2

- **New Feature**
- add `object` type, closes #86 (@gcanti)

# 0.8.1

- **New Feature**
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ import * as t from 'io-ts'
| boolean | `boolean` | `t.boolean` |
| any | `any` | `t.any` |
| never | `never` | `t.never` |
| object | `object` | `t.object` |
| integer || `t.Integer` |
| array of any | `Array<any>` | `t.Array` |
| array of type | `Array<A>` | `t.array(A)` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "0.8.1",
"version": "0.8.2",
"description": "TypeScript compatible runtime type system for IO validation",
"files": ["lib"],
"main": "lib/index.js",
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ export class AnyDictionaryType implements Type<{ [key: string]: any }> {

export const Dictionary: AnyDictionaryType = new AnyDictionaryType()

export class ObjectType implements Type<object> {
readonly _tag: 'ObjectType' = 'ObjectType'
readonly _A: object
readonly name: 'object' = 'object'
readonly validate: Validate<object> = Dictionary.validate
}

export const object: ObjectType = new ObjectType()

export class FunctionType implements Type<Function> {
readonly _tag: 'FunctionType' = 'FunctionType'
readonly _A: Function
Expand Down
23 changes: 23 additions & 0 deletions test/default-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ describe('null', () => {
assertFailure(t.validate(1, t.null), ['Invalid value 1 supplied to : null'])
})
})

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

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

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

it('should fail with null and undefined', () => {
const T = t.object
assertFailure(t.validate(null, T), ['Invalid value null supplied to : object'])
assertFailure(t.validate(undefined, T), ['Invalid value undefined supplied to : object'])
})
})
9 changes: 9 additions & 0 deletions typings-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,12 @@ const x33input = { name: 'foo', foo: 'foo' }
const x33: TS1 = x33input
// $ExpectError Argument of type 'StringType' is not assignable to parameter of type 'Props'
const S2 = t.strict(t.string)

//
// object
//
const O1 = t.object
type TO1 = TypeOf<typeof O1>
const x34: TO1 = { name: 'Giulio' }
// $ExpectError Type '"foo"' is not assignable to type 'object'
const x35: TO1 = 'foo'

0 comments on commit 8fa3ccb

Please sign in to comment.