33 lines
1.4 KiB
Markdown
33 lines
1.4 KiB
Markdown
# contract
|
|
|
|
Generated API projects use this package to compile Go field tags once and bind
|
|
each request without depending on a specific HTTP server. `New[T]` validates a
|
|
struct and returns an immutable Binder. `ForType` provides the same compiler to
|
|
the CLI. `BodySchema`, `Parameters`, and `ResponseSchema` return detached OpenAPI
|
|
snapshots. `MustNew[T]` is intended for generated, already checked declarations.
|
|
|
|
```go
|
|
type Request struct {
|
|
ID int64 `path:"id,range=[1:100]"`
|
|
Name string `json:"name" binding:"required,max=50"`
|
|
}
|
|
|
|
var request = contract.MustNew[Request]()
|
|
|
|
func Handle(c *httpx.Context) error {
|
|
var req Request
|
|
if err := request.Bind(c.Request(), httpx.RouteParams(c.Context()), &req); err != nil {
|
|
return err
|
|
}
|
|
return c.OK(req.Name)
|
|
}
|
|
```
|
|
|
|
The default body limit is 1 MiB. An optional positive final argument to Bind
|
|
overrides it. Application transport read timeouts still control slow clients.
|
|
See [API.md](../API.md) for source tags, validation rules, null/presence behavior,
|
|
strict decoding and supported type limits. This API leaves the existing
|
|
`Context.BindJSON/BindQuery/BindURI` behavior unchanged.
|
|
|
|
Recursive response DTOs in `.api` are compiled by the CLI as named OpenAPI references; see [the response guide](../DEVELOPMENT.md#递归响应-dto). This does not change the nonrecursive request type requirement of `New`, `ForType`, or `Describe`.
|