Skip to content

Commit

Permalink
ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 31, 2016
1 parent 260a6c6 commit 86610bc
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
'use strict'

var isFunction = require('lodash.isfunction')
var isBoolean = require('lodash.isboolean')
var assign = require('lodash.assign')
var reduce = require('lodash.reduce')
var merge = require('lodash.merge')
var chaste = require('chaste')
var type = require('kind-of')

function exists (value) {
return value != null
}
const isFunction = require('lodash.isfunction')
const isBoolean = require('lodash.isboolean')
const assign = require('lodash.assign')
const reduce = require('lodash.reduce')
const merge = require('lodash.merge')
const chaste = require('chaste')
const type = require('kind-of')

const exists = (value) => value != null

var DEFAULT = {
const DEFAULT = {
BLUEPRINT: {
casting: true,
transform: []
}
}

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

Expand All @@ -31,7 +29,7 @@ function addRule (globalRules, schema, blueprint, name) {
}

function createTypeError (message, field) {
var error = new TypeError(message)
const error = new TypeError(message)
error.field = field
return error
}
Expand All @@ -42,7 +40,7 @@ function throwTypeError (name, type, message) {
}

function throwValidationError (name, value, description) {
var message
let message
if (description) message = description.replace('{VALUE}', value)
else message = `Fail '${value}' validation for '${name}'.`
throw createTypeError(message, name)
Expand All @@ -51,11 +49,11 @@ function throwValidationError (name, value, description) {
function Osom (schemaBlueprint, globalRules) {
if (!(this instanceof Osom)) return new Osom(schemaBlueprint, globalRules)
globalRules = globalRules || {}
var schemaTypes = {}
const schemaTypes = {}

var schema = reduce(schemaBlueprint, function (schema, blueprint, name) {
const schema = reduce(schemaBlueprint, function (schema, blueprint, name) {
schema = addRule(globalRules, schema, blueprint, name)
var type = schema[name].type
const type = schema[name].type
schemaTypes[name] = type.name.toLowerCase()
schema[name].type = chaste(type)
return schema
Expand All @@ -65,26 +63,24 @@ function Osom (schemaBlueprint, globalRules) {
obj = obj || {}

return reduce(schema, function applyRule (objSchema, rule, name) {
var value = obj[name]
var hasValue = exists(value)
var isMissing = rule.required && !hasValue
let value = obj[name]
const hasValue = exists(value)
const isMissing = rule.required && !hasValue
if (isMissing) throwTypeError(name, schemaTypes[name], rule.required)

var isCastingDisabled = hasValue && !rule.casting
var isSameType = type(value) === schemaTypes[name]
var isInvalidType = isCastingDisabled && !isSameType
const isCastingDisabled = hasValue && !rule.casting
const isSameType = type(value) === schemaTypes[name]
const isInvalidType = isCastingDisabled && !isSameType
if (isInvalidType) throwTypeError(name, schemaTypes[name], rule.required)

if (rule.casting && hasValue) value = rule.type(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)
value = reduce(rule.transform, function (acc, fn) {
return fn(acc)
}, value)
value = reduce(rule.transform, (acc, fn) => fn(acc), value)

if (rule.validate) {
var validator = isFunction(rule.validate) ? rule.validate : rule.validate.validator
const validator = isFunction(rule.validate) ? rule.validate : rule.validate.validator
if (!validator(value)) throwValidationError(name, value, rule.validate.message)
}

Expand Down

0 comments on commit 86610bc

Please sign in to comment.