Skip to content
Snippets Groups Projects
Commit a994a498 authored by BRAMILLE Sébastien's avatar BRAMILLE Sébastien Committed by Alvaro Cabrera Durán
Browse files

Replace empty value by random value (#522)

* Update doc

* Declare option

* Permit test to handle multiple requires

* Add tests

* Add replaceEmptyByRandomValue test option

* Handle replaceEmptyByRandomValue option
parent 8924224d
No related branches found
No related tags found
No related merge requests found
...@@ -123,6 +123,7 @@ jsf.locate('faker'); ...@@ -123,6 +123,7 @@ jsf.locate('faker');
- `reuseProperties` — If enabled, it will try to generate missing properties from existing ones. Only when `fillProperties` is enabled too (default: `false`) - `reuseProperties` — If enabled, it will try to generate missing properties from existing ones. Only when `fillProperties` is enabled too (default: `false`)
- `fillProperties` — If enabled, it will try to generate missing properties to fulfill the schema definition (default: `true`) - `fillProperties` — If enabled, it will try to generate missing properties to fulfill the schema definition (default: `true`)
- `random` — Setup a custom _randonmess_ generator, useful for getting deterministic results (default: `Math.random`) - `random` — Setup a custom _randonmess_ generator, useful for getting deterministic results (default: `Math.random`)
- `replaceEmptyByRandomValue` — Remplace default empty value by a random value (default: `false`)
## Building ## Building
......
...@@ -25,6 +25,7 @@ defaults.maxLength = null; ...@@ -25,6 +25,7 @@ defaults.maxLength = null;
defaults.resolveJsonPath = false; defaults.resolveJsonPath = false;
defaults.reuseProperties = false; defaults.reuseProperties = false;
defaults.fillProperties = true; defaults.fillProperties = true;
defaults.replaceEmptyByRandomValue = false;
defaults.random = Math.random; defaults.random = Math.random;
......
...@@ -25,7 +25,9 @@ function traverse(schema, path, resolve, rootSchema) { ...@@ -25,7 +25,9 @@ function traverse(schema, path, resolve, rootSchema) {
} }
if (optionAPI('useDefaultValue') && 'default' in schema) { if (optionAPI('useDefaultValue') && 'default' in schema) {
return schema.default; if (schema.default !== '' || !optionAPI('replaceEmptyByRandomValue')) {
return schema.default;
}
} }
if ('template' in schema) { if ('template' in schema) {
......
module.exports = {
register(jsf) {
return jsf.option({
replaceEmptyByRandomValue: true,
});
},
};
...@@ -10,6 +10,30 @@ ...@@ -10,6 +10,30 @@
}, },
"equal": "Hello", "equal": "Hello",
"require": "core/option/useDefaultValue" "require": "core/option/useDefaultValue"
},
{
"description": "should handle useDefaultValue option with an empty default value",
"schema": {
"type": "string",
"default": ""
},
"equal": "",
"require": "core/option/useDefaultValue"
},
{
"description": "should handle useDefaultValue & replaceEmptyByRandomValue option",
"schema": {
"test-response": {
"type": "string",
"default": ""
}
},
"valid": true,
"notEmpty": ["test-response"],
"require": [
"core/option/useDefaultValue",
"core/option/replaceEmptyByRandomValue"
]
} }
] ]
} }
......
...@@ -29,7 +29,11 @@ function seed() { ...@@ -29,7 +29,11 @@ function seed() {
}); });
if (test.require) { if (test.require) {
require(`./${test.require}`).register(jsf); test.require = Array.isArray(test.require) ? test.require : [test.require];
test.require.map(toRequire => {
return require(`./${toRequire}`).register(jsf);
});
} }
const schema = typeof test.schema === 'string' const schema = typeof test.schema === 'string'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment