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
No related branches found
No related tags found
No related merge requests found
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
[
{
"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,11 +29,8 @@ function generateFormat(value: IStringSchema): string { ...@@ -29,11 +29,8 @@ function generateFormat(value: IStringSchema): string {
} }
var stringType: FTypeGenerator = function stringType(value: IStringSchema): string { var stringType: FTypeGenerator = function stringType(value: IStringSchema): string {
if (value.format) { var output: string;
return generateFormat(value);
} else if (value.pattern) {
return randexp(value.pattern);
} else {
var minLength = value.minLength; var minLength = value.minLength;
var maxLength = value.maxLength; var maxLength = value.maxLength;
...@@ -49,8 +46,23 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri ...@@ -49,8 +46,23 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri
} }
} }
return thunk(minLength, maxLength); if (value.format) {
output = generateFormat(value);
} else if (value.pattern) {
output = randexp(value.pattern);
} else {
output = 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; export = stringType;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment