Skip to content

Commit

Permalink
coverage improved and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnbytesio committed Jul 11, 2019
1 parent 70999b8 commit 6422288
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- arrayUniqueObjects: array of objects must have unique attribute as per seed
- length: length rule with max and min (optional) seed

### Fixed

- requiredwith, requiredWithout throw exception in case of invalid seed

## [3.4.2]

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/rules/requiredIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module.exports = async function requiredIf(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 required = false;
Expand Down
8 changes: 4 additions & 4 deletions src/rules/requiredWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const {pathIndex} = require('../lib/ObjectIndex');

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

if (!Array.isArray(args)) args = [args];

if (!args.length) {
throw new Error('Invalid arguments supplied for field ' + field + ' in required with rule.');
if (!args) {
throw 'Invalid arguments supplied for field ' + field + ' in required with rule.';
}

if (!Array.isArray(args)) args = [args];

let i, required = false;

for (i = 0; i < args.length; ++i) {
Expand Down
8 changes: 4 additions & 4 deletions src/rules/requiredWithout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const {pathIndex} = require('../lib/ObjectIndex');

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

if (!Array.isArray(args)) args = [args];

if (!args.length) {
throw new Error('Invalid arguments supplied for field ' + field + ' in required with rule.');
if (!args) {
throw 'Invalid arguments supplied for field ' + field + ' in requiredWithout rule.';
}

if (!Array.isArray(args)) args = [args];

let i, required = false;

for (i = 0; i < args.length; ++i) {
Expand Down
14 changes: 14 additions & 0 deletions test/rules/arrayUnique.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ const Validator = require('../../index');

describe('arrayUnique', function () {

it('validation should fail with non array', async () => {

const v = new Validator(
{ features: 'test' },
{ features: 'arrayUnique' }
);

const matched = await v.check();

assert.equal(matched, false);

});


it('validation should pass: []', async () => {

const v = new Validator(
Expand Down
15 changes: 14 additions & 1 deletion test/rules/arrayUniqueObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ const Validator = require('../../index');

describe('arrayUniqueObjects', function () {

it('validation should fail with non array', async () => {

const v = new Validator(
{ features: 'test' },
{ features: 'arrayUniqueObjects:id' }
);

const matched = await v.check();

assert.equal(matched, false);

});


it('validation should pass: with single attribute', async () => {

Expand Down Expand Up @@ -50,7 +63,7 @@ describe('arrayUniqueObjects', function () {

});

it('validation should fail for duplicates', async () => {
it('validation should fail for duplicates', async () => {


const v = new Validator(
Expand Down
20 changes: 19 additions & 1 deletion test/rules/digitsBetween.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ let r = {};

describe('digitsBetween', function () {


it('validation should throw error of invalid seed', async () => {

try {
const v = new Validator(
{ attribute: '1250' },
{ attribute: 'digitsBetween:4,6' }
);

const matched = await v.check();
} catch (e) {
assert.equal(e, 'Seeds must be integer for attribute under digits between rule.');
}



});

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

const v = new Validator(
Expand Down Expand Up @@ -77,7 +95,7 @@ describe('digitsBetween', function () {

assert.equal(matched, false);

assert.equal(v.errors.attribute.message, v.parseExistingMessageOnly('digitsBetween', 'attribute', '',[2,3]));
assert.equal(v.errors.attribute.message, v.parseExistingMessageOnly('digitsBetween', 'attribute', '', [2, 3]));

});

Expand Down
28 changes: 28 additions & 0 deletions test/rules/requiredIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ describe('requiredIf', function () {

});

it('should return false for invalid seed length', async () => {

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

const matched = await v.check();
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field remember in requiredIf rule.');
}

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

});

it('should return false for missing seed length', async () => {

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

const matched = await v.check();
} catch (e) {
assert.equal(e, 'Error: Invalid arguments supplied for field remember in requiredIf rule.');
}

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

});

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

const v = new Validator({ age: 16 }, { remember: 'requiredIf:age,16' });
Expand Down
14 changes: 14 additions & 0 deletions test/rules/requiredWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ const Validator = require('../../index');

describe('#requiredWith', function () {

it('should return false for missing seed length', async () => {

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

const matched = await v.check();
} catch (e) {
assert.equal(e, 'Invalid arguments supplied for field remember in required with rule.');
}

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

});

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

let v, matched;
Expand Down
15 changes: 15 additions & 0 deletions test/rules/requiredWithout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ const Validator = require('../../index');

describe('requiredWithout', function () {


it('should return false for missing seed length', async () => {

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

const matched = await v.check();
} catch (e) {
assert.equal(e, 'Invalid arguments supplied for field remember in requiredWithout rule.');
}

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

});

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


Expand Down

0 comments on commit 6422288

Please sign in to comment.