1

Is there any way to combine the JSON validation [github.com/go-playground/validator/v10] and the JSON Query [sqlc] without having to create two different structs?

I have the following table definition

CREATE TABLE table1 (
   columnName1 VARCHAR(200) NOT NULL PRIMARY KEY,
   columnName2 VARCHAR(200) NOT NULL
)

And the following query

-- name: GetTable1:one
SELECT * FROM table1
WHERE columnName1 = $1 LIMIT 1;

Executing SQLC the following code is generated

...
// 
// This struct is created automatically by sqlC
//
type CreateTable1Params struct {
    Columnname1 string                `json:"columnname1"`
    Columnname2 string                `json:"columnname2"`
}

func (q *Queries) CreateTable1(ctx context.Context, arg CreateTable1Params ) (Table1, error) {
...
}

Now, I need to validate the REST parameters for CreateTable1, let's say that columnName2 is required, so I have a controller package like the following

table1Controller.go

import (
    ...
    validator "github.com/go-playground/validator/v10"
)

type Table1Controller struct {
    Validate   *validator.Validate
}

//
// This struct is created manually for validation
//
type InT1Param struct {
    ColumnName1 string `validate:"required" form:"columnName1" json:"columnName1" db:"columnname1"`
    ColumnName2 string `validate:"required" form:"columnName2" json:"columnName2" db:"columnname2" binding="required"`
}

func (c *Table1Controller) validateInput(t1 InT1Param) error {
    err := service.Validate.Struct(t1)
    if err != nil {
        errStr := ""
        for _, mapErr := range err.(validator.ValidationErrors) {
            errStr = fmt.Sprintf("%s%s\n", errStr, mapErr.Translate(service.Translator))
        }
        return errors.New(errStr)
    }

    return nil
}

func (c *Table1Controller) Insert(ctx *gin.Context, dbQueries *dbModel.Queries, t1 InT1Param) error {
    err := c.validateInput(t1)
    if err != nil {
        return err

    inParam = dbModel.CreateTable1Param {
        Columnname1: t1.columnName1,
        Columnname2: t2.ColumnName2
    }

    outParam, err := dbQueries.CreateTable1(ctx, inParam)
    if err != nil {
        return err

    return nil
}
saavedrah
  • 183
  • 14

1 Answers1

1

Using the overrides key of the sqlc.yaml configuration I was able to add the following fields to the columns.

version: 2
sql:
  - schema: "./dbModel/migration/schema_20221008.sql"
    queries: "./dbModel/query/query.sql"
    engine: "postgresql"
    gen:
      go:
        package: "dbModel"
        out: "dbModel"
        emit_json_tags: true
        emit_db_tags: true
        emit_prepared_queries: false
        emit_interface: false
        emit_exact_table_names: false
        json_tags_case_style: camel
        overrides:
          - column: table1.column_name1
            go_struct_tag: validate:"required" x:"y,z"
          - column: table1.column_name2
            go_struct_tag: validate:"required"

This configuration generated the following struct for the table:

type table1 struct {
    ColumnName1 string `db:"column_name1" json:"columnName1" validate:"required" x:"y,z"`
    ColumnName2 string `db:"column_name2" json:"columnName2" validate:"required"`

I wasn't able to change the Param structs but I guess using the table main struct use the validation.

saavedrah
  • 183
  • 14