Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require special validator before processing #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/date-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ util.inherits(DateController, Parent);
DateController.prototype.process = function process(req) {
_.each(this.options.fields, function processEach(v, k) {
if (
_.includes(v.validate, 'input-date') &&
(undefined !== req.body[k + '-year']) &&
(undefined !== req.body[k + '-month']) &&
(undefined !== req.body[k + '-day'])
Expand All @@ -21,7 +22,7 @@ DateController.prototype.process = function process(req) {

var processField = function processField(key, validators) {
this.options.fields[key] = {
validate: childValidators.concat(validators),
validate: _.union(childValidators, validators),
dependent: v.dependent
};

Expand Down
47 changes: 42 additions & 5 deletions test/spec/spec.date-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ describe('Date Controller', function () {
});

describe('process', function () {

var form, handler, req, res, cb, options, key;

describe('when a field is expected but not required', function () {
describe('when an input-date field is expected but not required', function () {
beforeEach(function () {
key = 'date-field';
options = { template: 'index', fields: {} };
options.fields[key] = {}
options.fields[key] = { validate: ['input-date'] }
form = new DateController(options);
sinon.stub(form, 'get').yields();
sinon.stub(form, 'post').yields();
Expand Down Expand Up @@ -279,11 +278,11 @@ describe('Date Controller', function () {
});
});

describe('when a field is expected and required', function () {
describe('when an input-date field is expected and required', function () {
beforeEach(function () {
key = 'date-field';
options = { template: 'index', fields: {} };
options.fields[key] = { validate: ['required'] }
options.fields[key] = { validate: ['required', 'input-date'] }
form = new DateController(options);
sinon.stub(form, 'get').yields();
sinon.stub(form, 'post').yields();
Expand Down Expand Up @@ -341,5 +340,43 @@ describe('Date Controller', function () {
});
});
});

describe('when an input-date field is not expected', function () {
beforeEach(function () {
key = 'date-field';
options = { template: 'index', fields: {} };
options.fields[key] = { validate: [] }
form = new DateController(options);
sinon.stub(form, 'get').yields();
sinon.stub(form, 'post').yields();
// use a spy instead of a stub so that the length is unaffected
sinon.spy(form, 'errorHandler');
req = request({
url: '/test',
params: {}
}),
res = {
send: sinon.stub()
};
cb = function callback() {};
});

describe('when an input-date is submitted for the field with valid data', function () {
beforeEach(function () {
req.body[key + '-year'] = '1999';
req.body[key + '-month'] = '12';
req.body[key + '-day'] = '01';

form._process(req, res, cb);
});

it('does NOT process the field as an input-date', function () {
req.form.values[key].should.eql('');
should.not.exist(req.form.values[key + '-year']);
should.not.exist(req.form.values[key + '-month']);
should.not.exist(req.form.values[key + '-day']);
});
});
});
});
});