hmpo-form-controller
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -16,2 +16,8 @@ var _ = require('underscore'); | ||
}); | ||
Object.defineProperty(this, 'title', { | ||
enumerable: true, | ||
get: function () { | ||
return options.title || this.getTitle(key, options, req, res); | ||
} | ||
}); | ||
} | ||
@@ -23,2 +29,6 @@ | ||
FormError.prototype.getTitle = function (/*key, options, req, res*/) { | ||
return 'Oops, something went wrong'; | ||
}; | ||
module.exports = FormError; |
@@ -20,2 +20,9 @@ var _ = require('underscore'), | ||
function customValidate() { | ||
debug('Applying custom %s validator with value %s', validator.name, value); | ||
if (!validator(value)) { | ||
return _.extend({ key: key }, { type: validator.name }); | ||
} | ||
} | ||
if (typeof validator === 'string') { | ||
@@ -28,2 +35,8 @@ validator = { | ||
return validate(); | ||
} else if (typeof validator === 'function') { | ||
if (validator.name) { | ||
return customValidate(); | ||
} else { | ||
throw new Error('Custom validator needs to be a named function'); | ||
} | ||
} else { | ||
@@ -30,0 +43,0 @@ throw new Error('Undefined validator:' + validator.type); |
{ | ||
"name": "hmpo-form-controller", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,2 +61,17 @@ # passports-form-controller | ||
#### Custom Validators | ||
Custom validator functions can be passed in field config. These must be named functions and the name is used as the error.type for looking up validation error messages. | ||
fields.js | ||
```js | ||
{ | ||
'field-1': { | ||
validate: ['required', function isTrue(val) { | ||
return val === true; | ||
}] | ||
} | ||
} | ||
``` | ||
### steps config | ||
@@ -126,1 +141,19 @@ | ||
``` | ||
### The FormError class | ||
FormError can be used as a façade to normalise different types of error one may receive / trigger, and to be subsequently returned from a controller. | ||
Its constructor takes a series of options. `title` and `message` have both getters and public methods to define default values. | ||
```js | ||
let error = new ErrorClass(this.missingDoB, { | ||
key: this.missingDob, | ||
type: 'required', | ||
redirect: '/missingData', | ||
title: 'Something went wrong', | ||
message: 'Please supply a valid date of birth'}); | ||
``` | ||
@@ -39,2 +39,12 @@ var ErrorClass = require('../../lib/error'); | ||
it('allows a custom title', function () { | ||
var err = new ErrorClass('field', { title: 'My error title' }); | ||
err.title.should.equal('My error title'); | ||
}); | ||
it('has a default title', function () { | ||
var err = new ErrorClass('field', {}); | ||
err.title.should.exist; | ||
}); | ||
}); |
var Validators = require('../../').validators; | ||
var validationHelper = require('../../lib/validation'); | ||
var _ = require('underscore'); | ||
@@ -587,2 +588,55 @@ | ||
describe('custom validators', function () { | ||
var fields, validator; | ||
beforeEach(function () { | ||
fields = { | ||
'field-1': { | ||
validate: [function doSomething() { | ||
return true; | ||
}] | ||
}, | ||
'field-2': { | ||
validate: [function () { | ||
return true; | ||
}] | ||
}, | ||
'field-3': { | ||
validate: [function fail() { | ||
return false; | ||
}] | ||
}, | ||
'field-4': { | ||
validate: [function checkVal(val) { | ||
return val === true; | ||
}] | ||
} | ||
}; | ||
validator = validationHelper(fields); | ||
}); | ||
it('accepts custom validators', function () { | ||
expect(validator('field-1', null)).to.be.undefined; | ||
}); | ||
it('throws an error if an anonymous function is passed', function () { | ||
try { | ||
validator('field-2'); | ||
} catch (err) { | ||
err.should.be.an('error') | ||
.and.have.property('message') | ||
.and.be.equal('Custom validator needs to be a named function'); | ||
} | ||
}); | ||
it('uses the name of the function as the error type', function () { | ||
validator('field-3').should.have.property('type') | ||
.and.be.equal('fail'); | ||
}); | ||
it('validates using the passed values', function () { | ||
expect(validator('field-4', true)).to.be.undefined; | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91831
2191
158