@miroir-framework/jzod-ts 0.5.1 → 0.5.2
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/babel.config.cjs +13 -13
- package/dist/bundle.d.ts +409 -409
- package/dist/bundle.js +292 -292
- package/dist/src/JzodToTs.d.ts +17 -17
- package/dist/src/JzodTsInterface.d.ts +397 -397
- package/dist/src/index.d.ts +3 -3
- package/dist/src/tools.d.ts +4 -4
- package/jest.config.js +62 -62
- package/package.json +2 -2
- package/rollup.config.js +36 -36
- package/src/JzodToTs.ts +104 -104
- package/src/JzodTsInterface.ts +450 -450
- package/src/index.ts +60 -60
- package/src/tools.ts +24 -24
- package/tests/jzod-ts.test.ts +108 -108
- package/tests/resources/tsTypeGeneration-testJzodSchema1 - reference.ts +5 -5
- package/tests/resources/tsTypeGeneration-testJzodSchema2 - reference.ts +3 -3
- package/tests/resources/tsTypeGeneration-testJzodSchema4 - reference.ts +27 -27
- package/tsconfig.json +25 -25
- package/dist/src/facade.d.ts +0 -8
package/dist/src/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
|
|
2
|
-
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
3
|
-
export { jzodArraySchema, jzodAttributeSchema, jzodAttributeDateValidationsSchema, jzodAttributeDateWithValidationsSchema, jzodAttributeNumberValidationsSchema, jzodAttributeNumberWithValidationsSchema, jzodAttributeStringValidationsSchema, jzodAttributeStringWithValidationsSchema, jzodElementSchema, jzodEnumAttributeTypesSchema, jzodEnumElementTypesSchema, jzodEnumSchema, jzodFunctionSchema, jzodIntersectionSchema, jzodLazySchema, jzodLiteralSchema, jzodMapSchema, jzodObjectSchema, jzodPromiseSchema, jzodRecordSchema, jzodReferenceSchema, jzodSetSchema, jzodTupleSchema, jzodUnionSchema, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./JzodTsInterface";
|
|
1
|
+
export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
|
|
2
|
+
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
3
|
+
export { jzodArraySchema, jzodAttributeSchema, jzodAttributeDateValidationsSchema, jzodAttributeDateWithValidationsSchema, jzodAttributeNumberValidationsSchema, jzodAttributeNumberWithValidationsSchema, jzodAttributeStringValidationsSchema, jzodAttributeStringWithValidationsSchema, jzodElementSchema, jzodEnumAttributeTypesSchema, jzodEnumElementTypesSchema, jzodEnumSchema, jzodFunctionSchema, jzodIntersectionSchema, jzodLazySchema, jzodLiteralSchema, jzodMapSchema, jzodObjectSchema, jzodPromiseSchema, jzodRecordSchema, jzodReferenceSchema, jzodSetSchema, jzodTupleSchema, jzodUnionSchema, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./JzodTsInterface";
|
package/dist/src/tools.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { TsTypeAliases } from "./JzodToTs";
|
|
3
|
-
export declare function printTsTypeAlias(typeAlias: ts.TypeAliasDeclaration, exportPrefix?: boolean): string;
|
|
4
|
-
export declare function printTsTypeAliases(typeAliases: TsTypeAliases, exportPrefix?: boolean): string;
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { TsTypeAliases } from "./JzodToTs";
|
|
3
|
+
export declare function printTsTypeAlias(typeAlias: ts.TypeAliasDeclaration, exportPrefix?: boolean): string;
|
|
4
|
+
export declare function printTsTypeAliases(typeAliases: TsTypeAliases, exportPrefix?: boolean): string;
|
package/jest.config.js
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
// import { pathsToModuleNameMapper } from 'ts-jest';
|
|
2
|
-
// import compilerOptions from './tsconfig'
|
|
3
|
-
|
|
4
|
-
const esModules = ['uuid'].join('|');
|
|
5
|
-
|
|
6
|
-
export default (path, options) =>({
|
|
7
|
-
"verbose": true,
|
|
8
|
-
"rootDir": ".",
|
|
9
|
-
"moduleDirectories": [
|
|
10
|
-
"node_modules",
|
|
11
|
-
],
|
|
12
|
-
transformIgnorePatterns: [`<rootDir>\/(?!(/node_modules/(?!${esModules})))\/`],
|
|
13
|
-
// modulePaths: ["./packages/miroir-standalone-app/src/"], // <-- This will be set to 'baseUrl' value
|
|
14
|
-
modulePaths: ["./src/"], // <-- This will be set to 'baseUrl' value
|
|
15
|
-
// modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
|
|
16
|
-
moduleNameMapper:
|
|
17
|
-
// Object.assign(
|
|
18
|
-
{
|
|
19
|
-
// "^miroir-fwk(.*)$": "<rootDir>/src/$1",
|
|
20
|
-
// "^(.*)$": "$1",
|
|
21
|
-
'\\.(css|scss|sass)$': 'identity-obj-proxy',
|
|
22
|
-
}
|
|
23
|
-
// pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */)
|
|
24
|
-
// )
|
|
25
|
-
,
|
|
26
|
-
// resolver:options.defaultResolver(path, {...options}),
|
|
27
|
-
testEnvironment: "node",
|
|
28
|
-
moduleFileExtensions:[
|
|
29
|
-
"ts", "tsx", "js", "mjs", "cjs", "jsx", "json", "node"
|
|
30
|
-
],
|
|
31
|
-
// preset:'ts-jest/presets/default-esm',
|
|
32
|
-
"transform": {
|
|
33
|
-
"^.+\\.tsx?$": [
|
|
34
|
-
"ts-jest",
|
|
35
|
-
{
|
|
36
|
-
// useESM:true,
|
|
37
|
-
// tsconfig: "../../tsconfig.json"
|
|
38
|
-
tsconfig:
|
|
39
|
-
{
|
|
40
|
-
resolveJsonModule: true,
|
|
41
|
-
esModuleInterop: true,
|
|
42
|
-
allowSyntheticDefaultImports:true,
|
|
43
|
-
allowJs: true,
|
|
44
|
-
moduleResolution: "node",
|
|
45
|
-
// module: "ESNext",
|
|
46
|
-
// target: "ES2017",
|
|
47
|
-
// rootDir: "./src/",
|
|
48
|
-
traceResolution: true,
|
|
49
|
-
module:"commonjs"
|
|
50
|
-
},
|
|
51
|
-
}
|
|
52
|
-
],
|
|
53
|
-
"^.+\\.js?$": ["babel-jest"],
|
|
54
|
-
},
|
|
55
|
-
// moduleNameMapper: {
|
|
56
|
-
// "^miroir-fwk\/(.+)$": "<rootDir>/src/$1",
|
|
57
|
-
// '\\.(css|scss|sass)$': 'identity-obj-proxy',
|
|
58
|
-
// },
|
|
59
|
-
"testPathIgnorePatterns": [
|
|
60
|
-
"./node_modules/",
|
|
61
|
-
"./.cache/"
|
|
62
|
-
],
|
|
1
|
+
// import { pathsToModuleNameMapper } from 'ts-jest';
|
|
2
|
+
// import compilerOptions from './tsconfig'
|
|
3
|
+
|
|
4
|
+
const esModules = ['uuid'].join('|');
|
|
5
|
+
|
|
6
|
+
export default (path, options) =>({
|
|
7
|
+
"verbose": true,
|
|
8
|
+
"rootDir": ".",
|
|
9
|
+
"moduleDirectories": [
|
|
10
|
+
"node_modules",
|
|
11
|
+
],
|
|
12
|
+
transformIgnorePatterns: [`<rootDir>\/(?!(/node_modules/(?!${esModules})))\/`],
|
|
13
|
+
// modulePaths: ["./packages/miroir-standalone-app/src/"], // <-- This will be set to 'baseUrl' value
|
|
14
|
+
modulePaths: ["./src/"], // <-- This will be set to 'baseUrl' value
|
|
15
|
+
// modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
|
|
16
|
+
moduleNameMapper:
|
|
17
|
+
// Object.assign(
|
|
18
|
+
{
|
|
19
|
+
// "^miroir-fwk(.*)$": "<rootDir>/src/$1",
|
|
20
|
+
// "^(.*)$": "$1",
|
|
21
|
+
'\\.(css|scss|sass)$': 'identity-obj-proxy',
|
|
22
|
+
}
|
|
23
|
+
// pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */)
|
|
24
|
+
// )
|
|
25
|
+
,
|
|
26
|
+
// resolver:options.defaultResolver(path, {...options}),
|
|
27
|
+
testEnvironment: "node",
|
|
28
|
+
moduleFileExtensions:[
|
|
29
|
+
"ts", "tsx", "js", "mjs", "cjs", "jsx", "json", "node"
|
|
30
|
+
],
|
|
31
|
+
// preset:'ts-jest/presets/default-esm',
|
|
32
|
+
"transform": {
|
|
33
|
+
"^.+\\.tsx?$": [
|
|
34
|
+
"ts-jest",
|
|
35
|
+
{
|
|
36
|
+
// useESM:true,
|
|
37
|
+
// tsconfig: "../../tsconfig.json"
|
|
38
|
+
tsconfig:
|
|
39
|
+
{
|
|
40
|
+
resolveJsonModule: true,
|
|
41
|
+
esModuleInterop: true,
|
|
42
|
+
allowSyntheticDefaultImports:true,
|
|
43
|
+
allowJs: true,
|
|
44
|
+
moduleResolution: "node",
|
|
45
|
+
// module: "ESNext",
|
|
46
|
+
// target: "ES2017",
|
|
47
|
+
// rootDir: "./src/",
|
|
48
|
+
traceResolution: true,
|
|
49
|
+
module:"commonjs"
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
"^.+\\.js?$": ["babel-jest"],
|
|
54
|
+
},
|
|
55
|
+
// moduleNameMapper: {
|
|
56
|
+
// "^miroir-fwk\/(.+)$": "<rootDir>/src/$1",
|
|
57
|
+
// '\\.(css|scss|sass)$': 'identity-obj-proxy',
|
|
58
|
+
// },
|
|
59
|
+
"testPathIgnorePatterns": [
|
|
60
|
+
"./node_modules/",
|
|
61
|
+
"./.cache/"
|
|
62
|
+
],
|
|
63
63
|
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miroir-framework/jzod-ts",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"description": "The Typescript-related functionalities of Jzod.",
|
|
6
6
|
"main": "dist/bundle.js",
|
|
7
7
|
"types": "dist/bundle.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"bugs": "https://github.com/miroir-framework/jzod/issues",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@miroir-framework/jzod": "0.5.
|
|
18
|
+
"@miroir-framework/jzod": "0.5.2",
|
|
19
19
|
"zod": "^3.22.2",
|
|
20
20
|
"zod-to-ts": "^1.2.0"
|
|
21
21
|
},
|
package/rollup.config.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import typescript from 'rollup-plugin-typescript2';
|
|
2
|
-
import dts from 'rollup-plugin-dts';
|
|
3
|
-
|
|
4
|
-
export default [
|
|
5
|
-
{
|
|
6
|
-
input: ["src/index.ts"],
|
|
7
|
-
external: [
|
|
8
|
-
'zod',
|
|
9
|
-
'zod-to-ts',
|
|
10
|
-
'@miroir-framework/jzod',
|
|
11
|
-
],
|
|
12
|
-
output: [
|
|
13
|
-
{
|
|
14
|
-
file: `dist/bundle.js`,
|
|
15
|
-
entryFileNames: "[name].js",
|
|
16
|
-
format: "es",
|
|
17
|
-
exports: "named"
|
|
18
|
-
}
|
|
19
|
-
],
|
|
20
|
-
plugins: [
|
|
21
|
-
typescript(),
|
|
22
|
-
],
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
input: ["dist/src/index.d.ts"],
|
|
26
|
-
output: [
|
|
27
|
-
{
|
|
28
|
-
file: `dist/bundle.d.ts`,
|
|
29
|
-
format: 'es',
|
|
30
|
-
}
|
|
31
|
-
],
|
|
32
|
-
plugins: [
|
|
33
|
-
dts(),
|
|
34
|
-
],
|
|
35
|
-
}
|
|
36
|
-
];
|
|
1
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
2
|
+
import dts from 'rollup-plugin-dts';
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
{
|
|
6
|
+
input: ["src/index.ts"],
|
|
7
|
+
external: [
|
|
8
|
+
'zod',
|
|
9
|
+
'zod-to-ts',
|
|
10
|
+
'@miroir-framework/jzod',
|
|
11
|
+
],
|
|
12
|
+
output: [
|
|
13
|
+
{
|
|
14
|
+
file: `dist/bundle.js`,
|
|
15
|
+
entryFileNames: "[name].js",
|
|
16
|
+
format: "es",
|
|
17
|
+
exports: "named"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
plugins: [
|
|
21
|
+
typescript(),
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
input: ["dist/src/index.d.ts"],
|
|
26
|
+
output: [
|
|
27
|
+
{
|
|
28
|
+
file: `dist/bundle.d.ts`,
|
|
29
|
+
format: 'es',
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
plugins: [
|
|
33
|
+
dts(),
|
|
34
|
+
],
|
|
35
|
+
}
|
|
36
|
+
];
|
package/src/JzodToTs.ts
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { ZodLazy } from "zod";
|
|
3
|
-
import { createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
|
|
4
|
-
|
|
5
|
-
import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
|
|
6
|
-
|
|
7
|
-
import { JzodElement } from "./JzodTsInterface";
|
|
8
|
-
|
|
9
|
-
import { printTsTypeAliases } from "./tools";
|
|
10
|
-
// ################################################################################################
|
|
11
|
-
export type TsTypeAliases = {
|
|
12
|
-
[k: string]: ts.TypeAliasDeclaration;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// ################################################################################################
|
|
16
|
-
export interface TsTypeAliasesAndZodText {
|
|
17
|
-
contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
|
|
18
|
-
contextZodText: { [k: string]: string },
|
|
19
|
-
mainTsTypeAlias: ts.TypeAliasDeclaration,
|
|
20
|
-
mainZodText: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// ################################################################################################
|
|
24
|
-
export function jzodToTsTypeAliasesAndZodText(
|
|
25
|
-
element: JzodElement,
|
|
26
|
-
typeName?: string,
|
|
27
|
-
): TsTypeAliasesAndZodText
|
|
28
|
-
{
|
|
29
|
-
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
|
|
30
|
-
element,
|
|
31
|
-
() => ({}),
|
|
32
|
-
() => ({}),
|
|
33
|
-
(innerReference: ZodLazy<any>, relativeReference: string | undefined) =>
|
|
34
|
-
withGetType(innerReference, (ts) => {
|
|
35
|
-
return ts.factory.createTypeReferenceNode(
|
|
36
|
-
ts.factory.createIdentifier(relativeReference ? relativeReference : "RELATIVEPATH_NOT_DEFINED")
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
)
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
const contextTsTypesString = Object.fromEntries(
|
|
43
|
-
Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
|
|
44
|
-
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription ", JSON.stringify(curr));
|
|
45
|
-
const tsNode = zodToTs(curr[1], typeName).node;
|
|
46
|
-
const typeAlias = createTypeAlias(tsNode, curr[0]);
|
|
47
|
-
return [curr[0], typeAlias];
|
|
48
|
-
})
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
|
|
52
|
-
const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName??"");
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
contextTsTypeAliases: contextTsTypesString,
|
|
56
|
-
contextZodText: elementZodSchemaAndDescription.contextZodText??{},
|
|
57
|
-
mainZodText: elementZodSchemaAndDescription.zodText,
|
|
58
|
-
mainTsTypeAlias: tsTypeStringTypeAlias,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// ################################################################################################
|
|
64
|
-
export function jzodToTsCode (
|
|
65
|
-
jzodElement: JzodElement,
|
|
66
|
-
exportPrefix: boolean = true,
|
|
67
|
-
typeName?: string,
|
|
68
|
-
): string {
|
|
69
|
-
// console.log(
|
|
70
|
-
// "getTsCodeCorrespondingToZodSchemaAndDescription jzodSchemaZodSchemaAndDescription.contextZodSchema",
|
|
71
|
-
// "jzodElement",
|
|
72
|
-
// JSON.stringify(jzodElement)
|
|
73
|
-
// );
|
|
74
|
-
|
|
75
|
-
const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
|
|
76
|
-
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription convertedJsonZodSchema", bodyJsCode);
|
|
77
|
-
|
|
78
|
-
const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
|
|
79
|
-
|
|
80
|
-
const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, typeName);
|
|
81
|
-
|
|
82
|
-
const bodyJsCode = `export const ${schemaName}: z.ZodType<${schemaName}> = ${typeAliasesAndZodText.mainZodText};`;
|
|
83
|
-
|
|
84
|
-
const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
|
|
85
|
-
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription contextTsTypesString",contextTsTypesString);
|
|
86
|
-
|
|
87
|
-
const contextJsCode = typeAliasesAndZodText.contextZodText
|
|
88
|
-
? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
|
|
89
|
-
return `${acc}
|
|
90
|
-
export const ${curr[0]}
|
|
91
|
-
}, "")
|
|
92
|
-
: ""
|
|
93
|
-
;
|
|
94
|
-
|
|
95
|
-
const tsTypesString = (exportPrefix?"export ":"") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
|
|
96
|
-
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription tsTypeString",tsTypesString);
|
|
97
|
-
|
|
98
|
-
return `${header}
|
|
99
|
-
${contextTsTypesString}
|
|
100
|
-
${tsTypesString}
|
|
101
|
-
${contextJsCode}
|
|
102
|
-
${bodyJsCode}
|
|
103
|
-
`;
|
|
104
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ZodLazy } from "zod";
|
|
3
|
+
import { createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
|
|
4
|
+
|
|
5
|
+
import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
|
|
6
|
+
|
|
7
|
+
import { JzodElement } from "./JzodTsInterface";
|
|
8
|
+
|
|
9
|
+
import { printTsTypeAliases } from "./tools";
|
|
10
|
+
// ################################################################################################
|
|
11
|
+
export type TsTypeAliases = {
|
|
12
|
+
[k: string]: ts.TypeAliasDeclaration;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ################################################################################################
|
|
16
|
+
export interface TsTypeAliasesAndZodText {
|
|
17
|
+
contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
|
|
18
|
+
contextZodText: { [k: string]: string },
|
|
19
|
+
mainTsTypeAlias: ts.TypeAliasDeclaration,
|
|
20
|
+
mainZodText: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ################################################################################################
|
|
24
|
+
export function jzodToTsTypeAliasesAndZodText(
|
|
25
|
+
element: JzodElement,
|
|
26
|
+
typeName?: string,
|
|
27
|
+
): TsTypeAliasesAndZodText
|
|
28
|
+
{
|
|
29
|
+
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
|
|
30
|
+
element,
|
|
31
|
+
() => ({}),
|
|
32
|
+
() => ({}),
|
|
33
|
+
(innerReference: ZodLazy<any>, relativeReference: string | undefined) =>
|
|
34
|
+
withGetType(innerReference, (ts) => {
|
|
35
|
+
return ts.factory.createTypeReferenceNode(
|
|
36
|
+
ts.factory.createIdentifier(relativeReference ? relativeReference : "RELATIVEPATH_NOT_DEFINED")
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const contextTsTypesString = Object.fromEntries(
|
|
43
|
+
Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
|
|
44
|
+
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription ", JSON.stringify(curr));
|
|
45
|
+
const tsNode = zodToTs(curr[1], typeName).node;
|
|
46
|
+
const typeAlias = createTypeAlias(tsNode, curr[0]);
|
|
47
|
+
return [curr[0], typeAlias];
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
|
|
52
|
+
const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName??"");
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
contextTsTypeAliases: contextTsTypesString,
|
|
56
|
+
contextZodText: elementZodSchemaAndDescription.contextZodText??{},
|
|
57
|
+
mainZodText: elementZodSchemaAndDescription.zodText,
|
|
58
|
+
mainTsTypeAlias: tsTypeStringTypeAlias,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// ################################################################################################
|
|
64
|
+
export function jzodToTsCode (
|
|
65
|
+
jzodElement: JzodElement,
|
|
66
|
+
exportPrefix: boolean = true,
|
|
67
|
+
typeName?: string,
|
|
68
|
+
): string {
|
|
69
|
+
// console.log(
|
|
70
|
+
// "getTsCodeCorrespondingToZodSchemaAndDescription jzodSchemaZodSchemaAndDescription.contextZodSchema",
|
|
71
|
+
// "jzodElement",
|
|
72
|
+
// JSON.stringify(jzodElement)
|
|
73
|
+
// );
|
|
74
|
+
|
|
75
|
+
const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
|
|
76
|
+
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription convertedJsonZodSchema", bodyJsCode);
|
|
77
|
+
|
|
78
|
+
const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
|
|
79
|
+
|
|
80
|
+
const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, typeName);
|
|
81
|
+
|
|
82
|
+
const bodyJsCode = `export const ${schemaName}: z.ZodType<${schemaName}> = ${typeAliasesAndZodText.mainZodText};`;
|
|
83
|
+
|
|
84
|
+
const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
|
|
85
|
+
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription contextTsTypesString",contextTsTypesString);
|
|
86
|
+
|
|
87
|
+
const contextJsCode = typeAliasesAndZodText.contextZodText
|
|
88
|
+
? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
|
|
89
|
+
return `${acc}
|
|
90
|
+
export const ${curr[0]}:z.ZodType<${curr[0]}> = ${curr[1]};`;
|
|
91
|
+
}, "")
|
|
92
|
+
: ""
|
|
93
|
+
;
|
|
94
|
+
|
|
95
|
+
const tsTypesString = (exportPrefix?"export ":"") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
|
|
96
|
+
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription tsTypeString",tsTypesString);
|
|
97
|
+
|
|
98
|
+
return `${header}
|
|
99
|
+
${contextTsTypesString}
|
|
100
|
+
${tsTypesString}
|
|
101
|
+
${contextJsCode}
|
|
102
|
+
${bodyJsCode}
|
|
103
|
+
`;
|
|
104
|
+
}
|