Skip to content

Commit 5a149eb

Browse files
committed
feat: adds pruneProperties option for deep cleanup
1 parent 41c96bf commit 5a149eb

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ jsf.locate('faker');
107107

108108
- `defaultInvalidTypeProduct` — If `failOnInvalidTypes` is disabled this value will be returned on any invalid `type` given (default: `null`)
109109
- `defaultRandExpMax` — Setup default value directly as `RandExp.prototype.max` (default: `10`)
110+
- `pruneProperties` — Remove given properties from generated objects (default: `[]`)
110111
- `ignoreProperties` — Skip given properties from being generated (default: `[]`)
111112
- `ignoreMissingRefs` — If enabled, it will resolve to `{}` for unknown references (default: `false`)
112113
- `failOnInvalidTypes` — If enabled, it will throw an `Error` for unknown types (default: `true`)

src/lib/api/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defaults;
55
defaults.defaultInvalidTypeProduct = undefined;
66
defaults.defaultRandExpMax = 10;
77

8+
defaults.pruneProperties = [];
89
defaults.ignoreProperties = [];
910
defaults.ignoreMissingRefs = false;
1011
defaults.failOnInvalidTypes = true;

src/lib/core/traverse.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ function traverse(schema, path, resolve, rootSchema) {
164164
valueCopy = [];
165165
}
166166

167+
const pruneProperties = optionAPI('pruneProperties') || [];
168+
167169
Object.keys(schema).forEach(prop => {
170+
if (pruneProperties.includes(prop)) return;
168171
if (typeof schema[prop] === 'object' && prop !== 'definitions') {
169172
const { value, context: innerContext } = traverse(schema[prop], path.concat([prop]), resolve, valueCopy);
170173
valueCopy[prop] = utils.clean(value, schema[prop], false);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"description": "pruneProperties option",
4+
"tests": [
5+
{
6+
"description": "should remove nested keys from pruneProperties",
7+
"schema": {
8+
"properties": {
9+
"test": {
10+
"nestedProp": {
11+
"foo": "bar"
12+
},
13+
"otherProp": true
14+
}
15+
},
16+
"required": [
17+
"test"
18+
]
19+
},
20+
"equal": { "test": {} },
21+
"set": {
22+
"pruneProperties": ["nestedProp", "otherProp"]
23+
}
24+
}
25+
]
26+
}
27+
]

0 commit comments

Comments
 (0)