Skip to content

Commit f0d4c4e

Browse files
committed
:start: new: add validator for array
1 parent 03d9d1b commit f0d4c4e

File tree

6 files changed

+129
-6
lines changed

6 files changed

+129
-6
lines changed

dev/app.vue

+8
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,12 @@
229229
}
230230
}
231231
232+
.errors {
233+
.alert {
234+
padding: 4px;
235+
width: 80%;
236+
margin: 5px auto;
237+
}
238+
}
239+
232240
</style>

dev/schema.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ module.exports = {
7878
"AngularJS",
7979
"ReactJS",
8080
"VueJS"
81-
]
81+
],
82+
min: 2,
83+
max: 4,
84+
validator: validators.array
8285
},
8386
{
8487
type: "text",
@@ -263,14 +266,12 @@ module.exports = {
263266
label: "City",
264267
model: "address.city",
265268
multi: true,
266-
required: true,
267269
validator: validators.required
268270
},
269271
{
270272
type: "text",
271273
label: "Street",
272-
model: "address.streetC",
273-
required: true
274+
model: "address.streetC"
274275
},
275276
{
276277
type: "text",

src/fields/abstractField.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default {
5454
validate() {
5555
this.clearValidationErrors();
5656

57-
if (this.schema.validator) {
57+
if (this.schema.validator && this.schema.readonly !== true && this.disabled !== true) {
5858

5959
let validators = [];
6060
if (!isArray(this.schema.validator)) {

src/utils/validators.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNil, isNumber, isString } from "lodash";
1+
import { isNil, isNumber, isString, isArray } from "lodash";
22
import moment from "moment";
33

44
function checkEmpty(value, required) {
@@ -61,6 +61,27 @@ module.exports = {
6161
return err;
6262
},
6363

64+
array(value, field) {
65+
if (field.required) {
66+
67+
if (!isArray(value))
68+
return ["Value is not an array!"];
69+
70+
if (value.length == 0)
71+
return ["This field is required!"];
72+
}
73+
74+
if (!isNil(value)) {
75+
if (!isNil(field.min))
76+
if (value.length < field.min)
77+
return ["Select minimum " + field.min + " items!"];
78+
79+
if (!isNil(field.max))
80+
if (value.length > field.max)
81+
return ["Select maximum " + field.max + " items!"];
82+
}
83+
},
84+
6485
date(value, field) {
6586
let res = checkEmpty(value, field.required); if (res != null) return res;
6687

test/unit/specs/fields/abstractField.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,51 @@ describe("abstractField.vue", () => {
197197

198198
});
199199

200+
describe("check validate function if field is disabled", () => {
201+
let schema = {
202+
type: "text",
203+
label: "Name",
204+
model: "name",
205+
validator: sinon.spy()
206+
};
207+
208+
let model = { name: "John Doe" };
209+
210+
beforeEach( () => {
211+
createField(schema, model, true);
212+
});
213+
214+
it("should not call schema validator", () => {
215+
schema.validator.reset();
216+
field.validate();
217+
expect(schema.validator.callCount).to.be.equal(0);
218+
});
219+
220+
});
221+
222+
describe("check validate function if field is readonly", () => {
223+
let schema = {
224+
type: "text",
225+
label: "Name",
226+
model: "name",
227+
readonly: true,
228+
validator: sinon.spy()
229+
};
230+
231+
let model = { name: "John Doe" };
232+
233+
beforeEach( () => {
234+
createField(schema, model);
235+
});
236+
237+
it("should not call schema validator", () => {
238+
schema.validator.reset();
239+
field.validate();
240+
expect(schema.validator.callCount).to.be.equal(0);
241+
});
242+
243+
});
244+
200245
describe("check validate function with validator array", () => {
201246
let spy1 = sinon.spy();
202247
let spy2 = sinon.spy();

test/unit/specs/utils/validators.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,54 @@ describe("Validators", () => {
129129
});
130130
});
131131

132+
describe.only("test Validators.array", () => {
133+
134+
let field = {
135+
required: true,
136+
min: 2,
137+
max: 4
138+
}
139+
140+
it("should give error if value is null, but field is required", () => {
141+
check(v.array, null, field, 1);
142+
});
143+
144+
it("should give error if count of items is smaller than min", () => {
145+
check(v.array, [], field, 1);
146+
check(v.array, [1], field, 1);
147+
check(v.array, ["ab"], field, 1);
148+
check(v.array, [true], field, 1);
149+
});
150+
151+
it("should give error if count of items is greater than max", () => {
152+
check(v.array, [1,2,3,4,5], field, 1);
153+
});
154+
155+
it("should give error if value is not array", () => {
156+
check(v.array, 123, field, 1);
157+
check(v.array, true, field, 1);
158+
check(v.array, "John", field, 1);
159+
});
160+
161+
it("should not give error", () => {
162+
check(v.array, [1,4], field, 0);
163+
check(v.array, ["John", "Doe", "Jane"], field, 0);
164+
check(v.array, [true, true, false], field, 0);
165+
check(v.array, [ [5], [3] ], field, 0);
166+
});
167+
168+
it("should not give error if value is null and field is not required", () => {
169+
field.required = false;
170+
check(v.array, null, field, 0);
171+
});
172+
173+
it("should give error if count of item is smaller than minimum and field is not required", () => {
174+
field.required = false;
175+
check(v.array, ["Foo"], field, 1);
176+
});
177+
178+
});
179+
132180
describe("test Validators.date", () => {
133181

134182
let field = {

0 commit comments

Comments
 (0)