Skip to content

Commit

Permalink
Merge pull request #167 from jmverges/master
Browse files Browse the repository at this point in the history
Provides ability to use strings as validators
  • Loading branch information
icebob committed Mar 22, 2017
2 parents 3ee0865 + 36c7829 commit 01f0554
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions dist/vfg-core.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/vfg.js

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { get as objGet, each, isFunction, isString, isArray } from "lodash";
import validators from "../utils/validators";

function convertValidator(validator) {
if (isString(validator)) {
if (validators[validator] != null)
return validators[validator];
else {
console.warn(`'${validator}' is not a validator function!`);
return null; // caller need to handle null
}
}
return validator;
}

export default {
props: [
Expand Down Expand Up @@ -69,10 +82,10 @@ export default {

let validators = [];
if (!isArray(this.schema.validator)) {
validators.push(this.schema.validator.bind(this));
validators.push(convertValidator(this.schema.validator).bind(this));
} else {
each(this.schema.validator, (validator) => {
validators.push(validator.bind(this));
validators.push(convertValidator(validator).bind(this));
});
}

Expand Down
52 changes: 47 additions & 5 deletions test/unit/specs/VueFormGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,16 @@ describe("VueFormGenerator.vue", () => {
});
});

});
});

describe("check validate", () => {
let schema = {
fields: [
{
type: "input",
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
label: "Name",
model: "name",
min: 3,
validator: VueFormGenerator.validators.string
}
Expand Down Expand Up @@ -567,6 +567,48 @@ describe("VueFormGenerator.vue", () => {

});

describe("check validate with validator as string instead of object", () => {
let schema = {
fields: [
{
type: "input",
inputType: "text",
label: "Name",
model: "name",
min: 3,
validator: "string"
}
]
};

let model = { name: "John Doe" };
let form;

before( () => {
createFormGenerator(schema, model);
form = vm.$refs.form;
});

it("should empty the errors", () => {
expect(form.errors).to.be.length(0);
expect(form.validate()).to.be.true;
expect(form.errors).to.be.length(0);
});

it("should give an validation error", () => {
model.name = "Ab";
expect(form.validate()).to.be.false;
expect(form.errors).to.be.length(1);
});

it("should no validation error", () => {
model.name = "Abc";
expect(form.validate()).to.be.true;
expect(form.errors).to.be.length(0);
});

});

describe("check if option null", () => {
let schema = {
fields: [
Expand Down

0 comments on commit 01f0554

Please sign in to comment.