Skip to content

Commit

Permalink
feat: adds pruneProperties option for deep cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Jul 21, 2021
1 parent 41c96bf commit 5a149eb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jsf.locate('faker');

- `defaultInvalidTypeProduct` — If `failOnInvalidTypes` is disabled this value will be returned on any invalid `type` given (default: `null`)
- `defaultRandExpMax` — Setup default value directly as `RandExp.prototype.max` (default: `10`)
- `pruneProperties` — Remove given properties from generated objects (default: `[]`)
- `ignoreProperties` — Skip given properties from being generated (default: `[]`)
- `ignoreMissingRefs` — If enabled, it will resolve to `{}` for unknown references (default: `false`)
- `failOnInvalidTypes` — If enabled, it will throw an `Error` for unknown types (default: `true`)
Expand Down
1 change: 1 addition & 0 deletions src/lib/api/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defaults;
defaults.defaultInvalidTypeProduct = undefined;
defaults.defaultRandExpMax = 10;

defaults.pruneProperties = [];
defaults.ignoreProperties = [];
defaults.ignoreMissingRefs = false;
defaults.failOnInvalidTypes = true;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ function traverse(schema, path, resolve, rootSchema) {
valueCopy = [];
}

const pruneProperties = optionAPI('pruneProperties') || [];

Object.keys(schema).forEach(prop => {
if (pruneProperties.includes(prop)) return;
if (typeof schema[prop] === 'object' && prop !== 'definitions') {
const { value, context: innerContext } = traverse(schema[prop], path.concat([prop]), resolve, valueCopy);
valueCopy[prop] = utils.clean(value, schema[prop], false);
Expand Down
27 changes: 27 additions & 0 deletions tests/schema/core/option/pruneProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"description": "pruneProperties option",
"tests": [
{
"description": "should remove nested keys from pruneProperties",
"schema": {
"properties": {
"test": {
"nestedProp": {
"foo": "bar"
},
"otherProp": true
}
},
"required": [
"test"
]
},
"equal": { "test": {} },
"set": {
"pruneProperties": ["nestedProp", "otherProp"]
}
}
]
}
]

0 comments on commit 5a149eb

Please sign in to comment.