{"id":13767505,"url":"https://github.com/Southclaws/supervillain","last_synced_at":"2025-05-10T23:30:45.513Z","repository":{"id":57624483,"uuid":"398626594","full_name":"Southclaws/supervillain","owner":"Southclaws","description":"Converts Go structs to Zod schemas","archived":false,"fork":false,"pushed_at":"2024-08-28T14:27:23.000Z","size":60,"stargazers_count":87,"open_issues_count":2,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T12:21:32.524Z","etag":null,"topics":["go","go-generics","schemas","typescript","zod"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Southclaws.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-21T18:08:39.000Z","updated_at":"2025-03-21T17:28:08.000Z","dependencies_parsed_at":"2023-12-13T18:23:25.598Z","dependency_job_id":"8fdbe23b-8a3e-4862-ae2a-88ac79f24820","html_url":"https://github.com/Southclaws/supervillain","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fsupervillain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fsupervillain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fsupervillain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fsupervillain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Southclaws","download_url":"https://codeload.github.com/Southclaws/supervillain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253497296,"owners_count":21917683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","go-generics","schemas","typescript","zod"],"created_at":"2024-08-03T16:01:09.209Z","updated_at":"2025-05-10T23:30:45.105Z","avatar_url":"https://github.com/Southclaws.png","language":"Go","funding_links":[],"categories":["typescript","Convertors and Generators"],"sub_categories":[],"readme":"# Supervillain\n\nConverts Go structs to Zod schemas.\n\nUsage:\n\n```go\ntype Post struct {\n    Title string\n}\ntype User struct {\n    Name   string\n    Nickname *string // pointers become optional\n    Age    int\n    Height float64\n    Tags []string\n    Favourites []struct { // nested structs are kept inline\n        Name string\n    }\n    Posts []Post // external structs are emitted as separate exports\n}\n\nStructToZodSchema(User{})\n```\n\nOutputs:\n\n```typescript\nexport const PostSchema = z.object({\n  title: z.string(),\n});\nexport type Post = z.infer\u003ctypeof PostSchema\u003e;\n\nexport const UserSchema = z.object({\n  name: z.string(),\n  nickname: z.string().optional(),\n  age: z.number(),\n  height: z.number(),\n  tags: z.string().array(),\n  favourites: z\n    .object({\n      name: z.string(),\n    })\n    .array(),\n  posts: PostSchema.array(),\n});\nexport type User = z.infer\u003ctypeof UserSchema\u003e;\n```\n\n## Custom Types\n\n### ZodSchema() method\n\nYou can define a custom conversion using a `ZodSchema()` method. This should have one of the following types:\n```go\nZodSchema() string\nZodSchema(c *supervillain.Converter, t reflect.Type, name, generic string, indent int) string\nZodSchema(convert func(t reflect.Type, name string, indent int) string, t reflect.Type, name, generic string, indent int) string\n```\n(The first signature is available to simplify the simple case; the last signature is available in case you do not want the package defining the type to depend on supervillain.)\n\nZod will obtain a schema by creating a zero value of your type and calling its ZodSchema() method.\n\n```go\ntype State int\n\nfunc (s State) MarshalJSON() ([]byte, error) {\n  return json.Marshal(fmt.Sprint(s))\n}\n\nfunc (s State) ZodSchema() string {\n  return \"z.string()\"\n}\n\ntype Job struct {\n  State State\n}\n\nc.Convert(Job{})\n```\n\nOutputs:\n\n```typescript\nexport const JobSchema = z.object({\n  State: z.string(),\n})\nexport type Job = z.infer\u003ctypeof JobSchema\u003e\n```\n\n### Mapping\n\nIf you don't control the type yourself, you can also pass a map of type names to custom conversion functions:\n\n```go\nc := supervillain.NewConverter(map[string]supervillain.CustomFn{\n    \"github.com/shopspring/decimal.Decimal\": func(c *supervillain.Converter, t reflect.Type, s, g string, i int) string {\n        // Shopspring's decimal type serialises to a string.\n        return \"z.string()\"\n    },\n})\n\nc.Convert(User{\n    Money decimal.Decimal\n})\n```\n\nOutputs:\n\n```typescript\nexport const UserSchema = z.object({\n  Money: z.string(),\n})\nexport type User = z.infer\u003ctypeof UserSchema\u003e\n```\n\nThere are some custom types with tests in the \"custom\" directory.\n\nThe function signature for custom type handlers is:\n\n```go\nfunc(c *supervillain.Converter, t reflect.Type, typeName, genericTypeName string, indentLevel int) string\n```\n\nYou can use the Converter to process nested types. The `genericTypeName` is the name of the `T` in `Generic[T]` and the indent level is for passing to other converter APIs.\n\n### Custom Schema Enforcement\n\nTypes with a custom MarshalJSON() method but no custom schema are typically problematic, since the generated schema may not match the custom marshalled format. You can use the `WithStrictCustomSchemas` option to cause conversion to fail (panic) if such a type is found:\n\n```go\nc := NewConverter(map[string]CustomFn{}, WithStrictCustomSchemas(true))\n// or\nStructToZodSchema(User{}, WithStrictCustomSchemas(true))\n```\n\n## Caveats\n\n- Does not support self-referential types - should be a simple fix.\n- Sometimes outputs in the wrong order - it really needs an intermediate DAG to solve this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSouthclaws%2Fsupervillain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSouthclaws%2Fsupervillain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSouthclaws%2Fsupervillain/lists"}