Skip to content

Commit

Permalink
Merge 4dd7f15 into d619491
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiBanos committed Feb 22, 2019
2 parents d619491 + 4dd7f15 commit 581e5ac
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 25 deletions.
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-input-validator",
"version": "3.1.0",
"version": "3.2.0",
"description": "validation library for nodejs, inspired by laravel.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,21 +28,12 @@
"image",
"mimes"
],
"repository": "artisangang/node-input-validator",
"repository": "SergiBanos/node-input-validator",
"author": {
"name": "Harcharan Singh",
"name": "Sergi Baños",
"email": "artisangang@gmail.com"
},
"contributors": [
{
"name": "Jakub Nietrzeba",
"url": "https://github.com/gluth"
},
{
"name": "Nitin Mehra",
"url": "https://github.com/cybersultan"
}
],

"license": "ISC",
"dependencies": {
"file-type": "^10.7.0",
Expand All @@ -63,4 +54,4 @@
"ncp": "^2.0.0",
"should": "^13.2.3"
}
}
}
17 changes: 17 additions & 0 deletions src/lib/ObjectIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* return the value of the object for the given index
* @param {any} object
* @param {Number} index
* @returns {boolean}
*/

// obj,'1.2.3' -> multiIndex(obj,['1','2','3'])
function pathIndex(obj,is) {
return multiIndex(obj,is.split('.'));
}

// obj,['1','2','3'] -> ((obj['1'])['2'])['3']
function multiIndex(obj,is){
return is.length ? multiIndex(obj[is[0]],is.slice(1)) : obj
}
module.exports = {pathIndex,multiIndex}
42 changes: 42 additions & 0 deletions src/rules/acceptedIf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');

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

if (!args || args.length < 2) {

throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.');
return false;
}

if (args.length % 2 !== 0) {

throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.');
return false;
}
let acceptedValues = [true, 'true', 1, '1', 'yes', 'on'];

let canbetrue = false;
for (let start = 0; start < args.length; start += 2) {
let requiredField = args[start];
let requiredValue = args[start + 1];

if (requiredField == field) {
return false;
}

// field can be true if all values are presented
if (!empty(pathIndex(this.inputs,requiredField))
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) {
canbetrue = true;
} else {
canbetrue = false;
break
}
}
if (canbetrue && !(acceptedValues.indexOf(value)>= 0)) {
return false;
}

return true;
}
42 changes: 42 additions & 0 deletions src/rules/acceptedNotIf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');

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

if (!args || args.length < 2) {

throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.');
return false;
}

if (args.length % 2 !== 0) {

throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.');
return false;
}
let acceptedValues = [true, 'true', 1, '1', 'yes', 'on'];

let canbetrue = false;
for (let start = 0; start < args.length; start += 2) {
let requiredField = args[start];
let requiredValue = args[start + 1];

if (requiredField == field) {
return false;
}

// field can be true if all values are presented
if (!empty(pathIndex(this.inputs,requiredField))
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) {
canbetrue = true;
} else {
canbetrue = false;
break
}
}
if (canbetrue && acceptedValues.indexOf(value)>= 0) {
return false;
}

return true;
}
4 changes: 3 additions & 1 deletion src/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports.accepted = require('./accepted');
exports.acceptedIf = require('./acceptedIf');
exports.acceptedNotIf = require('./acceptedNotIf');
exports.alpha = require('./alpha');
exports.alphaDash = require('./alphaDash');
exports.alphaNumeric = require('./alphaNumeric');
Expand Down Expand Up @@ -52,4 +54,4 @@ exports.same = require('./same');
exports.size = require('./size');
exports.sometimes = require('./sometimes');
exports.string = require('./string');
exports.url = require('./url');
exports.url = require('./url');
7 changes: 4 additions & 3 deletions src/rules/requiredIf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');

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

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

// field is required if all values are presented
if (!empty(this.inputs[requiredField])
&& this.inputs[requiredField].toString() == requiredValue) {
if (!empty(pathIndex(this.inputs,requiredField))
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) {
required = true;
} else {
required = false;
Expand All @@ -38,4 +39,4 @@ module.exports = async function requiredIf(field, value, args) {
}

return true;
}
}
5 changes: 3 additions & 2 deletions src/rules/requiredWith.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');

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

Expand All @@ -16,7 +17,7 @@ module.exports = async function requiredWith(field, value, args) {
continue;
}

if (!empty(this.inputs[args[i]])) {
if (!empty(pathIndex(this.inputs,args[i]))) {
required = true;
break;
}
Expand All @@ -28,4 +29,4 @@ module.exports = async function requiredWith(field, value, args) {
}

return true;
}
}
5 changes: 3 additions & 2 deletions src/rules/requiredWithout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const empty = require('../lib/empty');
const {pathIndex} = require('../lib/ObjectIndex');

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

Expand All @@ -16,7 +17,7 @@ module.exports = async function requiredWithout(field, value, args) {
continue;
}

if (empty(this.inputs[args[i]])) {
if (empty(pathIndex(this.inputs,args[i]))) {
required = true;
break;
}
Expand All @@ -29,4 +30,4 @@ module.exports = async function requiredWithout(field, value, args) {

return true;

}
}
21 changes: 20 additions & 1 deletion test/rules/requiredIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ describe('requiredIf', function () {

});

it('should return true', async () => {

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 () => {

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 () => {

const v = new Validator({ remember: 'false', age: 16 }, { remember: 'requiredIf:age,16' });
Expand Down Expand Up @@ -131,4 +150,4 @@ describe('requiredIf', function () {
});


});
});
25 changes: 24 additions & 1 deletion test/rules/requiredNotIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ describe('#requiredNotIf', function () {

assert.equal(matched, true);

});
it('validation should pass', async () => {

let v, matched;

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

matched = await v.check();

assert.equal(matched, true);

});

it('validation should fail', async () => {
Expand All @@ -28,6 +41,16 @@ describe('#requiredNotIf', function () {

assert.equal(matched, false);
});
it('validation should fail', async () => {

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

const matched = await v.check();

assert.equal(matched, false);
});

// should(v.errors).be.an.instanceOf(Object);
// should(v.errors).have.property('sex');
Expand Down Expand Up @@ -76,4 +99,4 @@ describe('#requiredNotIf', function () {

});

});
});
25 changes: 24 additions & 1 deletion test/rules/requiredWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ describe('#requiredWith', function () {

assert.equal(matched, true);
});
it('should pass', async () => {

let v, matched;

// validate with single seed
v = new Validator(
{ name: 'Harcharan Singh', sex: 'male', address:{street:"fantastic"} , ip: '' },
{ email: 'email', sex: 'requiredWith:address.street' });

matched = await v.check();

assert.equal(matched, true);
});

it('should fail', async () => {

Expand All @@ -29,7 +42,17 @@ describe('#requiredWith', function () {

assert.equal(matched, false);
});
it('should fail', async () => {

// validate with multiple seeds
const v = new Validator(
{ name: 'Harcharan Singh', address:{street:"fantastic"} , email: '', ip: '' },
{ email: 'requiredWith:name,address.street' });

const matched = await v.check();

assert.equal(matched, false);
});

it('should pass', async () => {
// validate with multiple seeds
Expand Down Expand Up @@ -61,4 +84,4 @@ describe('#requiredWith', function () {



});
});

0 comments on commit 581e5ac

Please sign in to comment.