@povio/openapi-codegen-cli 0.10.5 → 0.10.7
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.
- package/dist/generators/const/deps.const.d.ts +7 -1
- package/dist/generators/generate/generateZod.d.ts +2 -0
- package/dist/generators/utils/validation.utils.d.ts +1 -0
- package/dist/index.js +44 -44
- package/dist/sh.js +65 -65
- package/package.json +1 -1
- package/src/generators/templates/partials/endpoint-param-sorting.hbs +1 -1
- package/src/generators/templates/zod.hbs +40 -0
- package/src/assets/zodExtended.ts +0 -24
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{{zodExtended}}.{{sortingString}}({{importedZodSchemaName enumSchemaName}})
|
|
1
|
+
{{zodExtended}}.{{parse}}({{zodExtended}}.{{sortingString}}({{importedZodSchemaName enumSchemaName}}), {{paramName}})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* eslint-disable no-useless-escape */
|
|
2
|
+
{{! Zod import }}
|
|
3
|
+
{{{genImport zodImport}}}
|
|
4
|
+
{{! Error handling import }}
|
|
5
|
+
{{{genImport errorHandlingImport}}}
|
|
6
|
+
|
|
7
|
+
export const {{zodExtension}} = {
|
|
8
|
+
{{sortingString}}: (enumSchema: z.ZodEnum<[string, ...string[]]>) =>
|
|
9
|
+
z
|
|
10
|
+
.string()
|
|
11
|
+
.optional()
|
|
12
|
+
.superRefine((arg, ctx) => {
|
|
13
|
+
if (!isSortingStringValid(enumSchema, arg)) {
|
|
14
|
+
ctx.addIssue({
|
|
15
|
+
code: z.ZodIssueCode.custom,
|
|
16
|
+
message: "Invalid sorting string.",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}),
|
|
20
|
+
{{parse}},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function isSortingStringValid(enumSchema: z.ZodEnum<[string, ...string[]]>, data?: string) {
|
|
24
|
+
if (data === undefined || data === "" || enumSchema.options.length === 0) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const prefixedEnumOptions = `([+-]?(${enumSchema.options.join("|")}))`;
|
|
29
|
+
const commaSeparatedOptions = `(${prefixedEnumOptions})(\s*,\s*${prefixedEnumOptions})*`;
|
|
30
|
+
return new RegExp(`^${commaSeparatedOptions}$`).test(data);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function {{parse}}(schema: z.ZodSchema, data: unknown, errorHandler?: {{errorHandler}}<never>) {
|
|
34
|
+
try {
|
|
35
|
+
return schema.parse(data);
|
|
36
|
+
} catch (e: unknown) {
|
|
37
|
+
(errorHandler ?? {{sharedErrorHandler}}).rethrowError(e);
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-useless-escape */
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
export const zodExtended = {
|
|
5
|
-
sortingString: (enumSchema: z.ZodEnum<[string, ...string[]]>) =>
|
|
6
|
-
z.string().superRefine((val, ctx) => {
|
|
7
|
-
if (!isSortingStringValid(val, enumSchema)) {
|
|
8
|
-
ctx.addIssue({
|
|
9
|
-
code: z.ZodIssueCode.custom,
|
|
10
|
-
message: "Invalid sorting string.",
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
}),
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
function isSortingStringValid(val: string, enumSchema: z.ZodEnum<[string, ...string[]]>) {
|
|
17
|
-
if (val === "" || enumSchema.options.length === 0) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const prefixedEnumOptions = `([+-]?(${enumSchema.options.join("|")}))`;
|
|
22
|
-
const commaSeparatedOptions = `(${prefixedEnumOptions})(\s*,\s*${prefixedEnumOptions})*`;
|
|
23
|
-
return new RegExp(`^${commaSeparatedOptions}$`).test(val);
|
|
24
|
-
}
|