@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/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
- jzodArraySchema,
12
- jzodAttributeSchema,
13
- jzodAttributeDateValidationsSchema,
14
- jzodAttributeDateWithValidationsSchema,
15
- jzodAttributeNumberValidationsSchema,
16
- jzodAttributeNumberWithValidationsSchema,
17
- jzodAttributeStringValidationsSchema,
18
- jzodAttributeStringWithValidationsSchema,
19
- jzodElementSchema,
20
- jzodEnumAttributeTypesSchema,
21
- jzodEnumElementTypesSchema,
22
- jzodEnumSchema,
23
- jzodFunctionSchema,
24
- jzodIntersectionSchema,
25
- jzodLazySchema,
26
- jzodLiteralSchema,
27
- jzodMapSchema,
28
- jzodObjectSchema,
29
- jzodPromiseSchema,
30
- jzodRecordSchema,
31
- jzodReferenceSchema,
32
- jzodSetSchema,
33
- jzodTupleSchema,
34
- jzodUnionSchema,
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
  }
@@ -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, jzodElementSchema } 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: "jzodArraySchema"} }
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 testJzodSchema1 = string;
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 a = string;
4
- export type b = {
5
- test?: a;
3
+ export type A = string;
4
+ export type B = {
5
+ test?: A;
6
6
  };
7
- export type testJzodSchema2 = b;
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 jzodLiteralSchema = {
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 jzodElementSchema = jzodLiteralSchema;
12
+ type jzodElement = jzodLiteral;
13
13
  type testJzodSchema3 = {
14
- b: jzodElementSchema[];
14
+ b: jzodElement[];
15
15
  };
16
16
 
17
- export const jzodLiteralSchema=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 jzodElementSchema=z.union([z.lazy(() =>jzodLiteralSchema),]);
19
- export const testJzodSchema3 = z.object({b:z.array(z.lazy(() =>jzodElementSchema)),}).strict();
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();