Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 44e2b40

Browse files
author
Bassem Dghaidi
authored
Merge pull request #13 from aymanfarhat/develop
Documentation + test on using closures as wildcard
2 parents 62a54d0 + 47a0c1b commit 44e2b40

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,61 @@ console.dir(result, {depth: null});
273273
}
274274
```
275275

276+
### 7. Using closures as wildcard (Success)
277+
278+
You can utilize closures to return an entire sub-object or sub-array from specific keys in a payload. This can be useful in cases where you want to return all values under a key without the need for further matching.
279+
280+
```javascript
281+
const match = require('@menadevs/objectron');
282+
283+
const payload = {
284+
request: {
285+
status: 200,
286+
headers: [
287+
{
288+
"name": "User-Agent",
289+
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
290+
},
291+
{
292+
"name": "Referer",
293+
"value": "https://www.somethingabcxyz.kfc/"
294+
}
295+
]
296+
}
297+
}
298+
299+
const tester = {
300+
request: {
301+
status: 200,
302+
headers: (val) => val
303+
}
304+
};
305+
306+
const result = match(payload, tester);
307+
308+
# Output
309+
> {
310+
match: true,
311+
total: 2,
312+
matches: {
313+
request: {
314+
status: 200,
315+
headers: [
316+
{
317+
"name": "User-Agent",
318+
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
319+
},
320+
{
321+
"name": "Referer",
322+
"value": "https://www.somethingabcxyz.kfc/"
323+
}
324+
]
325+
}
326+
},
327+
groups: {}
328+
}
329+
```
330+
276331
## FAQs
277332

278333
### 1. What's the difference between Objectron and any other Schema Validator?

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_objectron.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,55 @@ suite('Objectron Core Tests', () => {
441441
assert.isTrue(called)
442442
})
443443

444+
test('Callback used as wildcard', () => {
445+
const payload = {
446+
request: {
447+
status: 200,
448+
headers: [
449+
{
450+
"name": "User-Agent",
451+
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
452+
},
453+
{
454+
"name": "Referer",
455+
"value": "https://www.somethingabcxyz.kfc/"
456+
}
457+
]
458+
}
459+
}
460+
461+
const result = match(payload, {
462+
request: {
463+
status: 200,
464+
headers: (val) => val
465+
}
466+
});
467+
468+
const expected = {
469+
match: true,
470+
total: 2,
471+
matches: {
472+
request: {
473+
status: 200,
474+
headers: [
475+
{
476+
"name": "User-Agent",
477+
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
478+
},
479+
{
480+
"name": "Referer",
481+
"value": "https://www.somethingabcxyz.kfc/"
482+
}
483+
]
484+
}
485+
},
486+
groups: {}
487+
}
488+
489+
assert.isTrue(result.match)
490+
assert.deepEqual(result, expected)
491+
})
492+
444493
test('Variation of all the tests above', () => {
445494
const payload = {
446495
api: 13,

0 commit comments

Comments
 (0)