@miroir-framework/jzod-ts 0.6.0 → 0.6.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/dist/bundle.d.ts +6 -2
- package/dist/bundle.js +12 -11
- package/dist/out-tsc/dts/src/JzodToTs.d.ts +2 -2
- package/dist/out-tsc/dts/src/generated_jzodBootstrapElementSchema.d.ts +250 -0
- package/dist/out-tsc/dts/src/index.d.ts +1 -1
- package/dist/src/JzodToTs.d.ts +1 -1
- package/dist/src/generated_jzodBootstrapElementSchema.d.ts +7 -3
- package/package.json +5 -4
- package/src/JzodToTs.ts +13 -9
- package/src/generated_jzodBootstrapElementSchema.ts +253 -249
- package/src/preBuild.ts +9 -8
- package/tests/jzod-ts.test.ts +4 -5
- package/tests/resources/tsTypeGeneration-testJzodSchema4 - reference.ts +17 -13
- package/dist/out-tsc/dts/src/JzodTsInterface.d.ts +0 -397
- package/dist/out-tsc/src/JzodToTs.js +0 -47
- package/dist/out-tsc/src/JzodTsInterface.js +0 -250
- package/dist/out-tsc/src/index.js +0 -34
- package/dist/out-tsc/src/preBuild.js +0 -42
- package/dist/out-tsc/src/tools.js +0 -17
- package/dist/src/JzodTsInterface.d.ts +0 -397
package/dist/bundle.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ type JzodAttributeDateWithValidations = {
|
|
|
35
35
|
} | undefined;
|
|
36
36
|
type: "simpleType";
|
|
37
37
|
definition: "date";
|
|
38
|
+
coerce?: boolean | undefined;
|
|
38
39
|
validations: JzodAttributeDateValidations[];
|
|
39
40
|
};
|
|
40
41
|
type JzodAttributeNumberValidations = {
|
|
@@ -52,6 +53,7 @@ type JzodAttributeNumberWithValidations = {
|
|
|
52
53
|
} | undefined;
|
|
53
54
|
type: "simpleType";
|
|
54
55
|
definition: "number";
|
|
56
|
+
coerce?: boolean | undefined;
|
|
55
57
|
validations: JzodAttributeNumberValidations[];
|
|
56
58
|
};
|
|
57
59
|
type JzodAttributeStringValidations = {
|
|
@@ -69,6 +71,7 @@ type JzodAttributeStringWithValidations = {
|
|
|
69
71
|
} | undefined;
|
|
70
72
|
type: "simpleType";
|
|
71
73
|
definition: "string";
|
|
74
|
+
coerce?: boolean | undefined;
|
|
72
75
|
validations: JzodAttributeStringValidations[];
|
|
73
76
|
};
|
|
74
77
|
type JzodElement = JzodArray | JzodAttribute | JzodAttributeDateWithValidations | JzodAttributeNumberWithValidations | JzodAttributeStringWithValidations | JzodEnum | JzodFunction | JzodLazy | JzodLiteral | JzodIntersection | JzodMap | JzodObject | JzodPromise | JzodRecord | JzodReference | JzodSet | JzodTuple | JzodUnion;
|
|
@@ -111,7 +114,7 @@ type JzodLiteral = {
|
|
|
111
114
|
[x: string]: any;
|
|
112
115
|
} | undefined;
|
|
113
116
|
type: "literal";
|
|
114
|
-
definition: string;
|
|
117
|
+
definition: string | number | bigint | boolean;
|
|
115
118
|
};
|
|
116
119
|
type JzodIntersection = {
|
|
117
120
|
optional?: boolean | undefined;
|
|
@@ -146,6 +149,7 @@ type JzodObject = {
|
|
|
146
149
|
extend?: (JzodReference | JzodObject) | undefined;
|
|
147
150
|
type: "object";
|
|
148
151
|
nonStrict?: boolean | undefined;
|
|
152
|
+
partial?: boolean | undefined;
|
|
149
153
|
definition: {
|
|
150
154
|
[x: string]: JzodElement;
|
|
151
155
|
};
|
|
@@ -251,7 +255,7 @@ interface TsTypeAliasesAndZodText {
|
|
|
251
255
|
mainZodText: string;
|
|
252
256
|
}
|
|
253
257
|
declare function jzodToTsTypeAliasesAndZodText(element: JzodElement, typeName?: string): TsTypeAliasesAndZodText;
|
|
254
|
-
declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string,
|
|
258
|
+
declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string, typeAnotationForSchema?: string[]): string;
|
|
255
259
|
|
|
256
260
|
declare function printTsTypeAlias(typeAlias: ts.TypeAliasDeclaration, exportPrefix?: boolean): string;
|
|
257
261
|
declare function printTsTypeAliases(typeAliases: TsTypeAliases, exportPrefix?: boolean): string;
|
package/dist/bundle.js
CHANGED
|
@@ -17,7 +17,7 @@ function jzodToTsTypeAliasesAndZodText(element, typeName) {
|
|
|
17
17
|
var _a, _b;
|
|
18
18
|
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(element, () => ({}), () => ({}), (innerReference, relativeReference) => withGetType(innerReference, (ts) => {
|
|
19
19
|
const actualTypeName = relativeReference ? relativeReference.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c) : "";
|
|
20
|
-
return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(actualTypeName ? actualTypeName : "RELATIVEPATH_NOT_DEFINED"), undefined);
|
|
20
|
+
return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(actualTypeName !== null && actualTypeName !== void 0 ? actualTypeName : "RELATIVEPATH_NOT_DEFINED"), undefined);
|
|
21
21
|
}));
|
|
22
22
|
const contextTsTypesString = Object.fromEntries(Object.entries((_a = elementZodSchemaAndDescription.contextZodSchema) !== null && _a !== void 0 ? _a : {}).map((curr) => {
|
|
23
23
|
const actualTypeName = curr[0] ? curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c) : "";
|
|
@@ -34,22 +34,23 @@ function jzodToTsTypeAliasesAndZodText(element, typeName) {
|
|
|
34
34
|
mainTsTypeAlias: tsTypeStringTypeAlias,
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
function jzodToTsCode(jzodElement, exportPrefix = true, typeName,
|
|
37
|
+
function jzodToTsCode(jzodElement, exportPrefix = true, typeName, typeAnotationForSchema = []) {
|
|
38
38
|
const schemaName = typeName ? typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c) : "";
|
|
39
39
|
const actualTypeName = typeName ? typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c) : "";
|
|
40
40
|
const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
|
|
41
41
|
const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, actualTypeName);
|
|
42
|
-
const bodyJsCode =
|
|
42
|
+
const bodyJsCode = typeAnotationForSchema.includes(schemaName !== null && schemaName !== void 0 ? schemaName : "")
|
|
43
43
|
? `export const ${schemaName}: z.ZodType<${actualTypeName}> = ${typeAliasesAndZodText.mainZodText};`
|
|
44
44
|
: `export const ${schemaName} = ${typeAliasesAndZodText.mainZodText};`;
|
|
45
45
|
const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
|
|
46
46
|
const contextJsCode = typeAliasesAndZodText.contextZodText
|
|
47
47
|
? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
|
|
48
48
|
const contextTypeName = curr[0] ? curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c) : "";
|
|
49
|
-
return
|
|
50
|
-
`${acc}
|
|
51
|
-
export const ${curr[0]}: z.ZodType<${contextTypeName}> = ${curr[1]};` :
|
|
49
|
+
return typeAnotationForSchema.includes(curr[0]) ?
|
|
52
50
|
`${acc}
|
|
51
|
+
export const ${curr[0]}: z.ZodType<${contextTypeName}> = ${curr[1]};`
|
|
52
|
+
:
|
|
53
|
+
`${acc}
|
|
53
54
|
export const ${curr[0]} = ${curr[1]};`;
|
|
54
55
|
}, "")
|
|
55
56
|
: "";
|
|
@@ -66,21 +67,21 @@ z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), e
|
|
|
66
67
|
const jzodArray = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("array"), definition: z.lazy(() => jzodElement) }).strict();
|
|
67
68
|
const jzodAttribute = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), coerce: z.boolean().optional(), definition: z.lazy(() => jzodEnumAttributeTypes) }).strict();
|
|
68
69
|
const jzodAttributeDateValidations = z.object({ extra: z.record(z.string(), z.any()).optional(), type: z.enum(["min", "max"]), parameter: z.any() }).strict();
|
|
69
|
-
const jzodAttributeDateWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("date"), validations: z.array(z.lazy(() => jzodAttributeDateValidations)) }).strict();
|
|
70
|
+
const jzodAttributeDateWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("date"), coerce: z.boolean().optional(), validations: z.array(z.lazy(() => jzodAttributeDateValidations)) }).strict();
|
|
70
71
|
const jzodAttributeNumberValidations = z.object({ extra: z.record(z.string(), z.any()).optional(), type: z.enum(["gt", "gte", "lt", "lte", "int", "positive", "nonpositive", "negative", "nonnegative", "multipleOf", "finite", "safe"]), parameter: z.any() }).strict();
|
|
71
|
-
const jzodAttributeNumberWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("number"), validations: z.array(z.lazy(() => jzodAttributeNumberValidations)) }).strict();
|
|
72
|
+
const jzodAttributeNumberWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("number"), coerce: z.boolean().optional(), validations: z.array(z.lazy(() => jzodAttributeNumberValidations)) }).strict();
|
|
72
73
|
const jzodAttributeStringValidations = z.object({ extra: z.record(z.string(), z.any()).optional(), type: z.enum(["max", "min", "length", "email", "url", "emoji", "uuid", "cuid", "cuid2", "ulid", "regex", "includes", "startsWith", "endsWith", "datetime", "ip"]), parameter: z.any() }).strict();
|
|
73
|
-
const jzodAttributeStringWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("string"), validations: z.array(z.lazy(() => jzodAttributeStringValidations)) }).strict();
|
|
74
|
+
const jzodAttributeStringWithValidations = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("simpleType"), definition: z.literal("string"), coerce: z.boolean().optional(), validations: z.array(z.lazy(() => jzodAttributeStringValidations)) }).strict();
|
|
74
75
|
const jzodElement = z.union([z.lazy(() => jzodArray), z.lazy(() => jzodAttribute), z.lazy(() => jzodAttributeDateWithValidations), z.lazy(() => jzodAttributeNumberWithValidations), z.lazy(() => jzodAttributeStringWithValidations), z.lazy(() => jzodEnum), z.lazy(() => jzodFunction), z.lazy(() => jzodLazy), z.lazy(() => jzodLiteral), z.lazy(() => jzodIntersection), z.lazy(() => jzodMap), z.lazy(() => jzodObject), z.lazy(() => jzodPromise), z.lazy(() => jzodRecord), z.lazy(() => jzodReference), z.lazy(() => jzodSet), z.lazy(() => jzodTuple), z.lazy(() => jzodUnion)]);
|
|
75
76
|
const jzodEnum = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("enum"), definition: z.array(z.string()) }).strict();
|
|
76
77
|
const jzodEnumAttributeTypes = z.enum(["any", "bigint", "boolean", "date", "never", "null", "number", "string", "uuid", "undefined", "unknown", "void"]);
|
|
77
78
|
const jzodEnumElementTypes = z.enum(["array", "enum", "function", "lazy", "literal", "intersection", "map", "object", "promise", "record", "schemaReference", "set", "simpleType", "tuple", "union"]);
|
|
78
79
|
const jzodFunction = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("function"), definition: z.object({ args: z.array(z.lazy(() => jzodElement)), returns: z.lazy(() => jzodElement).optional() }).strict() }).strict();
|
|
79
80
|
const jzodLazy = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("lazy"), definition: z.lazy(() => jzodFunction) }).strict();
|
|
80
|
-
const jzodLiteral = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("literal"), definition: z.string() }).strict();
|
|
81
|
+
const jzodLiteral = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("literal"), definition: z.union([z.string(), z.number(), z.bigint(), z.boolean()]) }).strict();
|
|
81
82
|
const jzodIntersection = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("intersection"), definition: z.object({ left: z.lazy(() => jzodElement), right: z.lazy(() => jzodElement) }).strict() }).strict();
|
|
82
83
|
const jzodMap = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("map"), definition: z.tuple([z.lazy(() => jzodElement), z.lazy(() => jzodElement)]) }).strict();
|
|
83
|
-
const jzodObject = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ extend: z.union([z.lazy(() => jzodReference), z.lazy(() => jzodObject)]).optional(), type: z.literal("object"), nonStrict: z.boolean().optional(), definition: z.record(z.string(), z.lazy(() => jzodElement)) }).strict();
|
|
84
|
+
const jzodObject = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ extend: z.union([z.lazy(() => jzodReference), z.lazy(() => jzodObject)]).optional(), type: z.literal("object"), nonStrict: z.boolean().optional(), partial: z.boolean().optional(), definition: z.record(z.string(), z.lazy(() => jzodElement)) }).strict();
|
|
84
85
|
const jzodPromise = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("promise"), definition: z.lazy(() => jzodElement) }).strict();
|
|
85
86
|
const jzodRecord = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("record"), definition: z.lazy(() => jzodElement) }).strict();
|
|
86
87
|
const jzodReference = z.object({ optional: z.boolean().optional(), nullable: z.boolean().optional(), extra: z.record(z.string(), z.any()).optional() }).strict().extend({ type: z.literal("schemaReference"), context: z.record(z.string(), z.lazy(() => jzodElement)).optional(), definition: z.object({ eager: z.boolean().optional(), relativePath: z.string().optional(), absolutePath: z.string().optional() }).strict() }).strict();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import { JzodElement } from "./
|
|
2
|
+
import { JzodElement } from "./generated_jzodBootstrapElementSchema";
|
|
3
3
|
export type TsTypeAliases = {
|
|
4
4
|
[k: string]: ts.TypeAliasDeclaration;
|
|
5
5
|
};
|
|
@@ -14,4 +14,4 @@ export interface TsTypeAliasesAndZodText {
|
|
|
14
14
|
mainZodText: string;
|
|
15
15
|
}
|
|
16
16
|
export declare function jzodToTsTypeAliasesAndZodText(element: JzodElement, typeName?: string): TsTypeAliasesAndZodText;
|
|
17
|
-
export declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string): string;
|
|
17
|
+
export declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string, typeAnotationForSchema?: string[]): string;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
|
+
export type JzodBaseObject = {
|
|
3
|
+
optional?: boolean | undefined;
|
|
4
|
+
nullable?: boolean | undefined;
|
|
5
|
+
extra?: {
|
|
6
|
+
[x: string]: any;
|
|
7
|
+
} | undefined;
|
|
8
|
+
};
|
|
9
|
+
export type JzodArray = {
|
|
10
|
+
optional?: boolean | undefined;
|
|
11
|
+
nullable?: boolean | undefined;
|
|
12
|
+
extra?: {
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
} | undefined;
|
|
15
|
+
type: "array";
|
|
16
|
+
definition: JzodElement;
|
|
17
|
+
};
|
|
18
|
+
export type JzodAttribute = {
|
|
19
|
+
optional?: boolean | undefined;
|
|
20
|
+
nullable?: boolean | undefined;
|
|
21
|
+
extra?: {
|
|
22
|
+
[x: string]: any;
|
|
23
|
+
} | undefined;
|
|
24
|
+
type: "simpleType";
|
|
25
|
+
coerce?: boolean | undefined;
|
|
26
|
+
definition: JzodEnumAttributeTypes;
|
|
27
|
+
};
|
|
28
|
+
export type JzodAttributeDateValidations = {
|
|
29
|
+
extra?: {
|
|
30
|
+
[x: string]: any;
|
|
31
|
+
} | undefined;
|
|
32
|
+
type: "min" | "max";
|
|
33
|
+
parameter?: any;
|
|
34
|
+
};
|
|
35
|
+
export type JzodAttributeDateWithValidations = {
|
|
36
|
+
optional?: boolean | undefined;
|
|
37
|
+
nullable?: boolean | undefined;
|
|
38
|
+
extra?: {
|
|
39
|
+
[x: string]: any;
|
|
40
|
+
} | undefined;
|
|
41
|
+
type: "simpleType";
|
|
42
|
+
definition: "date";
|
|
43
|
+
coerce?: boolean | undefined;
|
|
44
|
+
validations: JzodAttributeDateValidations[];
|
|
45
|
+
};
|
|
46
|
+
export type JzodAttributeNumberValidations = {
|
|
47
|
+
extra?: {
|
|
48
|
+
[x: string]: any;
|
|
49
|
+
} | undefined;
|
|
50
|
+
type: "gt" | "gte" | "lt" | "lte" | "int" | "positive" | "nonpositive" | "negative" | "nonnegative" | "multipleOf" | "finite" | "safe";
|
|
51
|
+
parameter?: any;
|
|
52
|
+
};
|
|
53
|
+
export type JzodAttributeNumberWithValidations = {
|
|
54
|
+
optional?: boolean | undefined;
|
|
55
|
+
nullable?: boolean | undefined;
|
|
56
|
+
extra?: {
|
|
57
|
+
[x: string]: any;
|
|
58
|
+
} | undefined;
|
|
59
|
+
type: "simpleType";
|
|
60
|
+
definition: "number";
|
|
61
|
+
coerce?: boolean | undefined;
|
|
62
|
+
validations: JzodAttributeNumberValidations[];
|
|
63
|
+
};
|
|
64
|
+
export type JzodAttributeStringValidations = {
|
|
65
|
+
extra?: {
|
|
66
|
+
[x: string]: any;
|
|
67
|
+
} | undefined;
|
|
68
|
+
type: "max" | "min" | "length" | "email" | "url" | "emoji" | "uuid" | "cuid" | "cuid2" | "ulid" | "regex" | "includes" | "startsWith" | "endsWith" | "datetime" | "ip";
|
|
69
|
+
parameter?: any;
|
|
70
|
+
};
|
|
71
|
+
export type JzodAttributeStringWithValidations = {
|
|
72
|
+
optional?: boolean | undefined;
|
|
73
|
+
nullable?: boolean | undefined;
|
|
74
|
+
extra?: {
|
|
75
|
+
[x: string]: any;
|
|
76
|
+
} | undefined;
|
|
77
|
+
type: "simpleType";
|
|
78
|
+
definition: "string";
|
|
79
|
+
coerce?: boolean | undefined;
|
|
80
|
+
validations: JzodAttributeStringValidations[];
|
|
81
|
+
};
|
|
82
|
+
export type JzodElement = JzodArray | JzodAttribute | JzodAttributeDateWithValidations | JzodAttributeNumberWithValidations | JzodAttributeStringWithValidations | JzodEnum | JzodFunction | JzodLazy | JzodLiteral | JzodIntersection | JzodMap | JzodObject | JzodPromise | JzodRecord | JzodReference | JzodSet | JzodTuple | JzodUnion;
|
|
83
|
+
export type JzodEnum = {
|
|
84
|
+
optional?: boolean | undefined;
|
|
85
|
+
nullable?: boolean | undefined;
|
|
86
|
+
extra?: {
|
|
87
|
+
[x: string]: any;
|
|
88
|
+
} | undefined;
|
|
89
|
+
type: "enum";
|
|
90
|
+
definition: string[];
|
|
91
|
+
};
|
|
92
|
+
export type JzodEnumAttributeTypes = "any" | "bigint" | "boolean" | "date" | "never" | "null" | "number" | "string" | "uuid" | "undefined" | "unknown" | "void";
|
|
93
|
+
export type JzodEnumElementTypes = "array" | "enum" | "function" | "lazy" | "literal" | "intersection" | "map" | "object" | "promise" | "record" | "schemaReference" | "set" | "simpleType" | "tuple" | "union";
|
|
94
|
+
export type JzodFunction = {
|
|
95
|
+
optional?: boolean | undefined;
|
|
96
|
+
nullable?: boolean | undefined;
|
|
97
|
+
extra?: {
|
|
98
|
+
[x: string]: any;
|
|
99
|
+
} | undefined;
|
|
100
|
+
type: "function";
|
|
101
|
+
definition: {
|
|
102
|
+
args: JzodElement[];
|
|
103
|
+
returns?: JzodElement | undefined;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export type JzodLazy = {
|
|
107
|
+
optional?: boolean | undefined;
|
|
108
|
+
nullable?: boolean | undefined;
|
|
109
|
+
extra?: {
|
|
110
|
+
[x: string]: any;
|
|
111
|
+
} | undefined;
|
|
112
|
+
type: "lazy";
|
|
113
|
+
definition: JzodFunction;
|
|
114
|
+
};
|
|
115
|
+
export type JzodLiteral = {
|
|
116
|
+
optional?: boolean | undefined;
|
|
117
|
+
nullable?: boolean | undefined;
|
|
118
|
+
extra?: {
|
|
119
|
+
[x: string]: any;
|
|
120
|
+
} | undefined;
|
|
121
|
+
type: "literal";
|
|
122
|
+
definition: string | number | bigint | boolean;
|
|
123
|
+
};
|
|
124
|
+
export type JzodIntersection = {
|
|
125
|
+
optional?: boolean | undefined;
|
|
126
|
+
nullable?: boolean | undefined;
|
|
127
|
+
extra?: {
|
|
128
|
+
[x: string]: any;
|
|
129
|
+
} | undefined;
|
|
130
|
+
type: "intersection";
|
|
131
|
+
definition: {
|
|
132
|
+
left: JzodElement;
|
|
133
|
+
right: JzodElement;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
export type JzodMap = {
|
|
137
|
+
optional?: boolean | undefined;
|
|
138
|
+
nullable?: boolean | undefined;
|
|
139
|
+
extra?: {
|
|
140
|
+
[x: string]: any;
|
|
141
|
+
} | undefined;
|
|
142
|
+
type: "map";
|
|
143
|
+
definition: [
|
|
144
|
+
JzodElement,
|
|
145
|
+
JzodElement
|
|
146
|
+
];
|
|
147
|
+
};
|
|
148
|
+
export type JzodObject = {
|
|
149
|
+
optional?: boolean | undefined;
|
|
150
|
+
nullable?: boolean | undefined;
|
|
151
|
+
extra?: {
|
|
152
|
+
[x: string]: any;
|
|
153
|
+
} | undefined;
|
|
154
|
+
extend?: (JzodReference | JzodObject) | undefined;
|
|
155
|
+
type: "object";
|
|
156
|
+
nonStrict?: boolean | undefined;
|
|
157
|
+
partial?: boolean | undefined;
|
|
158
|
+
definition: {
|
|
159
|
+
[x: string]: JzodElement;
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
export type JzodPromise = {
|
|
163
|
+
optional?: boolean | undefined;
|
|
164
|
+
nullable?: boolean | undefined;
|
|
165
|
+
extra?: {
|
|
166
|
+
[x: string]: any;
|
|
167
|
+
} | undefined;
|
|
168
|
+
type: "promise";
|
|
169
|
+
definition: JzodElement;
|
|
170
|
+
};
|
|
171
|
+
export type JzodRecord = {
|
|
172
|
+
optional?: boolean | undefined;
|
|
173
|
+
nullable?: boolean | undefined;
|
|
174
|
+
extra?: {
|
|
175
|
+
[x: string]: any;
|
|
176
|
+
} | undefined;
|
|
177
|
+
type: "record";
|
|
178
|
+
definition: JzodElement;
|
|
179
|
+
};
|
|
180
|
+
export type JzodReference = {
|
|
181
|
+
optional?: boolean | undefined;
|
|
182
|
+
nullable?: boolean | undefined;
|
|
183
|
+
extra?: {
|
|
184
|
+
[x: string]: any;
|
|
185
|
+
} | undefined;
|
|
186
|
+
type: "schemaReference";
|
|
187
|
+
context?: {
|
|
188
|
+
[x: string]: JzodElement;
|
|
189
|
+
} | undefined;
|
|
190
|
+
definition: {
|
|
191
|
+
eager?: boolean | undefined;
|
|
192
|
+
relativePath?: string | undefined;
|
|
193
|
+
absolutePath?: string | undefined;
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
export type JzodSet = {
|
|
197
|
+
optional?: boolean | undefined;
|
|
198
|
+
nullable?: boolean | undefined;
|
|
199
|
+
extra?: {
|
|
200
|
+
[x: string]: any;
|
|
201
|
+
} | undefined;
|
|
202
|
+
type: "set";
|
|
203
|
+
definition: JzodElement;
|
|
204
|
+
};
|
|
205
|
+
export type JzodTuple = {
|
|
206
|
+
optional?: boolean | undefined;
|
|
207
|
+
nullable?: boolean | undefined;
|
|
208
|
+
extra?: {
|
|
209
|
+
[x: string]: any;
|
|
210
|
+
} | undefined;
|
|
211
|
+
type: "tuple";
|
|
212
|
+
definition: JzodElement[];
|
|
213
|
+
};
|
|
214
|
+
export type JzodUnion = {
|
|
215
|
+
optional?: boolean | undefined;
|
|
216
|
+
nullable?: boolean | undefined;
|
|
217
|
+
extra?: {
|
|
218
|
+
[x: string]: any;
|
|
219
|
+
} | undefined;
|
|
220
|
+
type: "union";
|
|
221
|
+
discriminator?: string | undefined;
|
|
222
|
+
definition: JzodElement[];
|
|
223
|
+
};
|
|
224
|
+
export type JzodBootstrapElementSchema = JzodElement;
|
|
225
|
+
export declare const jzodBaseObject: z.ZodType<JzodBaseObject>;
|
|
226
|
+
export declare const jzodArray: z.ZodType<JzodArray>;
|
|
227
|
+
export declare const jzodAttribute: z.ZodType<JzodAttribute>;
|
|
228
|
+
export declare const jzodAttributeDateValidations: z.ZodType<JzodAttributeDateValidations>;
|
|
229
|
+
export declare const jzodAttributeDateWithValidations: z.ZodType<JzodAttributeDateWithValidations>;
|
|
230
|
+
export declare const jzodAttributeNumberValidations: z.ZodType<JzodAttributeNumberValidations>;
|
|
231
|
+
export declare const jzodAttributeNumberWithValidations: z.ZodType<JzodAttributeNumberWithValidations>;
|
|
232
|
+
export declare const jzodAttributeStringValidations: z.ZodType<JzodAttributeStringValidations>;
|
|
233
|
+
export declare const jzodAttributeStringWithValidations: z.ZodType<JzodAttributeStringWithValidations>;
|
|
234
|
+
export declare const jzodElement: z.ZodType<JzodElement>;
|
|
235
|
+
export declare const jzodEnum: z.ZodType<JzodEnum>;
|
|
236
|
+
export declare const jzodEnumAttributeTypes: z.ZodType<JzodEnumAttributeTypes>;
|
|
237
|
+
export declare const jzodEnumElementTypes: z.ZodType<JzodEnumElementTypes>;
|
|
238
|
+
export declare const jzodFunction: z.ZodType<JzodFunction>;
|
|
239
|
+
export declare const jzodLazy: z.ZodType<JzodLazy>;
|
|
240
|
+
export declare const jzodLiteral: z.ZodType<JzodLiteral>;
|
|
241
|
+
export declare const jzodIntersection: z.ZodType<JzodIntersection>;
|
|
242
|
+
export declare const jzodMap: z.ZodType<JzodMap>;
|
|
243
|
+
export declare const jzodObject: z.ZodType<JzodObject>;
|
|
244
|
+
export declare const jzodPromise: z.ZodType<JzodPromise>;
|
|
245
|
+
export declare const jzodRecord: z.ZodType<JzodRecord>;
|
|
246
|
+
export declare const jzodReference: z.ZodType<JzodReference>;
|
|
247
|
+
export declare const jzodSet: z.ZodType<JzodSet>;
|
|
248
|
+
export declare const jzodTuple: z.ZodType<JzodTuple>;
|
|
249
|
+
export declare const jzodUnion: z.ZodType<JzodUnion>;
|
|
250
|
+
export declare const jzodBootstrapElementSchema: z.ZodLazy<ZodType<JzodElement, z.ZodTypeDef, JzodElement>>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
|
|
2
2
|
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
3
|
-
export { jzodArray, jzodAttribute, jzodAttributeDateValidations, jzodAttributeDateWithValidations, jzodAttributeNumberValidations, jzodAttributeNumberWithValidations, jzodAttributeStringValidations, jzodAttributeStringWithValidations, jzodElement, jzodEnumAttributeTypes, jzodEnumElementTypes, jzodEnum, jzodFunction, jzodIntersection, jzodLazy, jzodLiteral,
|
|
3
|
+
export { jzodArray, jzodAttribute, jzodAttributeDateValidations, jzodAttributeDateWithValidations, jzodAttributeNumberValidations, jzodAttributeNumberWithValidations, jzodAttributeStringValidations, jzodAttributeStringWithValidations, jzodElement, jzodEnumAttributeTypes, jzodEnumElementTypes, jzodEnum, jzodFunction, jzodIntersection, jzodLazy, jzodLiteral, jzodMap, jzodObject, jzodPromise, jzodRecord, jzodReference, jzodSet, jzodTuple, jzodUnion, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumAttributeTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./generated_jzodBootstrapElementSchema";
|
package/dist/src/JzodToTs.d.ts
CHANGED
|
@@ -14,4 +14,4 @@ export interface TsTypeAliasesAndZodText {
|
|
|
14
14
|
mainZodText: string;
|
|
15
15
|
}
|
|
16
16
|
export declare function jzodToTsTypeAliasesAndZodText(element: JzodElement, typeName?: string): TsTypeAliasesAndZodText;
|
|
17
|
-
export declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string,
|
|
17
|
+
export declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string, typeAnotationForSchema?: string[]): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
2
|
export type JzodBaseObject = {
|
|
3
3
|
optional?: boolean | undefined;
|
|
4
4
|
nullable?: boolean | undefined;
|
|
@@ -40,6 +40,7 @@ export type JzodAttributeDateWithValidations = {
|
|
|
40
40
|
} | undefined;
|
|
41
41
|
type: "simpleType";
|
|
42
42
|
definition: "date";
|
|
43
|
+
coerce?: boolean | undefined;
|
|
43
44
|
validations: JzodAttributeDateValidations[];
|
|
44
45
|
};
|
|
45
46
|
export type JzodAttributeNumberValidations = {
|
|
@@ -57,6 +58,7 @@ export type JzodAttributeNumberWithValidations = {
|
|
|
57
58
|
} | undefined;
|
|
58
59
|
type: "simpleType";
|
|
59
60
|
definition: "number";
|
|
61
|
+
coerce?: boolean | undefined;
|
|
60
62
|
validations: JzodAttributeNumberValidations[];
|
|
61
63
|
};
|
|
62
64
|
export type JzodAttributeStringValidations = {
|
|
@@ -74,6 +76,7 @@ export type JzodAttributeStringWithValidations = {
|
|
|
74
76
|
} | undefined;
|
|
75
77
|
type: "simpleType";
|
|
76
78
|
definition: "string";
|
|
79
|
+
coerce?: boolean | undefined;
|
|
77
80
|
validations: JzodAttributeStringValidations[];
|
|
78
81
|
};
|
|
79
82
|
export type JzodElement = JzodArray | JzodAttribute | JzodAttributeDateWithValidations | JzodAttributeNumberWithValidations | JzodAttributeStringWithValidations | JzodEnum | JzodFunction | JzodLazy | JzodLiteral | JzodIntersection | JzodMap | JzodObject | JzodPromise | JzodRecord | JzodReference | JzodSet | JzodTuple | JzodUnion;
|
|
@@ -116,7 +119,7 @@ export type JzodLiteral = {
|
|
|
116
119
|
[x: string]: any;
|
|
117
120
|
} | undefined;
|
|
118
121
|
type: "literal";
|
|
119
|
-
definition: string;
|
|
122
|
+
definition: string | number | bigint | boolean;
|
|
120
123
|
};
|
|
121
124
|
export type JzodIntersection = {
|
|
122
125
|
optional?: boolean | undefined;
|
|
@@ -151,6 +154,7 @@ export type JzodObject = {
|
|
|
151
154
|
extend?: (JzodReference | JzodObject) | undefined;
|
|
152
155
|
type: "object";
|
|
153
156
|
nonStrict?: boolean | undefined;
|
|
157
|
+
partial?: boolean | undefined;
|
|
154
158
|
definition: {
|
|
155
159
|
[x: string]: JzodElement;
|
|
156
160
|
};
|
|
@@ -243,4 +247,4 @@ export declare const jzodReference: z.ZodType<JzodReference>;
|
|
|
243
247
|
export declare const jzodSet: z.ZodType<JzodSet>;
|
|
244
248
|
export declare const jzodTuple: z.ZodType<JzodTuple>;
|
|
245
249
|
export declare const jzodUnion: z.ZodType<JzodUnion>;
|
|
246
|
-
export declare const jzodBootstrapElementSchema: z.ZodType<
|
|
250
|
+
export declare const jzodBootstrapElementSchema: z.ZodLazy<ZodType<JzodElement, z.ZodTypeDef, JzodElement>>;
|
package/package.json
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miroir-framework/jzod-ts",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.2",
|
|
5
5
|
"description": "The Typescript-related functionalities of Jzod.",
|
|
6
6
|
"main": "dist/bundle.js",
|
|
7
7
|
"types": "dist/bundle.d.ts",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"preBuild": "
|
|
10
|
+
"preBuild": "tsc --rootDir . --baseUrl . --module ESnext --target ES2017 --moduleResolution node --allowSyntheticDefaultImports --outdir ./tmp/ && node ./tmp/src/preBuild.js",
|
|
11
|
+
"preBuildRun": "node ./tmp/src/preBuild.js",
|
|
11
12
|
"compile": "npx tsc",
|
|
12
13
|
"build": "rollup --config",
|
|
13
14
|
"test": "cross-env NODE_ENV=test jest --verbose --silent=false -t"
|
|
14
15
|
},
|
|
15
|
-
"author": "
|
|
16
|
+
"author": "Miroir Framework <miroir-framework@protonmail.com>",
|
|
16
17
|
"homepage": "https://github.com/miroir-framework/jzod/#readme",
|
|
17
18
|
"bugs": "https://github.com/miroir-framework/jzod/issues",
|
|
18
19
|
"license": "MIT",
|
|
19
20
|
"dependencies": {
|
|
20
|
-
"@miroir-framework/jzod": "0.6.
|
|
21
|
+
"@miroir-framework/jzod": "0.6.2",
|
|
21
22
|
"zod": "^3.22.2",
|
|
22
23
|
"zod-to-ts": "^1.2.0"
|
|
23
24
|
},
|
package/src/JzodToTs.ts
CHANGED
|
@@ -27,14 +27,14 @@ export function jzodToTsTypeAliasesAndZodText(
|
|
|
27
27
|
): TsTypeAliasesAndZodText
|
|
28
28
|
{
|
|
29
29
|
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
|
|
30
|
-
element,
|
|
30
|
+
element as any,
|
|
31
31
|
() => ({}),
|
|
32
32
|
() => ({}),
|
|
33
33
|
(innerReference: ZodTypeAny & GetType, relativeReference: string | undefined) =>
|
|
34
34
|
withGetType(innerReference, (ts:any) => {
|
|
35
35
|
const actualTypeName = relativeReference?relativeReference.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
36
36
|
return ts.factory.createTypeReferenceNode(
|
|
37
|
-
ts.factory.createIdentifier(actualTypeName
|
|
37
|
+
ts.factory.createIdentifier(actualTypeName ?? "RELATIVEPATH_NOT_DEFINED"),
|
|
38
38
|
undefined
|
|
39
39
|
);
|
|
40
40
|
}
|
|
@@ -67,7 +67,8 @@ export function jzodToTsCode (
|
|
|
67
67
|
jzodElement: JzodElement,
|
|
68
68
|
exportPrefix: boolean = true,
|
|
69
69
|
typeName?: string,
|
|
70
|
-
typeSchema: boolean = false,
|
|
70
|
+
// typeSchema: boolean = false,
|
|
71
|
+
typeAnotationForSchema: string[] = [],
|
|
71
72
|
): string {
|
|
72
73
|
// console.log(
|
|
73
74
|
// "################################### jzodToTsCode ",
|
|
@@ -77,13 +78,14 @@ export function jzodToTsCode (
|
|
|
77
78
|
|
|
78
79
|
const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
|
|
79
80
|
const actualTypeName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
80
|
-
|
|
81
|
+
|
|
82
|
+
// console.log("jzodToTsCode typeAnotationForSchema", typeAnotationForSchema);
|
|
81
83
|
|
|
82
84
|
const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
|
|
83
85
|
|
|
84
86
|
const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, actualTypeName);
|
|
85
87
|
|
|
86
|
-
const bodyJsCode =
|
|
88
|
+
const bodyJsCode = typeAnotationForSchema.includes(schemaName??"")
|
|
87
89
|
? `export const ${schemaName}: z.ZodType<${actualTypeName}> = ${typeAliasesAndZodText.mainZodText};`
|
|
88
90
|
: `export const ${schemaName} = ${typeAliasesAndZodText.mainZodText};`;
|
|
89
91
|
|
|
@@ -93,12 +95,14 @@ export function jzodToTsCode (
|
|
|
93
95
|
const contextJsCode = typeAliasesAndZodText.contextZodText
|
|
94
96
|
? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
|
|
95
97
|
const contextTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
96
|
-
return
|
|
98
|
+
return typeAnotationForSchema.includes(curr[0])?
|
|
97
99
|
`${acc}
|
|
98
|
-
export const ${curr[0]}: z.ZodType<${contextTypeName}> = ${curr[1]}
|
|
100
|
+
export const ${curr[0]}: z.ZodType<${contextTypeName}> = ${curr[1]};`
|
|
101
|
+
:
|
|
99
102
|
`${acc}
|
|
100
|
-
export const ${curr[0]} = ${curr[1]}
|
|
101
|
-
|
|
103
|
+
export const ${curr[0]} = ${curr[1]};`
|
|
104
|
+
;
|
|
105
|
+
}, "")
|
|
102
106
|
: ""
|
|
103
107
|
;
|
|
104
108
|
|