Skip to content

Commit 093cf94

Browse files
authored
Improve Zod enum handling with nativeEnum and JSDoc comments (#20)
- Use z.nativeEnum with const objects instead of z.literal/z.union - Add JSDoc comments for enum types with descriptions - Add helper methods for writing formatted comments - Fix type constraint from ZodTypeAny to ZodType for generics
1 parent cc5d35b commit 093cf94

File tree

1 file changed

+70
-7
lines changed
  • tools/EVA.SDK.Generator.V2/Commands/Generate/Outputs/zod

1 file changed

+70
-7
lines changed

tools/EVA.SDK.Generator.V2/Commands/Generate/Outputs/zod/ZodOutput.cs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,56 @@ private static void WriteType(string id, TypeSpecification type, IndentedStringB
4949
}
5050
else
5151
{
52-
var values = type.EnumValues.ToTotals().Select(x => x.value).Order().Select(x => $"z.literal({x})");
53-
54-
if (values.Count() == 1)
52+
var enumTypeName = TypeNameToTypescriptTypeName(input, id, null);
53+
var constName = enumTypeName.Replace("Schema", "");
54+
55+
var enumValues = type.EnumValues.ToTotals().OrderBy(x => x.value).ToList();
56+
57+
// Add JSDoc comment for the const object
58+
o.WriteLine("/**");
59+
if (type.Description != null)
5560
{
56-
o.WriteLine($"export const {TypeNameToTypescriptTypeName(input, id, null)} = {values.First()};");
61+
WriteCommentLines(o, type.Description);
62+
if (enumValues.Any())
63+
{
64+
o.WriteLine(" *");
65+
}
5766
}
58-
else
67+
if (enumValues.Any())
5968
{
60-
o.WriteLine($"export const {TypeNameToTypescriptTypeName(input, id, null)} = z.union([{string.Join(", ", values)}]);");
69+
o.WriteLine(" * @enum");
70+
foreach (var (value, name, description) in enumValues)
71+
{
72+
if (description != null)
73+
{
74+
o.WriteLine($" * @property {name} ({value}) - {description}");
75+
}
76+
else
77+
{
78+
o.WriteLine($" * @property {name} ({value})");
79+
}
80+
}
81+
}
82+
o.WriteLine(" */");
83+
84+
// Generate the const object
85+
o.WriteLine($"export const {constName} = {{");
86+
using (o.Indentation)
87+
{
88+
foreach (var (value, name, description) in enumValues)
89+
{
90+
if (description != null)
91+
{
92+
o.WriteLine($" /** `{name}` - {description} */");
93+
}
94+
o.WriteLine($" {name}: {value},");
95+
}
6196
}
97+
o.WriteLine("} as const;");
98+
o.WriteLine();
99+
100+
// Generate the schema using z.nativeEnum
101+
o.WriteLine($"export const {enumTypeName} = z.nativeEnum({constName});");
62102
}
63103
}
64104
else if (id == ApiSpecConsts.WellKnown.IProductSearchItem)
@@ -72,7 +112,7 @@ private static void WriteType(string id, TypeSpecification type, IndentedStringB
72112

73113
if (type.TypeArguments.Any())
74114
{
75-
var typeArgument1 = string.Join(", ", type.TypeArguments.Select(x => $"{x[1..]} extends z.ZodTypeAny"));
115+
var typeArgument1 = string.Join(", ", type.TypeArguments.Select(x => $"{x[1..]} extends z.ZodType"));
76116
var typeArgument2 = string.Join(", ", type.TypeArguments.Select(x => $"{x[1..]}Schema: {x[1..]}"));
77117
o.WriteLine($"function __{fixedTypeName}<{typeArgument1}>({typeArgument2}) {{ return z.object({{");
78118
}
@@ -411,6 +451,29 @@ private static string EscapeForString(string s)
411451
return $"'{s.Replace("'", @"\'")}'";
412452
}
413453

454+
private static void WriteCommentLines(IndentedStringBuilder o, string comment)
455+
{
456+
var c = comment.Trim();
457+
if (string.IsNullOrWhiteSpace(c)) return;
458+
foreach (var line in c.Split('\n'))
459+
{
460+
var trimmed = line.Trim();
461+
if (!string.IsNullOrWhiteSpace(trimmed))
462+
{
463+
o.WriteLine($" * {trimmed}");
464+
}
465+
}
466+
}
467+
468+
private static void WriteComment(IndentedStringBuilder o, string comment)
469+
{
470+
var c = comment.Trim();
471+
if (string.IsNullOrWhiteSpace(c)) return;
472+
o.WriteLine("/**");
473+
WriteCommentLines(o, comment);
474+
o.WriteLine(" */");
475+
}
476+
414477
private static string TypeNameToTypescriptTypeName(ApiDefinitionModel input, string name, HashSet<string>? references)
415478
{
416479
references?.Add(name);

0 commit comments

Comments
 (0)