Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
hector-js committed Apr 21, 2023
2 parents 9fac687 + 2f7114e commit cad6835
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,22 @@ _bodyPaths can be an array also as an option to validate a field or value.
]
```

Also, you can include a regex to check different values. Example:
```json
"/customers/{id}/data" : [
{
"_req": {
"_bodyPaths": {
"$.id": "^[v].*[3]$"
}
},
"_res":{
}
}
]
```
Above expresion will check id which starts with v and ends with 3


_NOTE:_ bodyPaths and body section can not be at the same time.

Expand Down
8 changes: 6 additions & 2 deletions lib/app/utils/path-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = class PathUtils {
}

/**
* Check valid path
* Check valid path and include regex
* @param {object} jsonA body
* @param {object} pathsObjs
* @return {boolean} object is equal.
Expand All @@ -49,7 +49,11 @@ module.exports = class PathUtils {
let valid = true;
Object.keys(pathsObjs).forEach((key) => {
const jsonAVal = jp.value(jsonA, key);
valid = valid && equal(jsonAVal, pathsObjs[key]);
const jsonPathOp = pathsObjs[key];
valid =
valid && isObject(jsonPathOp)?
equal(jsonAVal, jsonPathOp):
new RegExp(jsonPathOp).test(jsonAVal);
});
return valid;
}
Expand Down
12 changes: 6 additions & 6 deletions 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
Expand Up @@ -7,7 +7,7 @@
"test": "test"
},
"dependencies": {
"@hectorjs/stub-cli": "^1.40.0",
"@hectorjs/stub-cli": "^1.41.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"deep-equal": "^1.1.1",
Expand Down
14 changes: 14 additions & 0 deletions test/unit/utils/path-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ describe('bodyPathUtils', () => {
});
});
});

describe('regex', ()=>{
context('the value is a regex and it matched', ()=>{
it('return true', ()=>{
jsonA = { var1: 'var-1', var2: { var3: 'var-3', var4: { var5: 'var-5' } } };
paths = { '$.var2.var3': '^[v].*[3]$' };
jsonExcludePaths = null;

result = PathUtils.comparePathBody(jsonA, paths, jsonExcludePaths);

expect(result).to.be.true;
});
});
});
});
});
});
Expand Down

0 comments on commit cad6835

Please sign in to comment.