Skip to content

Commit

Permalink
feat: infer required if validator is present
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 20, 2019
1 parent c54672d commit a79256c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ function Osom (schemaBlueprint, globalRules) {
return reduce(schema, function applyRule (objSchema, rule, name) {
const value = obj[name]
const hasValue = !isNil(value)
const isRequired = rule.required
const isMissing = isRequired && !hasValue
const validate = rule.validate
const isRequired = rule.required || !isNil(validate)
const expectedValue = schemaTypes[name]

const isMissing = !hasValue && !validate && isRequired
if (isMissing) throwTypeError(name, value, expectedValue, isRequired)

const isCasting = rule.casting
Expand All @@ -78,13 +79,13 @@ function Osom (schemaBlueprint, globalRules) {

let TypedValue
const defaultValue = rule.default
const validate = rule.validate

if (hasValue) TypedValue = isCasting ? rule.type(value) : value
else if (defaultValue) TypedValue = !isFunction(defaultValue) ? defaultValue : defaultValue()

TypedValue = reduce(rule.transform, (acc, fn) => fn(acc), TypedValue)

if (hasValue && validate) {
if (isRequired && validate) {
const validator = getValidator(rule)
if (!validator(TypedValue)) throwValidationError(name, value, validate.message)
}
Expand Down
25 changes: 6 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ describe('schema defintion', function () {
}

var validator = osom(schema)
var errMessage = "Fail '25' validation for 'age'."
;(function () { validator({ age: 25 }) }).should.throw(errMessage)
;(function () { validator() }).should.throw("Fail 'undefined' validation for 'age'.")
;(function () { validator({}) }).should.throw("Fail 'undefined' validation for 'age'.")
;(function () { validator({ age: 25 }) }).should.throw("Fail '25' validation for 'age'.")
})

it('based in a object key', function () {
Expand All @@ -210,23 +211,9 @@ describe('schema defintion', function () {
}

var validator = osom(schema)
var errMessage = "Fail '25' validation for 'age'."
;(function () { validator({ age: 25 }) }).should.throw(errMessage)
})

it('just run validator when the field is present', function () {
var schema = {
name: String,
age: {
type: String,
validate: function (v) {
return v === '23'
}
}
}

var validator = osom(schema)
validator({ name: 'foo bar' }).should.be.eql({ name: 'foo bar' })
;(function () { validator() }).should.throw("Fail 'undefined' validation for 'age'.")
;(function () { validator({}) }).should.throw("Fail 'undefined' validation for 'age'.")
;(function () { validator({ age: 25 }) }).should.throw("Fail '25' validation for 'age'.")
})

it('custom error message', function () {
Expand Down

0 comments on commit a79256c

Please sign in to comment.