Skip to content

Commit

Permalink
Merge 33d30a1 into d619491
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiBanos committed Feb 22, 2019
2 parents d619491 + 33d30a1 commit e9114c6
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 30 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}
16 changes: 11 additions & 5 deletions src/messages/en/messages.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module.exports = {
accepted: 'The :attribute must be accepted.',
acceptedIf: 'The :attribute should be accepted if the parameter :arg0 is :arg1.',
acceptedNotIf: 'The :attribute can\'t be accepted if the parameter :arg0 is :arg1.',
activeUrl: 'The :attribute is not a valid url.',
after: 'The :attribute must be a date after :agr0.',
afterOrEqual: 'The :attribute must be a date after or equal :agr0.',
after: 'The :attribute must be a date after :arg0.',
afterOrEqual: 'The :attribute must be a date after or equal :arg0.',
alpha: 'The :attribute can only contain alphabets.',
alphaDash: 'The :attribute can only contain letters, numbers, and dashes.',
alphaNum: 'The :attribute can only contain letters and numbers.',
Expand All @@ -17,8 +19,12 @@ module.exports = {
contains: 'The :attribute must contains :arg0.',
creditCard: 'The :attribute value must be a valid card number.',
date: 'The :attribute must be a valid date.',
dateAfter: 'The :attribute must be a date after :agr0.',
dateBefore: 'The :attribute must be a date before :agr0.',
dateAfter: 'The :attribute must be a date after :arg0.',
dateDaysAfterToday : 'The :attribute must be a date after :arg0 days.',
dateYearsAfterToday : 'The :attribute must be a date after :arg0 years.',
dateDaysBeforeToday : 'The :attribute must be a date before :arg0 days.',
dateYearsBeforeToday : 'The :attribute must be a date before :arg0 years.',
dateBefore: 'The :attribute must be a date before :arg0.',
dateFormat: 'The :attribute does not match the date format :arg0.',
decimal: 'The :attribute must be a valid decimal value.',
different: 'The :attribute and :arg0 must be different.',
Expand Down Expand Up @@ -71,4 +77,4 @@ module.exports = {
'custom_attribute.rule': 'Message gies here.',
},
_default: 'The :attribute value is malformed.'
};
};
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;
}
18 changes: 18 additions & 0 deletions src/rules/dateDaysAfterToday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const moment = require('moment'),
{ dateFormats } = require('../lib/date');

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


let mAfterDate, mDate;

mAfterDate = moment('', dateFormats).add(days, 'days');
mDate = moment(value, dateFormats);

/* istanbul ignore next */
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() > mDate.valueOf()) {
return false;
}

return true;
}
18 changes: 18 additions & 0 deletions src/rules/dateDaysBeforeToday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const moment = require('moment'),
{ dateFormats } = require('../lib/date');

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


let mAfterDate, mDate;

mAfterDate = moment('', dateFormats).add(days, 'days');
mDate = moment(value, dateFormats);

/* istanbul ignore next */
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() < mDate.valueOf()) {
return false;
}

return true;
}
18 changes: 18 additions & 0 deletions src/rules/dateYearsAfterToday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const moment = require('moment'),
{ dateFormats } = require('../lib/date');

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


let mAfterDate, mDate;

mAfterDate = moment('', dateFormats).add(days, 'years');
mDate = moment(value, dateFormats);

/* istanbul ignore next */
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() > mDate.valueOf()) {
return false;
}

return true;
}
18 changes: 18 additions & 0 deletions src/rules/dateYearsBeforeToday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const moment = require('moment'),
{ dateFormats } = require('../lib/date');

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


let mAfterDate, mDate;

mAfterDate = moment('', dateFormats).add(days, 'years');
mDate = moment(value, dateFormats);

/* istanbul ignore next */
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() < mDate.valueOf()) {
return false;
}

return true;
}
8 changes: 7 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 All @@ -12,6 +14,10 @@ exports.creditCard = require('./creditCard');
exports.date = require('./date');
exports.dateFormat = require('./dateFormat');
exports.dateAfter = require('./dateAfter');
exports.dateDaysAfterToday = require('./dateDaysAfterToday');
exports.dateYearsAfterToday = require('./dateYearsAfterToday');
exports.dateDaysBeforeToday = require('./dateDaysBeforeToday');
exports.dateYearsBeforeToday = require('./dateYearsBeforeToday');
exports.dateBefore = require('./dateBefore');
exports.decimal = require('./decimal');
exports.digits = require('./digits');
Expand Down Expand Up @@ -52,4 +58,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 () {
});


});
});
Loading

0 comments on commit e9114c6

Please sign in to comment.