Skip to content

Commit

Permalink
reuse a single Assurance instance
Browse files Browse the repository at this point in the history
  • Loading branch information
danmilon committed Jan 27, 2013
1 parent 89ef72a commit 085cb6a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
34 changes: 21 additions & 13 deletions lib/Assurance.js
Expand Up @@ -13,15 +13,21 @@ function Assurance(object, onlyFields, alias) {
if (!(this instanceof Assurance)) {
return new Assurance(object, onlyFields, alias)
}

this.restart(object, onlyFields, alias)

this.object = object
this.errors = []
return this
}

Assurance.prototype.restart = function (object, onlyFields, alias) {
this.only = undefined
this.alias = undefined

this.object = object
this._path = []
this.errors = []
this._skip = false
this._canPop = true
this.alias = undefined

if (typeof onlyFields === 'string') {
onlyFields = [onlyFields]
Expand All @@ -47,9 +53,6 @@ function Assurance(object, onlyFields, alias) {
return this
}

Assurance.validators = validators
Assurance.sanitizers = sanitizers

/**
* @return {Boolean} Whether any error has occured yet or not
*/
Expand Down Expand Up @@ -179,16 +182,21 @@ Assurance.prototype.optional = function () {
return this
}

Assurance.prototype.default = function (def) {
var val = this._val()
/**
* If the currently validated field is missing, val is assigned
*
* @param {} val Value to assign
*/
Assurance.prototype.default = function (val) {
var currVal = this._val()

var isInvalid =
(typeof val === 'undefined') ||
(val === null) ||
(val === '')
(typeof currVal === 'undefined') ||
(currVal === null) ||
(currVal === '')

if (isInvalid) {
this._val(def)
this._val(val)
}

return this
Expand All @@ -212,7 +220,7 @@ Assurance.prototype.nest = function (fn) {

if (Array.isArray(val)) {
var top = this._top()

for (var i = 0; i < val.length; i++) {
// set the index since it's an array
// this will be picked up by _param() to set
Expand Down
8 changes: 7 additions & 1 deletion lib/index.js
@@ -1,8 +1,14 @@
var Assurance = require('./Assurance')
, errors = require('./errors')
, AssuranceGroup = require('./AssuranceGroup')
, singleton = require('./singleton')
, validators = require('./validators')
, sanitizers = require('./sanitizers')

module.exports = Assurance

module.exports = singleton
module.exports.validators = validators
module.exports.sanitizers = sanitizers

module.exports.single = function (object, key, alias) {
var assure = new Assurance(object, alias)
Expand Down
8 changes: 8 additions & 0 deletions lib/singleton.js
@@ -0,0 +1,8 @@
var Assurance = require('./Assurance')


var assurance = new Assurance()

module.exports = function singleton(object, onlyFields, alias) {
return assurance.restart(object, onlyFields, alias)
}
20 changes: 18 additions & 2 deletions test/test.assurance.js
Expand Up @@ -32,14 +32,14 @@ describe('Assurance', function () {

describe('_constructor_', function () {
it('should return an Assurance instance', function () {
assurance(person).should.be.instanceOf(assurance)
assurance(person).should.be.instanceOf(assurance.Assurance)
})

it('onlyFields should be optional', function () {
should.not.exist(assurance(person).only)
})

it('onlyFields should be optional', function () {
it('alias should be optional', function () {
should.not.exist(assurance(person).alias)
})

Expand Down Expand Up @@ -156,6 +156,22 @@ describe('Assurance', function () {
describe('#nest', function () {
it('should nest validation', testNest)

it('should nest in arrays', function () {
var assure = assurance({
bands: ['cranberries', 'the doors', 666]
})

assure.me('bands').nest(function (band) {
band.is('string')
})

var errors = assure.end()

errors.should.have.length(1)
errors[0].type.should.equal('InvalidType')
errors[0].param.should.equal('bands[2]')
})

it('should nest in complex ways', function () {
var assure = assurance({
i: {
Expand Down

0 comments on commit 085cb6a

Please sign in to comment.