Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add custom struct tags to params structs #2039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
timrabl opened this issue Jan 27, 2023 · 1 comment
Open

Add custom struct tags to params structs #2039

timrabl opened this issue Jan 27, 2023 · 1 comment

Comments

@timrabl
Copy link

timrabl commented Jan 27, 2023

What do you want to change?

Hello,
first of all thank you for the brilliant library.

As already mentioned in Issue #534, the feature of custom struct tags was implemented with PR #1569.
However, I noticed that the custom struct tags are only added to the database table structs. But not to the their corresponding parameter structs.

In my opinion, this would also be quite helpful and should not cause more problems than the struct tags of the database types, or tables, at least in terms of compatibility.

Currently I am facing the problem to validate user payloads of a REST API in GoLang.
For this I use among others the libraries: sqlc, gofiber, go-playground/validator/v10.

I noticed the whole thing after I wanted to validate payload data to create a user.
Some project snippets for reference:

version: "2"

sql:
  - engine: "postgresql"
    queries: "db/sqlc/"
    schema: "db/migrations/"
    gen:
      go:
        package: "db"
        out: "internal/db"
        emit_json_tags: true
        emit_empty_slices: true
        emit_db_tags: true
        overrides:
          - column: users.first_name
            go_struct_tag: 'validate:"required"'
          - column: users.flast_name
            go_struct_tag: 'validate:"required"'
          - column: users.email
            go_struct_tag: 'validate:"required,email"'
          - column: users.organizational_unit
            go_struct_tag: 'validate:"required"'
          - column: users.name
            go_struct_tag: 'validate:"required"'
// db.go
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.16.0
type User struct {
	ID                 int32     `db:"id" json:"id"`
	FirstName          string    `db:"first_name" json:"first_name" validate:"required"`
	LastName           string    `db:"last_name" json:"last_name"`
	OrganizationalUnit string    `db:"organizational_unit" json:"organizational_unit" validate:"required"`
	Name               string    `db:"name" json:"name" validate:"required"`
	Email              string    `db:"email" json:"email" validate:"required,email"`
	CreatedAt          time.Time `db:"created_at" json:"created_at"`
	ChangedAt          time.Time `db:"changed_at" json:"changed_at"`
}
// query.sql.go
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.16.0
// source: query.sql
type UpdateUserParams struct {
	ID                 int32  `db:"id" json:"id"`
	FirstName          string `db:"first_name" json:"first_name"`
	LastName           string `db:"last_name" json:"last_name"`
	OrganizationalUnit string `db:"organizational_unit" json:"organizational_unit"`
	Name               string `db:"name" json:"name"`
	Email              string `db:"email" json:"email"`
}
import (
    "github.com/gofiber/fiber/v2"
    "github.com/go-playground/validator/v10"
    "example.org/internal/database"
    "example.org/internal/db" // sqlc generated package
)

var (
    validate = validator.New()
)

func ValidateStruct(model interface{}) []*ErrorResponse {

	var errors []*ErrorResponse
	err := validate.Struct(model)
	if err != nil {
		for _, err := range err.(validator.ValidationErrors) {
			var element ErrorResponse
			element.FailedField = err.StructNamespace()
			element.Tag = err.Tag()
			element.Value = err.Param()
			errors = append(errors, &element)
		}
	}
	return errors
}

func CreateUser(c *fiber.Ctx) error {
	// user struct contains struct tags for "github.com/go-playground/validator/v10" defined in sqlc.yaml
	userPayload := &db.User{}

	err := c.BodyParser(&userPayload)
	if err != nil {
		return MessageBadRequest(c, "failure while binding payload data to structured data")
	}

	errors := ValidateStruct(*userPayload)
	if errors != nil {
		return MessageBadRequest(c, errors)
	}

	queries := database.GetQueries()
	ctx, cancel := database.NewContext()

	defer cancel()

	// CreateUserParams is missing custom struct tags for "github.com/go-playground/validator/v10" defined in sqlc.yaml
	insertedUser, err := queries.CreateUser(ctx, db.CreateUserParams{
		// ...
	})
	if err != nil {
		return err
	}

	return MessageOK(c, insertedUser)
}

What database engines need to be changed?

PostgreSQL, MySQL

What programming language backends need to be changed?

Go, Python, Kotlin

@timrabl timrabl added enhancement New feature or request triage New issues that hasn't been reviewed labels Jan 27, 2023
@kyleconroy kyleconroy added πŸ“š mysql πŸ“š postgresql πŸ”§ golang and removed triage New issues that hasn't been reviewed labels Jun 7, 2023
@WiRight
Copy link

WiRight commented Mar 5, 2025

any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants