@miroir-framework/jzod-ts 0.5.1 → 0.5.3
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 +411 -411
- package/dist/bundle.js +297 -293
- 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 +109 -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 +7 -7
- package/tests/resources/tsTypeGeneration-testJzodSchema3 - reference.ts +6 -6
- package/tests/resources/tsTypeGeneration-testJzodSchema4 - reference.ts +74 -74
- package/tsconfig.json +25 -25
- package/dist/src/facade.d.ts +0 -8
package/src/index.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
export {
|
|
2
|
-
jzodToTsCode,
|
|
3
|
-
jzodToTsTypeAliasesAndZodText,
|
|
4
|
-
TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText,
|
|
5
|
-
TsTypeAliases,
|
|
6
|
-
} from "./JzodToTs";
|
|
7
|
-
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
// SCHEMAS (const)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
jzodMapSchema,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// TYPES
|
|
36
|
-
JzodArray,
|
|
37
|
-
JzodAttribute,
|
|
38
|
-
JzodAttributeDateValidations,
|
|
39
|
-
JzodAttributeDateWithValidations,
|
|
40
|
-
JzodAttributeNumberValidations,
|
|
41
|
-
JzodAttributeNumberWithValidations,
|
|
42
|
-
JzodAttributeStringValidations,
|
|
43
|
-
JzodAttributeStringWithValidations,
|
|
44
|
-
JzodElement,
|
|
45
|
-
JzodEnum,
|
|
46
|
-
JzodEnumTypes,
|
|
47
|
-
JzodFunction,
|
|
48
|
-
JzodEnumElementTypes,
|
|
49
|
-
JzodIntersection,
|
|
50
|
-
JzodLazy,
|
|
51
|
-
JzodLiteral,
|
|
52
|
-
JzodMap,
|
|
53
|
-
JzodObject,
|
|
54
|
-
JzodPromise,
|
|
55
|
-
JzodRecord,
|
|
56
|
-
JzodReference,
|
|
57
|
-
JzodSet,
|
|
58
|
-
JzodUnion,
|
|
59
|
-
JzodTuple,
|
|
60
|
-
} from "./JzodTsInterface";
|
|
1
|
+
export {
|
|
2
|
+
jzodToTsCode,
|
|
3
|
+
jzodToTsTypeAliasesAndZodText,
|
|
4
|
+
TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText,
|
|
5
|
+
TsTypeAliases,
|
|
6
|
+
} from "./JzodToTs";
|
|
7
|
+
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
// SCHEMAS (const)
|
|
11
|
+
jzodArray,
|
|
12
|
+
jzodAttribute,
|
|
13
|
+
jzodAttributeDateValidations,
|
|
14
|
+
jzodAttributeDateWithValidations,
|
|
15
|
+
jzodAttributeNumberValidations,
|
|
16
|
+
jzodAttributeNumberWithValidations,
|
|
17
|
+
jzodAttributeStringValidations,
|
|
18
|
+
jzodAttributeStringWithValidations,
|
|
19
|
+
jzodElement,
|
|
20
|
+
jzodEnumAttributeTypes,
|
|
21
|
+
jzodEnumElementTypes,
|
|
22
|
+
jzodEnum,
|
|
23
|
+
jzodFunction,
|
|
24
|
+
jzodIntersection,
|
|
25
|
+
jzodLazy,
|
|
26
|
+
jzodLiteral,
|
|
27
|
+
jzodMapSchema,
|
|
28
|
+
jzodObject,
|
|
29
|
+
jzodPromise,
|
|
30
|
+
jzodRecord,
|
|
31
|
+
jzodReference,
|
|
32
|
+
jzodSet,
|
|
33
|
+
jzodTuple,
|
|
34
|
+
jzodUnion,
|
|
35
|
+
// TYPES
|
|
36
|
+
JzodArray,
|
|
37
|
+
JzodAttribute,
|
|
38
|
+
JzodAttributeDateValidations,
|
|
39
|
+
JzodAttributeDateWithValidations,
|
|
40
|
+
JzodAttributeNumberValidations,
|
|
41
|
+
JzodAttributeNumberWithValidations,
|
|
42
|
+
JzodAttributeStringValidations,
|
|
43
|
+
JzodAttributeStringWithValidations,
|
|
44
|
+
JzodElement,
|
|
45
|
+
JzodEnum,
|
|
46
|
+
JzodEnumTypes,
|
|
47
|
+
JzodFunction,
|
|
48
|
+
JzodEnumElementTypes,
|
|
49
|
+
JzodIntersection,
|
|
50
|
+
JzodLazy,
|
|
51
|
+
JzodLiteral,
|
|
52
|
+
JzodMap,
|
|
53
|
+
JzodObject,
|
|
54
|
+
JzodPromise,
|
|
55
|
+
JzodRecord,
|
|
56
|
+
JzodReference,
|
|
57
|
+
JzodSet,
|
|
58
|
+
JzodUnion,
|
|
59
|
+
JzodTuple,
|
|
60
|
+
} from "./JzodTsInterface";
|
package/src/tools.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { printNode } from "zod-to-ts";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { TsTypeAliases } from "./JzodToTs";
|
|
5
|
-
|
|
6
|
-
// ################################################################################################
|
|
7
|
-
export function printTsTypeAlias(
|
|
8
|
-
typeAlias: ts.TypeAliasDeclaration,
|
|
9
|
-
exportPrefix: boolean = true,
|
|
10
|
-
): string {
|
|
11
|
-
return (exportPrefix?"export ":"")+printNode(typeAlias)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// ################################################################################################
|
|
15
|
-
export function printTsTypeAliases(
|
|
16
|
-
typeAliases: TsTypeAliases,
|
|
17
|
-
exportPrefix: boolean = true,
|
|
18
|
-
): string {
|
|
19
|
-
const result = Object.entries(typeAliases).reduce((acc, curr) => {
|
|
20
|
-
// console.log("printTypeAliases ", JSON.stringify(curr));
|
|
21
|
-
return `${acc}
|
|
22
|
-
${printTsTypeAlias(curr[1],exportPrefix)}`;
|
|
23
|
-
}, "");
|
|
24
|
-
return result;
|
|
1
|
+
import { printNode } from "zod-to-ts";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { TsTypeAliases } from "./JzodToTs";
|
|
5
|
+
|
|
6
|
+
// ################################################################################################
|
|
7
|
+
export function printTsTypeAlias(
|
|
8
|
+
typeAlias: ts.TypeAliasDeclaration,
|
|
9
|
+
exportPrefix: boolean = true,
|
|
10
|
+
): string {
|
|
11
|
+
return (exportPrefix?"export ":"")+printNode(typeAlias)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ################################################################################################
|
|
15
|
+
export function printTsTypeAliases(
|
|
16
|
+
typeAliases: TsTypeAliases,
|
|
17
|
+
exportPrefix: boolean = true,
|
|
18
|
+
): string {
|
|
19
|
+
const result = Object.entries(typeAliases).reduce((acc, curr) => {
|
|
20
|
+
// console.log("printTypeAliases ", JSON.stringify(curr));
|
|
21
|
+
return `${acc}
|
|
22
|
+
${printTsTypeAlias(curr[1],exportPrefix)}`;
|
|
23
|
+
}, "");
|
|
24
|
+
return result;
|
|
25
25
|
}
|
package/tests/jzod-ts.test.ts
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
|
|
4
|
-
import { jzodBootstrapElementSchema } from "@miroir-framework/jzod";
|
|
5
|
-
import { jzodToTsCode } from "../src/JzodToTs";
|
|
6
|
-
import { JzodElement,
|
|
7
|
-
|
|
8
|
-
const refsPath = "./tests/resources"
|
|
9
|
-
const tmpPath = "./tests/tmp";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const testJzodToTs = (
|
|
13
|
-
// testDirectory: string,
|
|
14
|
-
referenceFileName: string,
|
|
15
|
-
testFileName: string,
|
|
16
|
-
testJzodSchema:JzodElement,
|
|
17
|
-
exportPrefix: boolean,
|
|
18
|
-
typeName: string
|
|
19
|
-
) => {
|
|
20
|
-
console.log("testJzodToTs:", typeName);
|
|
21
|
-
|
|
22
|
-
const testResultSchemaFilePath = path.join(tmpPath,testFileName);
|
|
23
|
-
const expectedSchemaFilePath = path.join(refsPath,referenceFileName);
|
|
24
|
-
|
|
25
|
-
const result = jzodToTsCode(testJzodSchema,exportPrefix,typeName)
|
|
26
|
-
fs.writeFileSync(testResultSchemaFilePath,result);
|
|
27
|
-
|
|
28
|
-
const resultContents = result.replace(/(\r\n|\n|\r)/gm, "");
|
|
29
|
-
// console.log("ts Type generation resultContents", resultContents);
|
|
30
|
-
|
|
31
|
-
const expectedFileContents = fs.readFileSync(expectedSchemaFilePath).toString().replace(/(\r\n|\n|\r)/gm, "")
|
|
32
|
-
expect(resultContents).toEqual(expectedFileContents);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
describe(
|
|
37
|
-
'Jzod-Ts',
|
|
38
|
-
() => {
|
|
39
|
-
// ############################################################################################
|
|
40
|
-
it(
|
|
41
|
-
"Jzod to TS Type",
|
|
42
|
-
async() => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
// ########################################################################################
|
|
46
|
-
const testJzodSchema1: JzodElement = { type: "simpleType", definition: "string" };
|
|
47
|
-
|
|
48
|
-
testJzodToTs(
|
|
49
|
-
"tsTypeGeneration-testJzodSchema1 - reference.ts",
|
|
50
|
-
"tsTypeGeneration-testJzodSchema1.ts",
|
|
51
|
-
testJzodSchema1,
|
|
52
|
-
true,
|
|
53
|
-
"testJzodSchema1"
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
// ########################################################################################
|
|
57
|
-
const testJzodSchema2: JzodElement = {
|
|
58
|
-
type: "schemaReference",
|
|
59
|
-
context: {
|
|
60
|
-
a: { type: "simpleType", definition: "string" },
|
|
61
|
-
b: {
|
|
62
|
-
type: "object",
|
|
63
|
-
definition: {
|
|
64
|
-
test: { type: "schemaReference", definition: { relativePath: "a" } }
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
definition: { relativePath: "b" },
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
testJzodToTs(
|
|
72
|
-
"tsTypeGeneration-testJzodSchema2 - reference.ts",
|
|
73
|
-
"tsTypeGeneration-testJzodSchema2.ts",
|
|
74
|
-
testJzodSchema2,
|
|
75
|
-
true,
|
|
76
|
-
"testJzodSchema2"
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
// ########################################################################################
|
|
80
|
-
const testJzodSchema4:JzodElement =
|
|
81
|
-
{
|
|
82
|
-
type: "schemaReference",
|
|
83
|
-
context: {
|
|
84
|
-
...jzodBootstrapElementSchema.context,
|
|
85
|
-
a: {
|
|
86
|
-
type: "array",
|
|
87
|
-
definition: { type: "schemaReference", definition: {relativePath: "
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
definition: {
|
|
91
|
-
relativePath: "a"
|
|
92
|
-
},
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
testJzodToTs(
|
|
96
|
-
"tsTypeGeneration-testJzodSchema4 - reference.ts",
|
|
97
|
-
"tsTypeGeneration-testJzodSchema4.ts",
|
|
98
|
-
testJzodSchema4,
|
|
99
|
-
true,
|
|
100
|
-
"testJzodSchema4"
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
|
|
4
|
+
import { jzodBootstrapElementSchema } from "@miroir-framework/jzod";
|
|
5
|
+
import { jzodToTsCode } from "../src/JzodToTs";
|
|
6
|
+
import { JzodElement, jzodElement } from "../src/JzodTsInterface";
|
|
7
|
+
|
|
8
|
+
const refsPath = "./tests/resources"
|
|
9
|
+
const tmpPath = "./tests/tmp";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const testJzodToTs = (
|
|
13
|
+
// testDirectory: string,
|
|
14
|
+
referenceFileName: string,
|
|
15
|
+
testFileName: string,
|
|
16
|
+
testJzodSchema:JzodElement,
|
|
17
|
+
exportPrefix: boolean,
|
|
18
|
+
typeName: string
|
|
19
|
+
) => {
|
|
20
|
+
console.log("testJzodToTs:", typeName);
|
|
21
|
+
|
|
22
|
+
const testResultSchemaFilePath = path.join(tmpPath,testFileName);
|
|
23
|
+
const expectedSchemaFilePath = path.join(refsPath,referenceFileName);
|
|
24
|
+
|
|
25
|
+
const result = jzodToTsCode(testJzodSchema,exportPrefix,typeName)
|
|
26
|
+
fs.writeFileSync(testResultSchemaFilePath,result);
|
|
27
|
+
|
|
28
|
+
const resultContents = result.replace(/(\r\n|\n|\r)/gm, "");
|
|
29
|
+
// console.log("ts Type generation resultContents", resultContents);
|
|
30
|
+
|
|
31
|
+
const expectedFileContents = fs.readFileSync(expectedSchemaFilePath).toString().replace(/(\r\n|\n|\r)/gm, "")
|
|
32
|
+
expect(resultContents).toEqual(expectedFileContents);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
describe(
|
|
37
|
+
'Jzod-Ts',
|
|
38
|
+
() => {
|
|
39
|
+
// ############################################################################################
|
|
40
|
+
it(
|
|
41
|
+
"Jzod to TS Type",
|
|
42
|
+
async() => {
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// ########################################################################################
|
|
46
|
+
const testJzodSchema1: JzodElement = { type: "simpleType", definition: "string" };
|
|
47
|
+
|
|
48
|
+
testJzodToTs(
|
|
49
|
+
"tsTypeGeneration-testJzodSchema1 - reference.ts",
|
|
50
|
+
"tsTypeGeneration-testJzodSchema1.ts",
|
|
51
|
+
testJzodSchema1,
|
|
52
|
+
true,
|
|
53
|
+
"testJzodSchema1"
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// ########################################################################################
|
|
57
|
+
const testJzodSchema2: JzodElement = {
|
|
58
|
+
type: "schemaReference",
|
|
59
|
+
context: {
|
|
60
|
+
a: { type: "simpleType", definition: "string" },
|
|
61
|
+
b: {
|
|
62
|
+
type: "object",
|
|
63
|
+
definition: {
|
|
64
|
+
test: { type: "schemaReference", definition: { relativePath: "a" } }
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
definition: { relativePath: "b" },
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
testJzodToTs(
|
|
72
|
+
"tsTypeGeneration-testJzodSchema2 - reference.ts",
|
|
73
|
+
"tsTypeGeneration-testJzodSchema2.ts",
|
|
74
|
+
testJzodSchema2,
|
|
75
|
+
true,
|
|
76
|
+
"testJzodSchema2"
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// ########################################################################################
|
|
80
|
+
const testJzodSchema4:JzodElement =
|
|
81
|
+
{
|
|
82
|
+
type: "schemaReference",
|
|
83
|
+
context: {
|
|
84
|
+
...jzodBootstrapElementSchema.context,
|
|
85
|
+
a: {
|
|
86
|
+
type: "array",
|
|
87
|
+
definition: { type: "schemaReference", definition: {relativePath: "jzodArray"} }
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
definition: {
|
|
91
|
+
relativePath: "a"
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
testJzodToTs(
|
|
96
|
+
"tsTypeGeneration-testJzodSchema4 - reference.ts",
|
|
97
|
+
"tsTypeGeneration-testJzodSchema4.ts",
|
|
98
|
+
testJzodSchema4,
|
|
99
|
+
true,
|
|
100
|
+
"testJzodSchema4"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
|
-
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
export const testJzodSchema1 = z.string();
|
|
1
|
+
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
|
+
|
|
3
|
+
export type TestJzodSchema1 = string;
|
|
4
|
+
|
|
5
|
+
export const testJzodSchema1: z.ZodType<TestJzodSchema1> = z.string();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
2
|
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
test?:
|
|
3
|
+
export type A = string;
|
|
4
|
+
export type B = {
|
|
5
|
+
test?: A;
|
|
6
6
|
};
|
|
7
|
-
export type
|
|
7
|
+
export type TestJzodSchema2 = B;
|
|
8
8
|
|
|
9
|
-
export const a=z.string();
|
|
10
|
-
export const b=z.object({test:z.lazy(() =>a),}).strict();
|
|
11
|
-
export const testJzodSchema2 = z.lazy(() =>b);
|
|
9
|
+
export const a:z.ZodType<A> = z.string();
|
|
10
|
+
export const b:z.ZodType<B> = z.object({test:z.lazy(() =>a),}).strict();
|
|
11
|
+
export const testJzodSchema2: z.ZodType<TestJzodSchema2> = z.lazy(() =>b);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
type
|
|
4
|
+
type jzodLiteral = {
|
|
5
5
|
optional?: boolean | undefined;
|
|
6
6
|
extra?: {
|
|
7
7
|
[x: string]: any;
|
|
@@ -9,11 +9,11 @@ type jzodLiteralSchema = {
|
|
|
9
9
|
type: "literal";
|
|
10
10
|
definition: string;
|
|
11
11
|
};
|
|
12
|
-
type
|
|
12
|
+
type jzodElement = jzodLiteral;
|
|
13
13
|
type testJzodSchema3 = {
|
|
14
|
-
b:
|
|
14
|
+
b: jzodElement[];
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
19
|
-
export const testJzodSchema3 = z.object({b:z.array(z.lazy(() =>
|
|
17
|
+
export const jzodLiteral=z.object({optional:z.boolean().optional(),extra:z.record(z.string(),z.any()).optional(),type:z.literal("literal"),definition:z.string(),}).strict();
|
|
18
|
+
export const jzodElement=z.union([z.lazy(() =>jzodLiteral),]);
|
|
19
|
+
export const testJzodSchema3 = z.object({b:z.array(z.lazy(() =>jzodElement)),}).strict();
|