Skip to content

Commit

Permalink
fixed #11
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnbytesio committed Mar 6, 2019
1 parent 1101da5 commit d3a8c3b
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 55 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Expand Up @@ -287,13 +287,13 @@ class Validator {
value = value.trim();
break;

case 'boolean':
value = String(value);
break;
// case 'boolean':
// value = String(value);
// break;

case 'number':
value = String(value);
break;
// case 'number':
// value = String(value);
// break;

case 'undefined':
value = '';
Expand Down
11 changes: 10 additions & 1 deletion src/lib/empty.js
Expand Up @@ -5,11 +5,20 @@
*/
module.exports = function empty(value) {

// this will check: null,undefined,NaN, string "", 0, false
if (!value) {
return true;
}

return !value.toString().trim();

}

module.exports.reallyEmpty = function reallyEmpty(value) {

if (!value && [false, 0].indexOf(value) < 0) {
return true;
}

return !value.toString().trim();

}
2 changes: 1 addition & 1 deletion src/rules/alpha.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function alpha(field, value) {

if (v.isAlpha(value)) {
if (v.isAlpha(String(value))) {

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/alphaNumeric.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function alphaNumeric(field, value) {

if (!v.isAlphanumeric(value + '')) {
if (!v.isAlphanumeric(String(value))) {

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/ascii.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function ascii(field, value) {

if (v.isAscii(value)) {
if (v.isAscii(String(value))) {

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/base64.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function base64(field, value) {

if (v.isBase64(value)) {
if (v.isBase64(String(value))) {

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/contains.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function contains(field, value, inString) {

if (!v.contains(value, inString.toString())) {
if (!v.contains(String(value), inString.toString())) {

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/creditCard.js
Expand Up @@ -3,7 +3,7 @@ const v = require('validator');

module.exports = async function creditCard(field, value) {

if (v.isCreditCard(value)) {
if (v.isCreditCard(String(value))) {

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/domain.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function domain(field, value) {
return v.isFQDN(value);
return v.isFQDN(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/email.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function email(field, value) {

if (!v.isEmail(value)) {
if (!v.isEmail(String(value))) {

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/hash.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function hash(field, value, hash) {
return v.isHash(value, hash);
return v.isHash(String(value), hash);
}
2 changes: 1 addition & 1 deletion src/rules/hex.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function hex(field, value) {
return v.isHexadecimal(value);
return v.isHexadecimal(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/hexColor.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function hexColor(field, value) {
return v.isHexColor(value);
return v.isHexColor(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/integer.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function integer(field, value) {

if (!v.isInt(value + '')) {
if (!v.isInt(String(value))) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/ip.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function ip(field, value) {

if (!v.isIP(value)) {
if (!v.isIP(String(value))) {

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/iso8601.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function iso8601(field, value) {
return v.isISO8601(value);
return v.isISO8601(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/json.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function json(field, value) {

if (!v.isJSON(value)) {
if (!v.isJSON(String(value))) {

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/latLong.js
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function latLong(field, value) {

if (v.isLatLong(value)) {
if (v.isLatLong(String(value))) {

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/macAddress.js
@@ -1,5 +1,5 @@
const v = require('validator');

module.exports = async function macAddress(field, value) {
return v.isMACAddress(value);
return v.isMACAddress(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/mongoId.js
Expand Up @@ -2,5 +2,5 @@ const v = require('validator');

module.exports = async function mongoId(field, value) {

return v.isMongoId(value);
return v.isMongoId(String(value));
}
2 changes: 1 addition & 1 deletion src/rules/phoneNumber.js
Expand Up @@ -2,5 +2,5 @@ const v = require('validator');

module.exports = async function phoneNumber(field, value) {
//@ts-ignore
return v.isMobilePhone(value);
return v.isMobilePhone(String(value));
}
9 changes: 5 additions & 4 deletions src/rules/requiredIf.js
@@ -1,5 +1,5 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');
const { pathIndex } = require('../lib/ObjectIndex');

module.exports = async function requiredIf(field, value, args) {

Expand All @@ -25,16 +25,17 @@ module.exports = async function requiredIf(field, value, args) {
}

// field is required if all values are presented
if (!empty(pathIndex(this.inputs,requiredField))
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) {
if (!empty(pathIndex(this.inputs, requiredField))
&& pathIndex(this.inputs, requiredField).toString() == requiredValue) {
required = true;
} else {
required = false;
break
}
}

if (required && empty(value)) {

if (required && empty.reallyEmpty(value)) {
return false;
}

Expand Down
19 changes: 4 additions & 15 deletions src/validator.js
Expand Up @@ -2,6 +2,8 @@ const validationRules = require('./rules');
const postValidationRules = require('./postRules');
const filters = require('./filters');

const reallyEmpty = require('./lib/empty').reallyEmpty;

const implicitRules = [
'required', 'requiredIf', 'requiredNotIf', 'requiredWith', 'requiredWithout', 'accepted', 'sometimes', 'nullable'
];
Expand Down Expand Up @@ -30,12 +32,8 @@ module.exports.applyRules = async function apply(field, validator) {

let item = validator.inputs[fieldArr[0]][i];

//console.log(item, fieldArr);

let indexedField = fieldName.replace('*', i);

//fieldWithIndex.push(indexedField);

let value = '';

let fieldArrLen = fieldArr.length;
Expand All @@ -62,17 +60,8 @@ module.exports.applyRules = async function apply(field, validator) {

field.value = validator.inputVal(field.field, field.multiple);

// if (field.value == false) {
// field.value = String(field.value);
// }

//console.log('validated rule', field.rules[r], field);
//console.log('--------------------------------------');

const rule = field.rules[r].rule;

//console.log('field value is', field.value);

if (rule == 'nullable' || field.nullable === true && field.value == null) {
continue;
}
Expand All @@ -81,11 +70,11 @@ module.exports.applyRules = async function apply(field, validator) {
throw new Error('Invalid Validation Rule: ' + rule + ' does not exist');
}

if (!field.required && validator.isEmpty(field.value)) {
if (!field.required && reallyEmpty(field.value)) {
continue;
}

let ruleArgs = [field.field, field.value || ''];
let ruleArgs = [field.field, field.value];

if (field.rules[r].args) {
ruleArgs.push(field.rules[r].args);
Expand Down
24 changes: 14 additions & 10 deletions test/rules/requiredIf.js
Expand Up @@ -6,7 +6,7 @@ const Validator = require('../../index');
describe('requiredIf', function () {


it('should return true', async () => {
it('single seed test: should return true', async () => {

const v = new Validator({ name: 'Harcharan Singh', sex: 'male', age: 16 }, { sex: 'requiredIf:age,16' });

Expand All @@ -16,26 +16,26 @@ describe('requiredIf', function () {

});

it('should return true', async () => {
it('with nested seed: should return true', async () => {

const v = new Validator({ name: 'Harcharan Singh',address:{street:"fantastic"}, age: 16 }, { age: 'requiredIf:address.street,fantastic' });
const v = new Validator({ name: 'Harcharan Singh', address: { street: "fantastic" }, age: 16 }, { age: 'requiredIf:address.street,fantastic' });

const matched = await v.check();

assert.equal(matched, true);

});
it('should return false', async () => {
it('with nested seed: should return false', async () => {

const v = new Validator({ name: 'Harcharan Singh',address:{street:"fantastic"} }, { age: 'requiredIf:address.street,fantastic' });
const v = new Validator({ name: 'Harcharan Singh', address: { street: "fantastic" } }, { age: 'requiredIf:address.street,fantastic' });

const matched = await v.check();

assert.equal(matched, false);

});

it('should return true', async () => {
it('with false as string: should return true', async () => {

const v = new Validator({ remember: 'false', age: 16 }, { remember: 'requiredIf:age,16' });

Expand All @@ -45,27 +45,31 @@ describe('requiredIf', function () {

});

it('should return true', async () => {
it('with false as boolean: should return true', async () => {

const v = new Validator({ remember: false, age: 16 }, { remember: 'requiredIf:age,16' });

const matched = await v.check();

console.log(v.errors);

assert.equal(matched, true);

});

it('should return true', async () => {
it('with 0 as int: should return true', async () => {

const v = new Validator({ remember: 0, age: 16 }, { remember: 'requiredIf:age,16' });

const matched = await v.check();

console.log(v.errors);

assert.equal(matched, true);

});

it('should return true', async () => {
it('with true as boolean: should return true', async () => {

const v = new Validator({ remember: true, age: 16 }, { remember: 'requiredIf:age,16' });

Expand All @@ -84,7 +88,7 @@ describe('requiredIf', function () {

assert.equal(matched, false);

assert.equal(v.errors.remember.message, v.parseExistingMessageOnly('requiredIf', 'remember', '',['age', '16']));
assert.equal(v.errors.remember.message, v.parseExistingMessageOnly('requiredIf', 'remember', '', ['age', '16']));

});

Expand Down

0 comments on commit d3a8c3b

Please sign in to comment.