Skip to content

Provides ability to use strings as validators #167

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

Merged
merged 1 commit into from
Mar 22, 2017
Merged
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
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