Skip to content

Commit

Permalink
fixes #362 - integer validator now calls number validator, and re…
Browse files Browse the repository at this point in the history
…turns `invalidIntegerl: "The value is not an integer"` as well as any errors generated by `number`

* wrapped one-liner conditionals/loops with `{}` (looks cleaner)
* simplified some conditionals by merging `if(cond1) { if(cond2) { } }` into `if(cond1 && cond2) { }`
  • Loading branch information
zoul0813 committed Dec 13, 2017
1 parent 268631a commit 8d436be
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 53 deletions.
6 changes: 3 additions & 3 deletions dist/vfg-core.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/vfg.js

Large diffs are not rendered by default.

125 changes: 80 additions & 45 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, isNil, isNumber, isString, isArray, isFunction } from "lodash";
import { defaults, isNil, isNumber, isInteger, isString, isArray, isFunction } from "lodash";
import fecha from "fecha";

let resources = {
Expand All @@ -8,6 +8,7 @@ let resources = {
numberTooSmall: "The number is too small! Minimum: {0}",
numberTooBig: "The number is too big! Maximum: {0}",
invalidNumber: "Invalid number",
invalidInteger: "The value is not an integer",

textTooSmall: "The length of text is too small! Current: {0}, Minimum: {1}",
textTooBig: "The length of text is too big! Current: {0}, Maximum: {1}",
Expand Down Expand Up @@ -35,23 +36,26 @@ let resources = {

function checkEmpty(value, required, messages = resources) {
if (isNil(value) || value === "") {
if (required)
if (required) {
return [msg(messages.fieldIsRequired)];
else
} else {
return [];
}
}
return null;
}

function msg(text) {
if (text != null && arguments.length > 1)
for (let i = 1; i < arguments.length; i++)
if (text != null && arguments.length > 1) {
for (let i = 1; i < arguments.length; i++) {
text = text.replace("{" + (i - 1) + "}", arguments[i]);
}
}

return text;
}

module.exports = {
const validators = {

resources,

Expand All @@ -60,47 +64,59 @@ module.exports = {
},

number(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let err = [];
if (isNumber(value)) {
if (!isNil(field.min) && value < field.min)
if (!isNil(field.min) && value < field.min) {
err.push(msg(messages.numberTooSmall, field.min));
}

if (!isNil(field.max) && value > field.max)
if (!isNil(field.max) && value > field.max) {
err.push(msg(messages.numberTooBig, field.max));

} else
}
} else {
err.push(msg(messages.invalidNumber));
}

return err;
},

integer(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;
let errs = validators.number(value, field, model, messages);

if (!(Number(value) === value && value % 1 === 0))
return [msg(messages.invalidNumber)];
if (!isInteger(value)) {
errs.push(msg(messages.invalidInteger));
}

return errs;
},

double(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

if (!isNumber(value) || isNaN(value))
if (!isNumber(value) || isNaN(value)) {
return [msg(messages.invalidNumber)];
}
},

string(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let err = [];
if (isString(value)) {
if (!isNil(field.min) && value.length < field.min)
if (!isNil(field.min) && value.length < field.min) {
err.push(msg(messages.textTooSmall, value.length, field.min));
}

if (!isNil(field.max) && value.length > field.max)
if (!isNil(field.max) && value.length > field.max) {
err.push(msg(messages.textTooBig, value.length, field.max));

}
} else
err.push(msg(messages.thisNotText));

Expand All @@ -109,77 +125,89 @@ module.exports = {

array(value, field, model, messages = resources) {
if (field.required) {

if (!isArray(value))
if (!isArray(value)) {
return [msg(messages.thisNotArray)];
}

if (value.length == 0)
if (value.length == 0) {
return [msg(messages.fieldIsRequired)];
}
}

if (!isNil(value)) {
if (!isNil(field.min))
if (value.length < field.min)
return [msg(messages.selectMinItems, field.min)];
if (!isNil(field.min) && value.length < field.min) {
return [msg(messages.selectMinItems, field.min)];
}

if (!isNil(field.max))
if (value.length > field.max)
return [msg(messages.selectMaxItems, field.max)];
if (!isNil(field.max) && value.length > field.max) {
return [msg(messages.selectMaxItems, field.max)];
}
}
},

date(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let m = new Date(value);
if (!m)
if (!m) {
return [msg(messages.invalidDate)];
}

let err = [];

if (!isNil(field.min)) {
let min = new Date(field.min);
if (m.valueOf() < min.valueOf())
if (m.valueOf() < min.valueOf()) {
err.push(msg(messages.dateIsEarly, fecha.format(m), fecha.format(min)));
}
}

if (!isNil(field.max)) {
let max = new Date(field.max);
if (m.valueOf() > max.valueOf())
if (m.valueOf() > max.valueOf()) {
err.push(msg(messages.dateIsLate, fecha.format(m), fecha.format(max)));
}
}

return err;
},

regexp(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

if (!isNil(field.pattern)) {
let re = new RegExp(field.pattern);
if (!re.test(value))
if (!re.test(value)) {
return [msg(messages.invalidFormat)];
}
}
},

email(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; // eslint-disable-line no-useless-escape
if (!re.test(value))
if (!re.test(value)) {
return [msg(messages.invalidEmail)];
}
},

url(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let re = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g; // eslint-disable-line no-useless-escape
if (!re.test(value))
if (!re.test(value)) {
return [msg(messages.invalidURL)];
}
},

creditCard(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

/* From validator.js code
https://github.com/chriso/validator.js/blob/master/src/lib/isCreditCard.js
Expand Down Expand Up @@ -209,27 +237,34 @@ module.exports = {
shouldDouble = !shouldDouble;
}

if (!((sum % 10) === 0 ? sanitized : false))
if (!((sum % 10) === 0 ? sanitized : false)) {
return [msg(messages.invalidCardNumber)];
}
},

alpha(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let re = /^[a-zA-Z]*$/;
if (!re.test(value))
if (!re.test(value)) {
return [msg(messages.invalidTextContainNumber)];
}
},

alphaNumeric(value, field, model, messages = resources) {
let res = checkEmpty(value, field.required, messages); if (res != null) return res;
let res = checkEmpty(value, field.required, messages);
if (res != null) return res;

let re = /^[a-zA-Z0-9]*$/;
if (!re.test(value))
if (!re.test(value)) {
return [msg(messages.invalidTextContainSpec)];
}
}
};

module.exports = validators;

Object.keys(module.exports).forEach(name => {
const fn = module.exports[name];
if (isFunction(fn)) {
Expand Down
4 changes: 3 additions & 1 deletion test/unit/specs/utils/validators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ describe("Validators", () => {
let field = {};

it("should give error if value is not integer", () => {
// invalid integer
check(v.integer, 3.14, field, 1);
check(v.integer, "3.14", field, 1);
// invalid number, invalid integer
check(v.integer, "3.14", field, 2);
});

it("should not give error if value is integer", () => {
Expand Down

0 comments on commit 8d436be

Please sign in to comment.