Skip to content
Snippets Groups Projects
Commit d64247ff authored by Kyle Fuller's avatar Kyle Fuller
Browse files

Allow setting a maxLength and maxItems to limit generatioN

Closes #199
parent 63e50e00
No related branches found
No related tags found
No related merge requests found
...@@ -401,6 +401,8 @@ You may define following options for `jsf` that alter its behavior: ...@@ -401,6 +401,8 @@ You may define following options for `jsf` that alter its behavior:
- `failOnInvalidTypes`: boolean - don't throw exception when invalid type passed - `failOnInvalidTypes`: boolean - don't throw exception when invalid type passed
- `defaultInvalidTypeProduct`: - default value generated for a schema with invalid type (works only if `failOnInvalidTypes` is set to `false`) - `defaultInvalidTypeProduct`: - default value generated for a schema with invalid type (works only if `failOnInvalidTypes` is set to `false`)
- `maxItems`: number - Configure a maximum amount of items to generate in an array. This will override the maximum items found inside a JSON Schema.
- `maxLength`: number - Configure a maximum length to allow generating strings for. This will override the maximum length found inside a JSON Schema.
Set options just as below: Set options just as below:
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
...@@ -12,6 +12,8 @@ class OptionRegistry extends Registry<Option> { ...@@ -12,6 +12,8 @@ class OptionRegistry extends Registry<Option> {
this.data['failOnInvalidTypes'] = true; this.data['failOnInvalidTypes'] = true;
this.data['defaultInvalidTypeProduct'] = null; this.data['defaultInvalidTypeProduct'] = null;
this.data['useDefaultValue'] = false; this.data['useDefaultValue'] = false;
this.data['maxItems'] = null;
this.data['maxLength'] = null;
} }
} }
......
import random = require('../core/random'); import random = require('../core/random');
import utils = require('../core/utils'); import utils = require('../core/utils');
import ParseError = require('../core/error'); import ParseError = require('../core/error');
import option = require('../api/option');
// TODO provide types // TODO provide types
function unique(path: SchemaPath, items, value, sample, resolve, traverseCallback: Function) { function unique(path: SchemaPath, items, value, sample, resolve, traverseCallback: Function) {
...@@ -56,7 +57,22 @@ var arrayType: FTypeGenerator = function arrayType(value: IArraySchema, path: Sc ...@@ -56,7 +57,22 @@ var arrayType: FTypeGenerator = function arrayType(value: IArraySchema, path: Sc
})); }));
} }
var length: number = random.number(value.minItems, value.maxItems, 1, 5), var minItems = value.minItems;
var maxItems = value.maxItems;
if (option('maxItems')) {
// Don't allow user to set max items above our maximum
if (maxItems && maxItems > option('maxItems')) {
maxItems = option('maxItems');
}
// Don't allow user to set min items above our maximum
if (minItems && minItems > option('maxItems')) {
minItems = maxItems;
}
}
var length: number = random.number(minItems, maxItems, 1, 5),
// TODO below looks bad. Should additionalItems be copied as-is? // TODO below looks bad. Should additionalItems be copied as-is?
sample: Object = typeof value.additionalItems === 'object' ? value.additionalItems : {}; sample: Object = typeof value.additionalItems === 'object' ? value.additionalItems : {};
......
...@@ -3,6 +3,7 @@ import ipv4 = require('../generators/ipv4'); ...@@ -3,6 +3,7 @@ import ipv4 = require('../generators/ipv4');
import dateTime = require('../generators/dateTime'); import dateTime = require('../generators/dateTime');
import coreFormat = require('../generators/coreFormat'); import coreFormat = require('../generators/coreFormat');
import format = require('../api/format'); import format = require('../api/format');
import option = require('../api/option');
import container = require('../class/Container'); import container = require('../class/Container');
var randexp = container.get('randexp'); var randexp = container.get('randexp');
...@@ -33,7 +34,22 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri ...@@ -33,7 +34,22 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri
} else if (value.pattern) { } else if (value.pattern) {
return randexp(value.pattern); return randexp(value.pattern);
} else { } else {
return thunk(value.minLength, value.maxLength); var minLength = value.minLength;
var maxLength = value.maxLength;
if (option('maxLength')) {
// Don't allow user to set max length above our maximum
if (maxLength && maxLength > option('maxLength')) {
maxLength = option('maxLength');
}
// Don't allow user to set min length above our maximum
if (minLength && minLength > option('maxLength')) {
minLength = option('maxLength');
}
}
return thunk(minLength, maxLength);
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment