Skip to content

Commit

Permalink
revert 37c74a5, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 23, 2017
1 parent 47ed6ab commit ad69c2d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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.9.3

* **Bug Fix**
* revert 37c74a5e2038de063a950f9ba8d18b1f132ef450, closes #8 (@gcanti)

# 0.9.2

* **New Feature**
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.9.2",
"version": "0.9.3",
"description": "TypeScript compatible runtime type system for IO validation",
"files": ["lib"],
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export const partial = <P extends Props>(
): PartialType<P, PartialOf<P>> => {
const partials: Props = {}
for (let k in props) {
partials[k] = union([undefinedType, props[k]])
partials[k] = union([props[k], undefinedType])
}
const partial = type(partials)
return new PartialType(
Expand Down
9 changes: 9 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ export const NumberFromString = new t.Type<string, number>(
)

export const IntegerFromString = t.refinement(NumberFromString, t.Integer.is, 'IntegerFromString')

export function withDefault<T extends t.Any>(type: T, defaultValue: t.TypeOf<T>): t.Type<t.InputOf<T>, t.TypeOf<T>> {
return new t.Type(
`withDefault(${type.name}, ${JSON.stringify(defaultValue)})`,
type.is,
(v, c) => type.validate(v != null ? v : defaultValue, c),
type.serialize
)
}
20 changes: 17 additions & 3 deletions test/partial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as assert from 'assert'
import * as t from '../src/index'
import { assertSuccess, assertFailure, assertStrictEqual, DateFromNumber } from './helpers'
import {
assertSuccess,
assertFailure,
assertStrictEqual,
DateFromNumber,
assertDeepEqual,
withDefault
} from './helpers'

describe('partial', () => {
it('should succeed validating a valid value', () => {
Expand Down Expand Up @@ -36,8 +43,8 @@ describe('partial', () => {
it('should fail validating an invalid value', () => {
const T = t.partial({ a: t.number })
assertFailure(t.validate({ a: 's' }, T), [
'Invalid value "s" supplied to : PartialType<{ a: number }>/a: (undefined | number)/0: undefined',
'Invalid value "s" supplied to : PartialType<{ a: number }>/a: (undefined | number)/1: number'
'Invalid value "s" supplied to : PartialType<{ a: number }>/a: (number | undefined)/0: number',
'Invalid value "s" supplied to : PartialType<{ a: number }>/a: (number | undefined)/1: undefined'
])
})

Expand All @@ -64,4 +71,11 @@ describe('partial', () => {
assert.strictEqual(T2.is({ a: 0 }), false)
assert.strictEqual(T2.is(undefined), false)
})

it('should support default values', () => {
const T = t.partial({
name: withDefault(t.string, 'foo')
})
assertDeepEqual(t.validate({}, T), { name: 'foo' })
})
})

0 comments on commit ad69c2d

Please sign in to comment.