Skip to content

Commit

Permalink
Support default fn values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 6, 2016
1 parent 829077a commit a3517f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ function Ardent (schemaBlueprint) {

if (rule.required && !hasValue) throwError(name, schemaTypes[name])

var value = hasValue ? rule.type(obj[name]) : rule.default
var value

if (hasValue) value = rule.type(obj[name])
else if (typeof rule.default !== 'function') value = rule.default
else value = rule.default()

objSchema[name] = applyFilters(value)
return objSchema
}, {})
Expand Down
27 changes: 20 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@ describe('schema defintion', function () {
})
})

it('support default value', function () {
var schema = {
age: {
type: Number, default: 23
describe('support default value', function () {
it('based in a value', function () {
var schema = {
age: {
type: Number, default: 23
}
}
}

var ardent = Ardent(schema)
ardent().should.be.eql({age: 23})
var ardent = Ardent(schema)
ardent().should.be.eql({age: 23})
})

it('based in a fn', function () {
var schema = {
age: {
type: Number, default: function () { return 23 }
}
}

var ardent = Ardent(schema)
ardent().should.be.eql({age: 23})
})
})

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

0 comments on commit a3517f6

Please sign in to comment.