Support skipping fields from generated schema via zen:"ignore"#25
Support skipping fields from generated schema via zen:"ignore"#25hi-rai wants to merge 1 commit into
zen:"ignore"#25Conversation
Add a `zen:"ignore"` struct tag that excludes a field from the generated Zod schema and TypeScript type. Unlike `json:"-"`, it leaves JSON serialization untouched and also works on embedded (anonymous) fields - useful for fields that must stay on the wire (e.g. deprecated fields kept for API compatibility) but shouldn't surface in the generated types.
431608e to
0d0e54d
Compare
There was a problem hiding this comment.
Clean, well-scoped PR. The implementation is correct across all four field-iteration sites, the isFieldIgnored helper is idiomatic, the documentation is clear, and the test covers the key scenarios (plain field, field with json tag, embedded struct).
Two minor nits left as inline comments — neither is blocking:
- The shadowing loop in
getTypeStruct(line 419) doesn't filterzen:"ignore"fields from the embedded struct — functionally harmless but slightly inconsistent. - The test only exercises the
convertStructpath; a self-referencing struct test would cover all fourisFieldIgnoredcall sites.
No security or performance concerns.
| func TestStructWithIgnoredField(t *testing.T) { | ||
| type HasSecret struct { | ||
| Secret string | ||
| } | ||
| type User struct { | ||
| Name string | ||
| Age int | ||
| Internal string `zen:"ignore"` | ||
| Password string `zen:"ignore" json:"password"` | ||
| HasSecret ` zen:"ignore"` | ||
| } | ||
| assertSchema(t, User{}) | ||
| } |
There was a problem hiding this comment.
nit: Good test — it covers plain ignored fields, ignored + json tag, and ignored embedded structs. However, this only exercises the convertStruct code path. The other three sites where isFieldIgnored was added (getStructShape, and both loops in getTypeStruct) are only reached for self-referencing structs.
Consider adding a self-referencing struct test to cover all four code paths:
func TestRecursiveStructWithIgnoredField(t *testing.T) {
type Node struct {
Value string
Internal string `zen:"ignore"`
Next *Node `json:"next"`
}
assertSchema(t, Node{})
}
Add a
zen:"ignore"struct tag that excludes a field from the generated Zod schema and TypeScript type. Unlikejson:"-", it leaves JSON serialization untouched and also works on embedded (anonymous) fields - useful for fields that must stay on the wire (e.g. deprecated fields kept for API compatibility) but shouldn't surface in the generated types.