Skip to content

Commit

Permalink
Merge pull request #11 from gsmithun4/development
Browse files Browse the repository at this point in the history
Hot Fix
  • Loading branch information
gsmithun4 authored Dec 20, 2021
2 parents 7162bc7 + 44ae30e commit 68a0fd4
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 39 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,16 @@ Ends a validation definition
{
"field1": "Value", // String, Mandatory
"field2": [ // array, Mandatory
{ "field21": "44443" } // Number Optional
{ "field21": "44443" }, // object Optional, number mandatory
{ "field21": "44443" }
],
"field3": { // Object Optional
{ "field31": "true" }, // Boolean Mandatory
{ "field32": "String" } // String Mandatory
}
"field31": "true", // Boolean Mandatory
"field32": "String" // String Mandatory
},
"field4": [ // array, Mandatory
123, 445, 3434 // Number Optional
],
}
```
Should send http status code 500 in case of error
Expand All @@ -315,12 +319,17 @@ router.post('/users/:id',
validateBody().isToBeRejected().sendErrorCode(500).addParams([
param('field1').isRequired().end(),
param('field2').isRequired().isArray().isRequired().addChild(
param('field21').isNumber().end()
param('field2-array').isObject().addChild( // field2-array is for tracking, you can give any name here
param('field21').isNumber().isRequired().end()
).end()
).end(),
param('field3').isRequired().isObject().addChildren([
param('field3').isObject().addChildren([
param('field31').isBoolean().isRequired().end(),
param('field32').isRequired().end()
]).end(),
param('field4').isRequired().isArray().isRequired().addChild(
param('field4-array').isNumber().end()
).end(),
]).done(),
(req, res, next) => {

Expand Down
24 changes: 11 additions & 13 deletions lib/validator/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = (location = 'body', validation = [], response = {}) => {
return false;
};
const checkFormat = (value, validateObj) => {
const { param, location, isRequired, isNumber, isEmail, isBoolean,
const { param, isRequired, isNumber, isEmail, isBoolean,
length, message, isDate, format, isArray, isObject, range,
includes, excludes, mobileNumber } = validateObj;

Expand Down Expand Up @@ -215,21 +215,19 @@ module.exports = (location = 'body', validation = [], response = {}) => {
});
};
const getChildArrayErrors = (child, value) => {
const arrayType = child[0];
return value.map(v => {
return child.map(c => {
const subvalue = getValue(v, c.param);
const err = checkFormat(subvalue, c);
if (err)
return [err];
const err = checkFormat(v, arrayType);
if (err)
return [err];

if (subvalue && c.children) {
if (c.isArray) {
return getChildArrayErrors(c.children, subvalue);
} else if (c.isObject) {
return getChildErrors(c.children, subvalue);
}
if (v && arrayType.children) {
if (arrayType.isArray) {
return getChildArrayErrors(arrayType.children, v);
} else if (arrayType.isObject) {
return getChildErrors(arrayType.children, v);
}
});
}
});
};
const value = getValue();
Expand Down
150 changes: 131 additions & 19 deletions lib/validator/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('Test for body params', () => {
expect(req.locals.data).toEqual({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Mandatory field sorted missing',
param: 'sorted',
}
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -434,7 +434,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -542,7 +542,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -711,7 +711,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -742,7 +742,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -773,7 +773,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -850,7 +850,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -881,7 +881,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -912,7 +912,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -943,7 +943,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -997,7 +997,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -1051,7 +1051,7 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
Expand Down Expand Up @@ -1105,11 +1105,123 @@ describe('Test for body params', () => {
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body.page',
location: 'body',
message: 'Invalid Field Error',
param: 'sorted',
}
]
});
});
test('Test nested objects Failure case - Array test', () => {
const req = {
body: {
page: [
1, 2, 3, 'sdsd'
]
}
};
const validation = [
{param : 'page', location : 'body', isArray : true, children : [
{param: 'page-array', isRequired : true, isNumber: true },
]}
];
const response = {
mode: 'reject'
};
const validatorfn = validaor('body', validation, response);
validatorfn(req, resp, next);
expect(next).toHaveBeenCalledTimes(0);
expect(resp.status).toHaveBeenCalledTimes(1);
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body',
message: 'Invalid Field Error',
param: 'page-array',
}
]
});
});
test('Test nested objects Success case - Array test', () => {
const req = {
body: {
page: [
1, 2, 3
]
}
};
const validation = [
{param : 'page', location : 'body', isArray : true, children : [
{param: 'page-array', isRequired : true, isNumber: true },
]}
];
const response = {
mode: 'reject'
};
const validatorfn = validaor('body', validation, response);
validatorfn(req, resp, next);
expect(next).toHaveBeenCalledTimes(1);
expect(resp.status).toHaveBeenCalledTimes(0);
});
test('Test nested objects Failure case - Array test 2', () => {
const req = {
body: {
page: [
{test: 1, test2: 2},
{test: 1, test2: 2},
{test: 1, test2: 'test'}
]
}
};
const validation = [
{param : 'page', location : 'body', isArray : true, children : [
{param: 'page-array', isRequired : true, isObject: true, children: [
{param: 'test', isNumber: true},
{param: 'test2', isNumber: true},
]},
]}
];
const response = {
mode: 'reject'
};
const validatorfn = validaor('body', validation, response);
validatorfn(req, resp, next);
expect(next).toHaveBeenCalledTimes(0);
expect(resp.status).toHaveBeenCalledTimes(1);
expect(resp.send).toHaveBeenCalledWith({
error: [
{
location: 'body',
message: 'Invalid Field Error',
param: 'test2',
}
]
});
});
test('Test nested objects Success case - Array test 2', () => {
const req = {
body: {
page: [
{test: 1, test2: 2},
{test: 1, test2: 2},
{test: 1, test2: 323}
]
}
};
const validation = [
{param : 'page', location : 'body', isArray : true, children : [
{param: 'page-array', isRequired : true, isObject: true, children: [
{param: 'test', isNumber: true},
{param: 'test2', isNumber: true},
]},
]}
];
const response = {
mode: 'reject'
};
const validatorfn = validaor('body', validation, response);
validatorfn(req, resp, next);
expect(next).toHaveBeenCalledTimes(1);
expect(resp.status).toHaveBeenCalledTimes(0);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expressjs-field-validator",
"version": "3.0.0",
"version": "3.0.2",
"description": "Plugin for validating field values of json request in expressjs",
"homepage": "https://gsmithun4.github.io/expressjs-field-validator",
"repository": {
Expand Down

0 comments on commit 68a0fd4

Please sign in to comment.