shape README

README проекта с github.com/rhevorn/shape. Одним файлом markdown: README.md.

shape

CI Go Reference Go Report Card License: MIT

Typed validation, transformation, and JSON, form, and query binding for Go.

Describe a struct once, then transform, validate, or parse input with the same definition. Invalid field configuration panics; bad input returns errors.

Features

Status

Preparing the first public release. The public surface and behavioral contract are specified in docs/API.md; after the first public tag, incompatible changes require a major version.

Install

go get github.com/rhevorn/shape

Requires Go 1.24 or newer.

Struct tags

For simple DTOs, put rules on the struct and bind with no Schema variable:

type CreateUserRequest struct {
	Name string `json:"name" form:"name" query:"name" shape:"trim,notempty,maxlength=50"`
	Age  int    `json:"age" form:"age" query:"age" shape:"min=18,max=120"`
}

var request CreateUserRequest
err := shape.BindJSON(&request, data)

BindJSON writes the target only after the full pipeline succeeds. Use shape.FromTags[CreateUserRequest]() to reuse the tagged Schema explicitly.

Explicit Schema

Field behavior written in Go with shape.New:

type User struct {
	Name string   `json:"name" form:"name" query:"name"`
	Age  int      `json:"age" form:"age" query:"age"`
	Tags []string `json:"tags" form:"tags" query:"tags"`
}

var userSchema = shape.New[User](
	shape.Field("Name", shape.String().Trim().NotEmpty().MaxLength(50)),
	shape.Field("Age", shape.Int().Min(18).Max(120)),
	shape.Field("Tags", shape.Slice(shape.String().Trim().NotEmpty()).NotEmpty().Unique()),
)

data := []byte(`{
	"name": " Pong ",
	"age": 20,
	"tags": [" go ", "shape"]
}`)

user, err := userSchema.ParseJSON(data)
// user.Name == "Pong"

The equivalent package-level call is:

user, err := shape.ParseJSON(userSchema, data)

Both forms decode, transform, and validate in one call. Schemas created with shape.New[T] or shape.FromTags[T] support the method form; the package function accepts any Schema[T], including scalars and collections.

Field names and their target types are checked at construction; generics check value and collection composition. Optional shapevet catches statically visible binding mistakes before runtime.

The same schema also supports Transform and Validate on typed values. Value factories such as String, Slice, and Map never take a field name; Field is the single explicit bridge from a value Schema to a struct field.

Forms and query parameters

The same struct Schema accepts url.Values:

values := url.Values{
    "name": {" Pong "},
    "age":  {"20"},
    "tags": {"go", "shape"},
}

// Schema methods.
user, err := userSchema.ParseForm(values)
user, err = userSchema.ParseQuery(values)

// Equivalent package functions.
user, err = shape.ParseForm(userSchema, values)
user, err = shape.ParseQuery(userSchema, values)

For tagged DTOs, use shape.BindForm(&request, values) or shape.BindQuery(&request, values). All calls have context variants and Bind updates its target only on success.

Each input uses its own tag (json, form, or query), falling back to the Go field name. Input tags never change the Schema’s transformation or validation rules. Repeated parameters bind to slices; repeated scalars return an error. Nested structs use names such as address.city.

For HTTP forms, parse the request and pass r.PostForm or r.MultipartForm.Value. For queries, use url.ParseQuery(r.URL.RawQuery) and check its error before passing the values. See Forms and query parameters for decoding rules, strict options, and HTTP examples.

HTTP requests

For automatic Query and body binding:

var request CreateUserRequest
err := shape.BindRequest(&request, r, shape.RequestOptions{
    DisallowUnknownFields: true,
})

Query uses query tags. Content-Type selects JSON (json tags) or Form (form tags) for the body. A field submitted by both sources returns a conflict error; set Precedence: shape.QueryFirst or shape.BodyFirst for explicit precedence. Objects and slices are replaced as whole top-level fields, never recursively merged. The combined value is transformed and validated once before replacing the target. The default body limit is 1 MiB; multipart file parts require manual handling. See HTTP binding.

Scalars

Standalone string Specs work the same way without a struct:

name := shape.String().Trim().ToLower().NotEmpty()

out, err := name.Transform(" Pong ")
// out == "pong", err == nil

err = name.Validate(out)

Documentation

License

MIT

shape (rhevorn/shape)

Ищем в базе…

Что это?Shape — типобезопасная библиотека валидации схем для Go без внешних зависимостей: вы описываете структуру один раз и переиспользуете её для преобразования, валидации и разбора JSON в значения Go.
Это фреймворк или библиотека?Это библиотека для Go из компонуемых пакетов (validate, transform, jsonschema, openapi) с публичным API на основе дженериков, а не фреймворк или отдельное приложение.
Как добавить его в проект?Установите его как Go-модуль (требуется Go 1.24+), затем определите схему с помощью shape.NewT или тегов структуры и вызывайте на ней Transform, Validate, ValidateFirst или ParseJSON/BindJSON.
Нужны ли внешние зависимости для его работы?Нет, исполняемые пакеты полагаются только на стандартную библиотеку Go, поэтому устанавливать сторонние зависимости не нужно.
Есть ли подводные камни при установке или работе со схемами?Неверные имена полей и несовместимые типы полей обнаруживаются при создании схемы, а для дополнительных проверок тегов/схем на этапе компиляции нужен необязательный статический анализатор shapevet; без него плохие определения выявляются только во время выполнения.
Валидация останавливается на первой ошибке или собирает все?Можно выбрать: Validate собирает все найденные проблемы, а ValidateFirst останавливается, как только встречает первую.
Можно ли сгенерировать определения JSON Schema или OpenAPI из схемы shape?Да, пакет jsonschema выдаёт JSON Schema Draft 2020-12, а пакет openapi создаёт адаптеры OpenAPI 3.1 из тех же определений схем.
Сообщения об ошибках валидации только на английском?Нет, встроенные сообщения об ошибках из коробки поддерживают несколько локалей, включая английскую и упрощённую китайскую, а локаль можно передать через context.
Что такое shapevet?shapevet — необязательный статический анализатор, который на этапе компиляции проверяет имена fluent-методов, теги структуры и явно заданные имена полей схемы; без него некорректные определения схем всё равно выявляются, но уже во время выполнения.
Какие есть похожие проекты?pydantic, OpenAPI-Specification, validator, grpc-gateway, exercises-dataset, go-swagger, sqlmodel, ajv
Автор
rhevorn
Размер
0.8 MB
Звёзды
170
Обновлён
КоммитSep 20
Возраст
2 мес
Коммиты · 90 дней