Skip to content

Commit 01f0554

Browse files
authored
Merge pull request #167 from jmverges/master
Provides ability to use strings as validators
2 parents 3ee0865 + 36c7829 commit 01f0554

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

dist/vfg-core.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vfg.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fields/abstractField.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { get as objGet, each, isFunction, isString, isArray } from "lodash";
2+
import validators from "../utils/validators";
3+
4+
function convertValidator(validator) {
5+
if (isString(validator)) {
6+
if (validators[validator] != null)
7+
return validators[validator];
8+
else {
9+
console.warn(`'${validator}' is not a validator function!`);
10+
return null; // caller need to handle null
11+
}
12+
}
13+
return validator;
14+
}
215

316
export default {
417
props: [
@@ -69,10 +82,10 @@ export default {
6982

7083
let validators = [];
7184
if (!isArray(this.schema.validator)) {
72-
validators.push(this.schema.validator.bind(this));
85+
validators.push(convertValidator(this.schema.validator).bind(this));
7386
} else {
7487
each(this.schema.validator, (validator) => {
75-
validators.push(validator.bind(this));
88+
validators.push(convertValidator(validator).bind(this));
7689
});
7790
}
7891

test/unit/specs/VueFormGenerator.spec.js

+47-5
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,16 @@ describe("VueFormGenerator.vue", () => {
523523
});
524524
});
525525

526-
});
526+
});
527527

528528
describe("check validate", () => {
529529
let schema = {
530530
fields: [
531-
{
532-
type: "input",
531+
{
532+
type: "input",
533533
inputType: "text",
534-
label: "Name",
535-
model: "name",
534+
label: "Name",
535+
model: "name",
536536
min: 3,
537537
validator: VueFormGenerator.validators.string
538538
}
@@ -567,6 +567,48 @@ describe("VueFormGenerator.vue", () => {
567567

568568
});
569569

570+
describe("check validate with validator as string instead of object", () => {
571+
let schema = {
572+
fields: [
573+
{
574+
type: "input",
575+
inputType: "text",
576+
label: "Name",
577+
model: "name",
578+
min: 3,
579+
validator: "string"
580+
}
581+
]
582+
};
583+
584+
let model = { name: "John Doe" };
585+
let form;
586+
587+
before( () => {
588+
createFormGenerator(schema, model);
589+
form = vm.$refs.form;
590+
});
591+
592+
it("should empty the errors", () => {
593+
expect(form.errors).to.be.length(0);
594+
expect(form.validate()).to.be.true;
595+
expect(form.errors).to.be.length(0);
596+
});
597+
598+
it("should give an validation error", () => {
599+
model.name = "Ab";
600+
expect(form.validate()).to.be.false;
601+
expect(form.errors).to.be.length(1);
602+
});
603+
604+
it("should no validation error", () => {
605+
model.name = "Abc";
606+
expect(form.validate()).to.be.true;
607+
expect(form.errors).to.be.length(0);
608+
});
609+
610+
});
611+
570612
describe("check if option null", () => {
571613
let schema = {
572614
fields: [

0 commit comments

Comments
 (0)