Skip to content

Commit

Permalink
bug fix: now default values don't override provided values
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Pai committed Jan 8, 2017
1 parent e390a67 commit 923076d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Osom (schemaBlueprint, globalRules) {
const isInvalidType = isCastingDisabled && !isSameType
if (isInvalidType) throwTypeError(name, schemaTypes[name], rule.required)

if (rule.casting && hasValue) value = rule.type(obj[name])
if (hasValue) value = rule.casting ? rule.type(obj[name]) : obj[name]
else if (rule.default) value = !isFunction(rule.default) ? rule.default : rule.default()

// lodash.flow is buggy, this is a workaround (and dep-free)
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ describe('schema defintion', function () {
var validator = osom(schema)
validator().should.be.eql({age: 23})
})

it('based in a fn with precedence given to provided values', function () {
var rand = () => String(Math.random())
var schema = {
age: {
type: String, default: rand
}
}

var validator = osom(schema, { casting: false })
validator({ age: '23' }).should.be.eql({ age: '23' })
})
})

it('support transforms', function () {
Expand Down

0 comments on commit 923076d

Please sign in to comment.