Skip to content
Snippets Groups Projects
Commit dc267d7b authored by Alvaro Cabrera's avatar Alvaro Cabrera
Browse files

Fix: apply length constraints to all generated strings; see #193

parent fdd16b1e
Branches
Tags
No related merge requests found
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
[
{
"description": "pattern and min/max length constraints",
"tests": [
{
"description": "should support minLength",
"schema": {
"type": "string",
"pattern": ".+",
"minLength": 21
},
"valid": true
},
{
"description": "should support maxLength",
"schema": {
"type": "string",
"pattern": ".+",
"maxLength": 2
},
"valid": true
}
]
}
]
......@@ -29,28 +29,40 @@ function generateFormat(value: IStringSchema): string {
}
var stringType: FTypeGenerator = function stringType(value: IStringSchema): string {
var output: string;
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');
}
}
if (value.format) {
return generateFormat(value);
output = generateFormat(value);
} else if (value.pattern) {
return randexp(value.pattern);
output = randexp(value.pattern);
} else {
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');
}
}
output = thunk(minLength, maxLength);
}
return thunk(minLength, maxLength);
while (output.length < minLength) {
output += Math.random() > 0.7 ? thunk() : randexp('.+');
}
if (output.length > maxLength) {
output = output.substr(0, maxLength);
}
return output;
};
export = stringType;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment