Skip to content

Commit

Permalink
refactor: remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 20, 2019
1 parent 31c15b6 commit bf823a9
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

const { isFunction, isBoolean, merge, reduce } = require('lodash')
const { isNil, isFunction, isBoolean, merge, reduce } = require('lodash')
const chaste = require('chaste')
const type = require('kind-of')

const exists = (value) => value != null
const is = require('kind-of')

const DEFAULT = {
BLUEPRINT: {
Expand All @@ -15,8 +13,8 @@ const DEFAULT = {

function createSchemaRule (rule, globalRules) {
const schema = isFunction(rule) ? { type: rule } : rule
const fields = merge({}, globalRules, schema)
return Object.assign({}, DEFAULT.BLUEPRINT, fields)
const fields = merge(globalRules, schema)
return Object.assign({ ...DEFAULT.BLUEPRINT, ...fields })
}

function addRule (globalRules, schema, blueprint, name) {
Expand Down Expand Up @@ -66,7 +64,7 @@ function Osom (schemaBlueprint, globalRules) {

return reduce(schema, function applyRule (objSchema, rule, name) {
const value = obj[name]
const hasValue = exists(value)
const hasValue = !isNil(value)
const isRequired = rule.required
const isMissing = isRequired && !hasValue
const expectedValue = schemaTypes[name]
Expand All @@ -75,8 +73,7 @@ function Osom (schemaBlueprint, globalRules) {

const isCasting = rule.casting
const isCastingDisabled = hasValue && !isCasting
const isSameType = type(value) === expectedValue
const isInvalidType = isCastingDisabled && !isSameType
const isInvalidType = isCastingDisabled && is(value) !== expectedValue
if (isInvalidType) throwTypeError(name, value, expectedValue, isRequired)

let TypedValue
Expand All @@ -85,15 +82,14 @@ function Osom (schemaBlueprint, globalRules) {
if (hasValue) TypedValue = isCasting ? rule.type(value) : value
else if (defaultValue) TypedValue = !isFunction(defaultValue) ? defaultValue : defaultValue()

// lodash.flow is buggy, this is a workaround (and dep-free)
TypedValue = reduce(rule.transform, (acc, fn) => fn(acc), TypedValue)

if (hasValue && validate) {
const validator = getValidator(rule)
if (!validator(TypedValue)) throwValidationError(name, value, validate.message)
}

if (exists(TypedValue)) objSchema[name] = TypedValue
if (!isNil((TypedValue))) objSchema[name] = TypedValue
return objSchema
}, {})
}
Expand Down

0 comments on commit bf823a9

Please sign in to comment.