AJ ONeal 89b1191fdd
feat(jsontypes): infer types from JSON, generate code in 9 formats
Add tools/jsontypes library and tools/jsontypes/cmd/jsonpaths CLI.

Given a JSON sample (file, URL, or stdin), walks the structure,
detects maps vs structs, infers optional fields from multiple
instances, and produces typed definitions.

Output formats (--format):
- json-paths: flat type path notation (default)
- go: struct definitions with json tags and union support
- typescript: interfaces with optional/nullable fields
- jsdoc: @typedef annotations
- zod: validation schemas with type inference
- python: TypedDict classes
- sql: CREATE TABLE with FK relationships
- json-schema: draft 2020-12
- json-typedef: RFC 8927

Features:
- Interactive prompts for ambiguous structure (map vs struct, same
  vs different types), with --anonymous mode for non-interactive use
- Answer replay: saves prompt answers to .answers files for iterative
  refinement
- URL fetching with local caching and sensitive param stripping
- Curl-like auth: -H, --bearer, --user, --cookie, --cookie-jar
- Discriminated union support with sealed interfaces, unique-field
  probing, and CHANGE ME comments for type/kind discriminators
- Extensive round-trip compilation tests for generated Go code
2026-03-07 14:34:01 -07:00

68 lines
1.5 KiB
Go

package jsontypes
import (
"fmt"
"strings"
)
// generateZod converts formatted flat paths into Zod schema definitions.
func GenerateZod(paths []string) string {
types, _ := buildGoTypes(paths)
if len(types) == 0 {
return ""
}
// Emit in reverse order so referenced schemas are defined first.
var buf strings.Builder
buf.WriteString("import { z } from \"zod\";\n\n")
for i := len(types) - 1; i >= 0; i-- {
t := types[i]
if i < len(types)-1 {
buf.WriteByte('\n')
}
buf.WriteString(fmt.Sprintf("export const %sSchema = z.object({\n", t.name))
for _, f := range t.fields {
zodType := goTypeToZod(f.goType)
if f.optional {
zodType += ".nullable().optional()"
}
buf.WriteString(fmt.Sprintf(" %s: %s,\n", f.jsonName, zodType))
}
buf.WriteString("});\n")
}
// Type aliases
buf.WriteByte('\n')
for _, t := range types {
buf.WriteString(fmt.Sprintf("export type %s = z.infer<typeof %sSchema>;\n", t.name, t.name))
}
return buf.String()
}
func goTypeToZod(goTyp string) string {
goTyp = strings.TrimPrefix(goTyp, "*")
if strings.HasPrefix(goTyp, "[]") {
return "z.array(" + goTypeToZod(goTyp[2:]) + ")"
}
if strings.HasPrefix(goTyp, "map[string]") {
return "z.record(z.string(), " + goTypeToZod(goTyp[11:]) + ")"
}
switch goTyp {
case "string":
return "z.string()"
case "int64":
return "z.number().int()"
case "float64":
return "z.number()"
case "bool":
return "z.boolean()"
case "any":
return "z.unknown()"
default:
return goTyp + "Schema"
}
}