Skip to content

Commit

Permalink
object: add defaultTo
Browse files Browse the repository at this point in the history
fixes #7
  • Loading branch information
andrenarchy committed Jun 25, 2019
1 parent fecbeaa commit 226aab5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'

import { FefeError } from './errors'
import { object, optional, ObjectOptions } from './object'
import { object, defaultTo, optional, ObjectOptions } from './object'
import { string } from './string'

describe('object()', () => {
Expand Down Expand Up @@ -58,6 +58,14 @@ describe('object()', () => {
})
})

describe('defaultTo()', () => {
it('should return an object options object with default value/function', () => {
const validator = string()
const options: ObjectOptions<string> = defaultTo(validator, 'foo')
expect(options).to.eql({ validator, default: 'foo' })
})
})

describe('optional()', () => {
it('should return an optional object options object', () => {
const validator = string()
Expand Down
7 changes: 7 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export function object<D extends ObjectDefinition> (
}
}

export function defaultTo<R> (validator: Validator<R>, _default: R | (() => R)): ObjectOptions<R> {
return {
validator,
default: _default
}
}

export function optional<R> (validator: Validator<R>): ObjectOptions<R> {
return {
validator,
Expand Down

0 comments on commit 226aab5

Please sign in to comment.