Skip to content

Commit

Permalink
Merge to main (#24)
Browse files Browse the repository at this point in the history
* added test cases

* remove coverage from npm

* bump version

* test case fix
  • Loading branch information
gsmithun4 authored Aug 1, 2023
1 parent dc8b360 commit 779c9dc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.git
/node_modules
/example
/coverage
/.github
/lib/validator/validator.js
/lib/validator/validator.test.js
Expand Down
44 changes: 44 additions & 0 deletions lib/queryBuilder/queryBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,50 @@ describe('Test for validateBody', () => {
}], {}
);
});
test('validateBody validator object isArray and object child nested - 1', () => {
validateBody().addParams([
param('test').isArray().addChildren([
param('child1').isObject().addChildren([
param('param1').isRequired().end(),
param('param2').isNumber().end()
]).end()
]).end()
]).done();
expect(validator).toBeCalledWith('body',
[{
param: 'test',
isArray: true,
children: [
{ param: 'child1', isObject: true, children: [
{ param: 'param1', isRequired: true},
{ param: 'param2', isNumber: true},
]}
]
}], {}
);
});
test('validateBody validator object isArray and object child nested - 2', () => {
validateBody().addParams([
param('test').isArray().addChild(
param('child1').isObject().addChildren([
param('param1').isRequired().end(),
param('param2').isNumber().end()
]).end()
).end()
]).done();
expect(validator).toBeCalledWith('body',
[{
param: 'test',
isArray: true,
children: [
{ param: 'child1', isObject: true, children: [
{ param: 'param1', isRequired: true},
{ param: 'param2', isNumber: true},
]}
]
}], {}
);
});
test('validateBody validator object shouldInclude', () => {
validateBody().addParams([
param('test').shouldInclude(['a','b']).end()
Expand Down
34 changes: 34 additions & 0 deletions lib/validator/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,40 @@ describe('Test for body params', () => {
]
});
});
test('Test nested objects - object in array required check', () => {
const req = {
body: {
page1: [
{test: 1, test2: 2},
{test: 1},
]
}
};
const validation = [
{param : 'page1', location : 'body', isArray : true, children : [
{param: 'page1-array', isRequired : true, isObject: true, children: [
{param: 'test', isNumber: true},
{param: 'test2', isNumber: true, isRequired: 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 Arrays Failure case - Array of arrays 1', () => {
const req = {
body: {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

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.5",
"version": "3.0.6",
"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 779c9dc

Please sign in to comment.