Schema Builder Basics
NewSchema, pointer-identified fields, and the base constraints
Every gonest validation and OpenAPI feature builds on one foundation:
gonest.NewSchema[T](func(t *T, m *gonest.Schema) {...}).
type UserEntity struct {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
var userEntitySchema = gonest.NewSchema[UserEntity](func(t *UserEntity, m *gonest.Schema) {
m.Title("UserEntity")
m.Property(&t.Id).Integer().Required()
m.Property(&t.Name).String().Required().Min(1).Max(50)
m.Property(&t.Email).Email().Required()
})Fields are identified by their own pointer address
(m.Property(&t.Name)) — not a struct tag, not a string field name. Rename
the field and the compiler catches every reference; there's no string to
fall out of sync.
Base constraints
Every branch (String(), Integer(), Array(), Object(), ...) returns a
chainable builder that always has these four:
| Method | Effect |
|---|---|
Required() | Field must be present (and non-null unless Nullable()). |
Nullable() | Accept JSON null even when Required(). |
Description("...") | OpenAPI field description. |
Examples(...) | OpenAPI example values. |
m.Title("...") sets the name this schema appears under in OpenAPI's
components.schemas — defaults to the Go type name.
Historical note: earlier .specs design docs used NewMetadata[T] /
*Metadata and MustJsonBody[T]. These were renamed before release —
NewSchema[T] / *Schema and MustParseRestJsonBody[T] are the current,
correct names. This site uses only the current API.