Skip to content

Commit

Permalink
Merge branch 'finboxio-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 8, 2017
2 parents 498adce + df9d07e commit f0ac8ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var schema = {
var validator = osom(schema)

// validate it!
validator({age: ' 23 '}) // => {age: '23'}
validator({title: ' 23 '}) // => {title: '23'}
```

## Usage
Expand Down
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
42 changes: 33 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,44 +117,68 @@ describe('schema defintion', function () {
})
})

describe('support casting', function () {
describe('support casting by default', function () {
it('enable (by default)', function () {
var schema = { age: Array }
var validator = osom(schema)
validator({age: '23'}).should.be.eql({age: ['23']})
})
})

it('disable explicit', function () {
describe('casting disabled explicitly', function () {
it('throw an error under different type provided', function () {
var schema = {
age: {
type: String,
casting: false
type: String
}
}

var errMessage = "Expected {string} for 'age'."
var validator = osom(schema)
var validator = osom(schema, {casting: false})

;[{age: 23}].forEach(function (obj) {
;(function () { validator(obj) }).should.throw(errMessage)
})
})

it('disable explicit works with nill values', function () {
it('works with nill values of the same type', function () {
[Number, String, Function, Boolean].forEach(function (type) {
var schema = {
age: {
type: type,
casting: false
type: type
}
}

var validator = osom(schema)
var validator = osom(schema, {casting: false})
;[null, {}, {age: null}].forEach(function (data) {
validator(data).should.be.eql({})
})
})
})

it('works with default value (not necessary of the same type defined)', function () {
var schema = {
age: {
type: String,
default: 23
}
}

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

it('works with a value provided of the same type', function () {
var schema = {
age: {
type: String,
default: '24'
}
}

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

describe('support validation', function () {
Expand Down

0 comments on commit f0ac8ee

Please sign in to comment.