Skip to content

Commit

Permalink
coverage improved, bug fixex
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnbytesio committed Jun 5, 2019
1 parent 6f096fd commit 3b97f0b
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/rules/acceptedNotIf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const empty = require('../lib/empty');
const { pathIndex } = require('../lib/ObjectIndex');

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

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

Expand Down
8 changes: 4 additions & 4 deletions src/rules/between.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ const numeric = require('./numeric'),
module.exports = async function between(attribute, value, args) {

if (!Array.isArray(args) && args.length !== 2) {
throw new Error('The number of arguments for between in the field ' + attribute + ' are invalid.');
throw 'The number of arguments for between in the field ' + attribute + ' are invalid.';
}

let [min, max] = args;

if (!numeric(min) || !numeric(max)) {
throw new Error('Seeds must be integer for between rule.');
if (!(await numeric(attribute, min)) || !(await numeric(attribute, max))) {
throw 'Seeds must be integer for ' + attribute + ' under between rule.';
}

min = parseFloat(min);
max = parseFloat(max);

if (min >= max) {

throw new Error('Seed min must be less then max in between.');
throw 'Seed min must be less then max in between rule for ' + attribute + '.';
}


Expand Down
4 changes: 2 additions & 2 deletions src/rules/digits.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const v = require('validator'),

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

if (!numeric(field)) {
throw `Please provide a numeric value for ${field} under digits rule`;
if (!(await numeric(field, dNumber))) {
throw `Please provide a numeric value for ${field} under digits rule.`;
}

if (dNumber != value.length) {
Expand Down
6 changes: 3 additions & 3 deletions src/rules/digitsBetween.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ module.exports = async function digitsBetween(field, value, args) {

if (!Array.isArray(args) && args.length !== 2) {

throw new Error('The number of arguments for digits between in the field ' + field + ' are invalid.');
throw 'The number of arguments for digits between rule in the field ' + field + ' are invalid.';
}

let [min, max] = args;

if (!(await integer(field, min)) || !(await integer(field, max))) {
throw new Error('Seeds must be integer for digits between rule.');
throw 'Seeds must be integer for ' + field + ' under digits between rule.';
}


Expand All @@ -25,7 +25,7 @@ module.exports = async function digitsBetween(field, value, args) {

if (min >= max) {

throw new Error('Seed min must be less then max in digits between.');
throw 'Seed min must be less then max in digits between rule for ' + field + '.';
}

if (value.length < min || value.length > max) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const numeric = require('./numeric');

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

if (!numeric(field, maxNum)) {
return false;
if (!(await numeric(field, maxNum))) {
throw 'Seed in max rule for ' + field + ' must be a number.';
}

if (Number(value) > Number(maxNum)) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/maxLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const v = require('validator');
module.exports = async function maxLength(field, value, maxNum) {

if (!v.isInt(maxNum)) {
return false;
throw 'Seed in maxLength rule for ' + field + ' must be a number.';;
}

if (value.toString().length > parseInt(maxNum)) {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/min.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const numeric = require('./numeric');

module.exports = async function min(field, value, minNum) {
if (!numeric(minNum)) {
throw new Error('Seed min is invalid.');
// throw minNum;
if (!(await numeric(field, minNum))) {
throw 'Seed in min rule for ' + field + ' must be a number.';
}

if (Number(value) < Number(minNum)) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/minLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const v = require('validator');

module.exports = async function minLength(field, value, minNum) {
if (!v.isInt(minNum)) {
return false;
throw 'Seed in minLength rule for ' + field + ' must be a number.';
}

if (value.toString().length < parseInt(minNum)) {
Expand Down
4 changes: 4 additions & 0 deletions test/arrayOfRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('Multiple rules test', function () {
it('should return true', async () => {

//{ name: 'required|minLength:5|maxLength:8|alpha' }
// @ts-ignore
let vi = Validator.make(
{ name: 'artisan' }
);

let v = Validator.make(
{ name: 'artisan' },
Expand Down
171 changes: 162 additions & 9 deletions test/crashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,63 @@ describe('crash', function () {

});


describe('acceptedIf exception', () => {
it('acceptedIf: Checking for invalid arguments', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|acceptedIf' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field attribute in acceptedIf rule.');
}

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|acceptedIf:1,2,3' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field attribute in acceptedIf rule.');
}

});
});

describe('acceptedNotIf exception', () => {
it('acceptedNotIf: Checking for invalid arguments', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|acceptedNotIf' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field attribute in acceptedNotIf rule.');
}

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|acceptedNotIf:1,2,3' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field attribute in acceptedNotIf rule.');
}

});
});

describe('between exception', () => {
it('Between: Checking for invalid seed count', async () => {

Expand All @@ -50,7 +107,7 @@ describe('between exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: The number of arguments for between in the field attribute are invalid.');
assert.equal(e, 'The number of arguments for between in the field attribute are invalid.');
}

});
Expand All @@ -66,7 +123,7 @@ describe('between exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seeds must be integer for between rule.');
assert.equal(e, 'Seeds must be integer for attribute under between rule.');
}

});
Expand All @@ -81,7 +138,7 @@ describe('between exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seeds must be integer for between rule.');
assert.equal(e, 'Seeds must be integer for attribute under between rule.');
}

});
Expand All @@ -97,7 +154,7 @@ describe('between exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seed min must be less then max in between.');
assert.equal(e, 'Seed min must be less then max in between rule for attribute.');
}

});
Expand Down Expand Up @@ -192,7 +249,7 @@ describe('digitsBetween exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: The number of arguments for digits between in the field attribute are invalid.');
assert.equal(e, 'The number of arguments for digits between rule in the field attribute are invalid.');
}

});
Expand All @@ -208,7 +265,7 @@ describe('digitsBetween exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seeds must be integer for digits between rule.');
assert.equal(e, 'Seeds must be integer for attribute under digits between rule.');
}

});
Expand All @@ -223,7 +280,7 @@ describe('digitsBetween exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seeds must be integer for digits between rule.');
assert.equal(e, 'Seeds must be integer for digits between rule.');
}

});
Expand All @@ -239,8 +296,104 @@ describe('digitsBetween exception', () => {

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Error: Seed min must be less then max in digits between.');
assert.equal(e, 'Seed min must be less then max in digits between rule for attribute.');
}

});
});



describe('max exception', () => {
it('max: Checking for invalid seed', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|max:test' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Seed in max rule for attribute must be a number.');
}


});
});


describe('min exception', () => {
it('min: Checking for invalid seed', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|min:test' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Seed in min rule for attribute must be a number.');
}


});
});



describe('maxLength exception', () => {
it('maxLength: Checking for invalid seed', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|maxLength:test' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Seed in maxLength rule for attribute must be a number.');
}


});
})
});


describe('minLength exception', () => {
it('minLength: Checking for invalid seed', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|minLength:test' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Seed in minLength rule for attribute must be a number.');
}


});
});

describe('digits exception', () => {
it('digits: Checking for invalid seed', async () => {

try {

let v = new Validator({ attribute: "Harcharan Singh" }, { attribute: 'required|digits:test' });

await v.check();

// assert.equal(matched, true);
} catch (e) {
assert.equal(e, 'Please provide a numeric value for attribute under digits rule.');
}


});
});
2 changes: 1 addition & 1 deletion test/rules/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('max', function () {
assert.equal(matched, true);

});


it('validation should fail: invalida value', async () => {

Expand Down

0 comments on commit 3b97f0b

Please sign in to comment.