Skip to content

Commit af31c2b

Browse files
authored
Validation should pass if form data is empty (#7)
1 parent 8697a86 commit af31c2b

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

src/Validator/Form.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isArray } from '../Utils/functions';
22
import ViolationBuilder from '../Utils/ViolationBuilder';
33
import Validator from './Validator';
44

5+
const FORM_ERROR_FIELD = 'form';
56
const MESSAGE_EXTRA_FIELDS = 'This form should not contain extra fields.';
67

78
export default class Form {
@@ -22,6 +23,7 @@ export default class Form {
2223
this.violationBuilder = new ViolationBuilder();
2324
this.fields = {};
2425
this.data = {};
26+
this.errors = {};
2527
}
2628

2729
/**
@@ -65,50 +67,64 @@ export default class Form {
6567
* @return {Array}
6668
*/
6769
validate(data) {
68-
const errors = {};
69-
70+
this.errors = {};
7071
this.data = data;
7172

7273
if (!this.options.extra_fields) {
73-
let extra = this.hasExtraFields(data);
74-
if (extra) {
75-
errors['form'] = [extra];
76-
}
74+
this.checkExtraFields();
7775
}
7876

79-
if (Object.keys(errors).length > 0) {
80-
return errors;
77+
if (Object.keys(this.errors).length > 0) {
78+
return this.errors;
8179
}
8280

83-
Object.keys(data).forEach((field) => {
84-
const value = data[field];
81+
Object.keys(this.fields).forEach((field) => {
82+
const value = this.data[field];
83+
const errors = this.validator.validate(value, this.fields[field].constants, {form: this});
8584

86-
if (typeof this.fields[field] === 'undefined') {
87-
return true;
85+
if (errors.length > 0) {
86+
this.addValidationErrors(field, errors);
8887
}
88+
});
8989

90-
const fieldErrors = this.validator.validate(value, this.fields[field].constants, {form: this});
90+
return this.errors;
91+
}
9192

92-
if (fieldErrors.length > 0) {
93-
errors[field] = fieldErrors;
94-
}
95-
});
93+
/**
94+
* @param {string} field
95+
* @param {Error|Error[]} error
96+
* @return {Form}
97+
*/
98+
addValidationErrors(field, error) {
99+
if (!isArray(this.errors[field])) {
100+
this.errors[field] = [];
101+
}
96102

97-
return errors;
103+
if (isArray(error)) {
104+
this.errors[field] = this.errors[field].concat(error);
105+
} else {
106+
this.errors[field].push(error);
107+
}
108+
109+
return this;
98110
}
99111

100112
/**
101-
* @param {Object} data
102-
* @return {Error|undefined}
113+
* @return {Form}
103114
*/
104-
hasExtraFields(data) {
105-
for (const field of Object.keys(data)) {
115+
checkExtraFields() {
116+
for (const field of Object.keys(this.data)) {
106117
if (typeof this.fields[field] === 'undefined') {
107-
return this.violationBuilder.build(this.options.extra_fields_message);
118+
this.addValidationErrors(
119+
FORM_ERROR_FIELD,
120+
this.violationBuilder.build(this.options.extra_fields_message)
121+
);
122+
123+
break;
108124
}
109125
}
110126

111-
return;
127+
return this;
112128
}
113129

114130
getData() {

tests/Validator/Form.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('Form', function () {
156156
.add('email')
157157
.add('password');
158158

159-
let e = form.validate({
159+
const e = form.validate({
160160
email: 'email@example.com',
161161
password: '1234567',
162162
ip: '8.8.8.8',
@@ -183,6 +183,20 @@ describe('Form', function () {
183183

184184
assert.strictEqual(Object.keys(e).length, 0);
185185
});
186+
187+
it('Validation empty form data', function () {
188+
const form = new Form({extra_fields: true});
189+
190+
form.add('email', [
191+
new NotBlank(),
192+
new Email(),
193+
new Length({min: 17}),
194+
]);
195+
196+
let e = form.validate({});
197+
198+
assert.strictEqual(Object.keys(e).length, 1);
199+
});
186200
});
187201

188202
describe('#getData()', function () {

0 commit comments

Comments
 (0)