diff --git a/.changeset/modern-kids-joke.md b/.changeset/modern-kids-joke.md new file mode 100644 index 000000000..f5939c59b --- /dev/null +++ b/.changeset/modern-kids-joke.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": patch +--- + +fix regexp for numeric template literals diff --git a/src/Arbitrary.ts b/src/Arbitrary.ts index 9225b535b..6e307a28c 100644 --- a/src/Arbitrary.ts +++ b/src/Arbitrary.ts @@ -118,9 +118,13 @@ export const go = (ast: AST.AST, constraints?: Constraints): Arbitrary => { return (fc) => fc.oneof(fc.object(), fc.array(fc.anything())) case "TemplateLiteral": { return (fc) => { - const components = [fc.constant(ast.head)] + const components: Array> = [fc.constant(ast.head)] for (const span of ast.spans) { - components.push(fc.string({ maxLength: 5 })) + if (AST.isStringKeyword(span.type)) { + components.push(fc.string({ maxLength: 5 })) + } else { + components.push(fc.float({ noDefaultInfinity: true })) + } components.push(fc.constant(span.literal)) } return fc.tuple(...components).map((spans) => spans.join("")) diff --git a/src/Parser.ts b/src/Parser.ts index 177f43527..d2b1c8bf4 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -1062,7 +1062,7 @@ const getTemplateLiteralRegex = (ast: AST.TemplateLiteral): RegExp => { if (AST.isStringKeyword(span.type)) { pattern += ".*" } else if (AST.isNumberKeyword(span.type)) { - pattern += "-?\\d+(\\.\\d+)?" + pattern += "[+-]?\\d*\\.?\\d+(?:[Ee][+-]?\\d+)?" } pattern += span.literal } diff --git a/test/Arbitrary/Arbitrary.test.ts b/test/Arbitrary/Arbitrary.test.ts index 1665175f5..68ee300e2 100644 --- a/test/Arbitrary/Arbitrary.test.ts +++ b/test/Arbitrary/Arbitrary.test.ts @@ -63,6 +63,11 @@ describe("Arbitrary/Arbitrary", () => { propertyTo(schema) }) + it("templateLiteral. a${number}", () => { + const schema = S.templateLiteral(S.literal("a"), S.number) + propertyTo(schema) + }) + it("templateLiteral. a", () => { const schema = S.templateLiteral(S.literal("a")) propertyTo(schema) diff --git a/test/Schema/templateLiteral.test.ts b/test/Schema/templateLiteral.test.ts index a83c836e9..45d1f2bc1 100644 --- a/test/Schema/templateLiteral.test.ts +++ b/test/Schema/templateLiteral.test.ts @@ -104,8 +104,22 @@ describe("Schema/templateLiteral", () => { it("a${number}", async () => { const schema = S.templateLiteral(S.literal("a"), S.number) - await Util.expectParseSuccess(schema, "a1", "a1") - await Util.expectParseSuccess(schema, "a1.2", "a1.2") + await Util.expectParseSuccess(schema, "a1") + await Util.expectParseSuccess(schema, "a1.2") + + await Util.expectParseSuccess(schema, "a-1.401298464324817e-45") + await Util.expectParseSuccess(schema, "a1.401298464324817e-45") + await Util.expectParseSuccess(schema, "a+1.401298464324817e-45") + await Util.expectParseSuccess(schema, "a-1.401298464324817e+45") + await Util.expectParseSuccess(schema, "a1.401298464324817e+45") + await Util.expectParseSuccess(schema, "a+1.401298464324817e+45") + + await Util.expectParseSuccess(schema, "a-1.401298464324817E-45") + await Util.expectParseSuccess(schema, "a1.401298464324817E-45") + await Util.expectParseSuccess(schema, "a+1.401298464324817E-45") + await Util.expectParseSuccess(schema, "a-1.401298464324817E+45") + await Util.expectParseSuccess(schema, "a1.401298464324817E+45") + await Util.expectParseSuccess(schema, "a+1.401298464324817E+45") await Util.expectParseFailure( schema,