Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 6, 2016
1 parent 5b8923c commit c0d8bcb
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@
// })

var isFunction = require('lodash/isfunction')
var isArray = require('lodash/isArray')
var isString = require('lodash/isString')
var isArray = require('lodash/isarray')
var isString = require('lodash/isstring')
var reduce = require('lodash/reduce')
var assign = require('lodash/assign')
var flow = require('lodash/flow')
var Whoops = require('whoops')
var chaste = require('chaste')

var ENOTYPE = Whoops.create('ENOTYPE')
var EARDENT = Whoops.create('EARDENT')

// TODO: Add disable casting {casting: false}
// TODO: Add validation like mongoose
// validate: {
// validator: function(v) {
// return /\d{3}-\d{3}-\d{4}/.test(v)
// },
// message: '{VALUE} is not a valid phone number!'
// },

function exists (value) {
return value != null
Expand All @@ -47,14 +57,18 @@ function addRule (schema, blueprint, name) {
return schema
}

function throwError (name, type, required) {
function throwTypeError (name, type, required) {
var msg
if (isArray(required) && isString(required[1])) msg = required[1]
else msg = 'Need to provide {' + type + "} for '" + name + "' field."
var err = ENOTYPE(msg)
throw err
}

function throwError () {
throw EARDENT.apply([EARDENT, arguments])
}

function Ardent (schemaBlueprint) {
if (!(this instanceof Ardent)) return new Ardent(schemaBlueprint)

Expand All @@ -77,15 +91,15 @@ function Ardent (schemaBlueprint) {
var applyFilters = flow(rule.filter)
var hasValue = exists(obj[name])

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

var value

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

objSchema[name] = applyFilters(value)
objSchema[name] = applyFilters(value, throwError)
return objSchema
}, {})
}
Expand Down

0 comments on commit c0d8bcb

Please sign in to comment.