Skip to content

Commit 9a7b280

Browse files
authored
Allow actual RegExp objects in the regex field of a textInput (#1247)
1 parent abb547f commit 9a7b280

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

spec/params/params.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe("Params spec extraction", () => {
99
.default
1010
).to.equal(`{{ params.BAR != 22 ? "asdf" : "jkl;" }}`);
1111
});
12+
13+
it("converts RegExps in string validation parameters to strings", () => {
14+
const foo = params.defineString("FOO", { input: { text: { validationRegex: /\d{5}/ } } });
15+
expect(foo.toSpec().input).to.deep.equal({ text: { validationRegex: "\\d{5}" } });
16+
});
1217
});
1318

1419
describe("Params value extraction", () => {

spec/runtime/manifest.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ describe("stackToWire", () => {
99
params.clearParams();
1010
});
1111

12+
it("converts regular expressions used in param inputs", () => {
13+
const regExpParam = params.defineString("foo", {
14+
input: { text: { validationRegex: /\d{5}/ } },
15+
});
16+
17+
const stack: ManifestStack = {
18+
endpoints: {},
19+
requiredAPIs: [],
20+
params: [regExpParam.toSpec()],
21+
specVersion: "v1alpha1",
22+
};
23+
const expected = {
24+
endpoints: {},
25+
requiredAPIs: [],
26+
params: [
27+
{
28+
name: "foo",
29+
type: "string",
30+
input: {
31+
text: {
32+
validationRegex: "\\d{5}",
33+
},
34+
},
35+
},
36+
],
37+
specVersion: "v1alpha1",
38+
};
39+
expect(stackToWire(stack)).to.deep.equal(expected);
40+
});
41+
1242
it("converts stack with null values", () => {
1343
const stack: ManifestStack = {
1444
endpoints: {

src/params/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ type ParamInput<T> =
151151
// eslint-disable-next-line @typescript-eslint/no-unused-vars
152152
export interface TextInput<T = unknown> {
153153
example?: string;
154-
validationRegex?: string;
154+
validationRegex?: string | RegExp;
155155
validationErrorMessage?: string;
156156
}
157157

@@ -261,6 +261,10 @@ export abstract class Param<T extends string | number | boolean | string[]> exte
261261
out.default = paramDefault;
262262
}
263263

264+
if (out.input && "text" in out.input && out.input.text.validationRegex instanceof RegExp) {
265+
out.input.text.validationRegex = out.input.text.validationRegex.source;
266+
}
267+
264268
return out;
265269
}
266270
}

0 commit comments

Comments
 (0)