Skip to content

Commit

Permalink
Merge pull request #12 from oesukam/ft-array-validation-164489344
Browse files Browse the repository at this point in the history
#164489344 Add array's validation
  • Loading branch information
oesukam committed Mar 8, 2019
2 parents b81e4fb + 96ded5e commit db3e687
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 9 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

---

---

[![Build Status](https://travis-ci.org/oesukam/cheke.svg?branch=master)](https://travis-ci.org/oesukam/cheke)
[![Coverage Status](https://coveralls.io/repos/github/oesukam/cheke/badge.svg?branch=master)](https://coveralls.io/github/oesukam/cheke?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/f0f25d4e5bc5182f32a5/maintainability)](https://codeclimate.com/github/oesukam/cheke/maintainability)
Expand Down
8 changes: 8 additions & 0 deletions __tests__/hasErrors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ describe('hasErrors', () => {
expect(res.age.message).toBe('The age must be a number.');
}));

test('should return The age must be a number', () =>
hasErrors({
data: { age: '21' },
reqRules: { page: 'number' },
}).then(res => {
expect(res.age.message).toBe('age is not allowed.');
}));

test('should return The age must be a number', () =>
hasErrors({
data: { age: 21 },
Expand Down
7 changes: 7 additions & 0 deletions __tests__/messages/notAllowedMessage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const notAllowed = require('../../src/messages/notAllowedMessage');

describe('not-an-array-message', () => {
test('should return `users is not allowed.`', () => {
expect(notAllowed('users')).toBe('users is not allowed.');
});
});
7 changes: 7 additions & 0 deletions __tests__/messages/notArrayMessage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const notArray = require('../../src/messages/notArrayMessage');

describe('not-an-array-message', () => {
test('should return `users must be an array.`', () => {
expect(notArray('users')).toBe('users must be an array.');
});
});
28 changes: 28 additions & 0 deletions __tests__/validators/array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const array = require('../../src/validators/array');

describe('integer-validator', () => {
test('should return `undefined must be an array.`', () => {
expect(array()).toBe('undefined must be an array.');
});

test('should return `users must be an array.`', () => {
expect(array({
value: 'not an array',
label: 'users',
}),).toBe('users must be an array.');
});

test('should return `users must be an array.`', () => {
expect(array({
value: {},
label: 'users',
}),).toBe('users must be an array.');
});

test('should return false', () => {
expect(array({
value: [],
label: 'users',
}),).toBe(false);
});
});
16 changes: 16 additions & 0 deletions __tests__/validators/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ describe('number-validator', () => {
}),).toBe('The age must be a number.');
});

test('should return false', () => {
expect(number({
value: '21p',
label: 'age',
path: 'query',
}),).toBe('The age must be a number.');
});

test('should return false', () => {
expect(number({
value: '21',
label: 'age',
path: 'params',
}),).toBe(false);
});

test('should return false', () => {
expect(number({
value: 21,
Expand Down
2 changes: 1 addition & 1 deletion lib/hasErrors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/messages/notAllowedMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports=a=>`${a} is not allowed.`;
1 change: 1 addition & 0 deletions lib/messages/notArrayMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports=a=>`${a} must be an array.`;
1 change: 1 addition & 0 deletions lib/validators/array.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/hasErrors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const validators = require('./validators');
const notAllowedMessage = require('./messages/notAllowedMessage');

const valids = Object.keys(validators);

const hasErrors = ({ data = {}, reqRules, path } = {}) =>
new Promise((resolve, reject) => {
if (!reqRules) resolve(false);
Object.keys(reqRules).forEach(key => {
if (!reqRules[key]) reject(Error(`${key}'s rule can not be empty`));
const dataRules = { ...data, ...reqRules };
Object.keys(dataRules).forEach(key => {
if (reqRules[key] === undefined) {
resolve({
[key]: {
path,
message: notAllowedMessage(key),
},
});
}
if (!reqRules[key] && reqRules[key] !== undefined) {
reject(Error(`${key}'s rule can not be empty`));
}
const rules = reqRules[key].split('|').map(rule => rule.trim());
Object.keys(rules).forEach(k => {
const [rule, valid] = rules[k].split(':');
Expand All @@ -20,7 +32,6 @@ const hasErrors = ({ data = {}, reqRules, path } = {}) =>
message: validators.required({
value: data[key],
label: key,
valid,
}),
},
});
Expand Down
1 change: 1 addition & 0 deletions src/messages/notAllowedMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = label => `${label} is not allowed.`;
1 change: 1 addition & 0 deletions src/messages/notArrayMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = label => `${label} must be an array.`;
6 changes: 6 additions & 0 deletions src/validators/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const notArrayMessage = require('../messages/notArrayMessage');

module.exports = ({ value, label } = {}) => {
if (value instanceof Array) return false;
return notArrayMessage(label);
};
8 changes: 6 additions & 2 deletions src/validators/number.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const numberMessage = require('../messages/notNumberMessage');

module.exports = ({ value, label } = {}) =>
typeof value === 'number' ? false : numberMessage(label);
module.exports = ({ value, label, path } = {}) => {
if (path === 'query' || path === 'params') {
return !Number.isNaN(Number(value)) ? false : numberMessage(label);
}
return typeof value === 'number' ? false : numberMessage(label);
};

0 comments on commit db3e687

Please sign in to comment.