validator

package module
v1.0.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 2 Imported by: 0

README

Compogo Validator

Go Reference

Адаптер go-playground/validator для фреймворка Compogo.

Регистрирует валидатор в DI-контейнере для проверки структур в HTTP-хендлерах, GRPC-сервисах и других компонентах.

Установка

go get github.com/Compogo/validator

Быстрый старт

package main

import (
    "github.com/Compogo/compogo"
    "github.com/Compogo/validator"
)

func main() {
    app := compogo.NewApp("myapp",
        compogo.WithComponents(&validator.Component),
    )

    if err := app.Serve(); err != nil {
        panic(err)
    }
}

Зависимости

Лицензия

MIT License

Copyright (c) 2026 Compogo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Component = compogo.Component{
	Init: compogo.StepFunc(func(container compogo.Container) error {
		return container.Provide(validator.New)
	}),
}

Component — компонент валидатора для Compogo. Регистрирует экземпляр validator.Validate в DI-контейнере.

Валидатор используется для проверки структур в HTTP-хендлерах, GRPC-сервисах и других компонентах приложения.

Пример использования:

type CreateUserRequest struct {
    Name  string `json:"name" validate:"required,min=3,max=50"`
    Email string `json:"email" validate:"required,email"`
    Age   int    `json:"age" validate:"gte=18,lte=100"`
}

func (api *UserAPI) Create(w http.ResponseWriter, r *http.Request) {
    var req CreateUserRequest
    // ... decode json

    if err := api.validator.Struct(req); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    // ... обработка
}

Подключение в приложении:

app.AddComponents(&validator.Component)

Получение валидатора из контейнера:

var validate *validator.Validate
container.Invoke(func(v *validator.Validate) { validate = v })

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL