From dc267d7bb26f37eb489aa5e5a5826dc780334c3c Mon Sep 17 00:00:00 2001 From: Alvaro Cabrera <pateketrueke@gmail.com> Date: Fri, 25 Nov 2016 00:41:36 -0600 Subject: [PATCH] Fix: apply length constraints to all generated strings; see #193 --- lib/types/string.js | Bin 1674 -> 1867 bytes spec/schema/core/issues/issue-193.json | 25 ++++++++++++++ ts/types/string.ts | 46 ++++++++++++++++--------- 3 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 spec/schema/core/issues/issue-193.json diff --git a/lib/types/string.js b/lib/types/string.js index e78439b2996ca59119cd550ed56c7ab8bc0ac79e..c79b82b4c93ed52b982893f74adb56557945b566 100644 GIT binary patch delta 338 zcmeC;J<YdaE;CPAVv$0AX-Pq8i8a?`a~93XDJ-g!ds&nxm$S-DzQCd~`3H-`WE)mZ zrdqDa`K%t3ce6T7{?Dq(UdyGRprBAYc?Pp02SmSu?d1K;E(i`MixGn3#->sa(omj} znUkuZ0nwqClbV-alA&Oukeiw317>R~RD%r$sf8#~(6&|ZO)SaKD@x2u$<Ni$RIpPp z&@)%CS18FS&CAx%RIpM2Nu^d4XsGLHt7}?=4S={WGYw%^G}r<KJB8fD3Y?bNLIXpu exHPG_q)5X+2T3`y$wjFprA2vA)z*_U*%Sc}{b(5g delta 123 zcmX@j*TuVGF7xCjRvEUU)RNMoyvfU0U0}>ltVWa9GOJEL$E-Q|Gqd{SlPn68%~(_> uhq1U!p2nifrJ$goPzxp}A7$~F%*pC9*^^aQ4k3)nf?A-%rZQQZT>${G>LX+T diff --git a/spec/schema/core/issues/issue-193.json b/spec/schema/core/issues/issue-193.json new file mode 100644 index 00000000..5d495d9f --- /dev/null +++ b/spec/schema/core/issues/issue-193.json @@ -0,0 +1,25 @@ +[ + { + "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 + } + ] + } +] diff --git a/ts/types/string.ts b/ts/types/string.ts index 926a0bae..39d5f7ec 100644 --- a/ts/types/string.ts +++ b/ts/types/string.ts @@ -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; -- GitLab