@f3liz/rescript-autogen-openapi 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -31,6 +31,18 @@ function escapeString(str) {
|
|
|
31
31
|
return str.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"").replaceAll("\n", "\\n").replaceAll("\r", "\\r").replaceAll("\t", "\\t");
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function escapeRegexPattern(str) {
|
|
35
|
+
return escapeString((function(s) {
|
|
36
|
+
return s.replace(/(\\*)(\/)/g, function(match, backslashes, slash) {
|
|
37
|
+
if (backslashes.length % 2 === 0) {
|
|
38
|
+
return backslashes + '\\\\' + slash;
|
|
39
|
+
} else {
|
|
40
|
+
return match;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
})(str));
|
|
44
|
+
}
|
|
45
|
+
|
|
34
46
|
function generateFileHeader(description) {
|
|
35
47
|
return `// ` + description + `
|
|
36
48
|
// Generated by @f3liz/rescript-autogen-openapi
|
|
@@ -247,6 +259,7 @@ export {
|
|
|
247
259
|
generateTypeName,
|
|
248
260
|
generateOperationName,
|
|
249
261
|
escapeString,
|
|
262
|
+
escapeRegexPattern,
|
|
250
263
|
generateFileHeader,
|
|
251
264
|
indent,
|
|
252
265
|
trimMargin,
|
|
@@ -50,7 +50,7 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
50
50
|
let s = applyConstraints("S.string", c.minLength, c.maxLength, v => v.toString());
|
|
51
51
|
let p = c.pattern;
|
|
52
52
|
if (p !== undefined) {
|
|
53
|
-
return s + `->S.pattern(%re("/` + CodegenUtils.
|
|
53
|
+
return s + `->S.pattern(%re("/` + CodegenUtils.escapeRegexPattern(p) + `/"))`;
|
|
54
54
|
} else {
|
|
55
55
|
return s;
|
|
56
56
|
}
|
package/package.json
CHANGED
|
@@ -48,6 +48,23 @@ let escapeString = (str: string): string =>
|
|
|
48
48
|
->String.replaceAll("\n", "\\n")->String.replaceAll("\r", "\\r")
|
|
49
49
|
->String.replaceAll("\t", "\\t")
|
|
50
50
|
|
|
51
|
+
// Escape Regex Pattern (escape unescaped slashes, then escape string)
|
|
52
|
+
let escapeRegexPattern = (str: string): string => {
|
|
53
|
+
let withEscapedSlashes = %raw(`
|
|
54
|
+
function(s) {
|
|
55
|
+
return s.replace(/(\\*)(\/)/g, function(match, backslashes, slash) {
|
|
56
|
+
if (backslashes.length % 2 === 0) {
|
|
57
|
+
return backslashes + '\\\\' + slash;
|
|
58
|
+
} else {
|
|
59
|
+
return match;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
`)(str)
|
|
64
|
+
|
|
65
|
+
escapeString(withEscapedSlashes)
|
|
66
|
+
}
|
|
67
|
+
|
|
51
68
|
// Generate file header
|
|
52
69
|
let generateFileHeader = (~description: string): string =>
|
|
53
70
|
`// ${description}
|
|
@@ -42,7 +42,7 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
42
42
|
| String({constraints: c}) =>
|
|
43
43
|
let s = applyConstraints("S.string", c.minLength, c.maxLength, v => Int.toString(v))
|
|
44
44
|
switch c.pattern {
|
|
45
|
-
| Some(p) => `${s}->S.pattern(%re("/${CodegenUtils.
|
|
45
|
+
| Some(p) => `${s}->S.pattern(%re("/${CodegenUtils.escapeRegexPattern(p)}/"))`
|
|
46
46
|
| None => s
|
|
47
47
|
}
|
|
48
48
|
| Number({constraints: c}) =>
|