jellydator / validation

An idiomatic validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
39 stars 6 forks source link

bug about type alias #2

Closed ghost closed 2 years ago

ghost commented 2 years ago

if I set a type alias of basic type,like []string ,then, I neet to add two function,such as ( value and scan ,mysql json need this), the bug is appened. the entire code under:

package main

import (
    "database/sql/driver"
    "encoding/json"
    "fmt"

    validation "github.com/go-ozzo/ozzo-validation"
)

type Tags []string

// mysql 8.0 json need value and scan function, so I need type alias
type Courses struct {
    Tag Tags `gorm:"type:json;comment:体系名是json格式" json:"tag"`
}

// !!!!!! IF YOU REMOVE THIS FUNCTION, IT IS VALIDETED !!!!!
// !!!!!! IF YOU ADD PONITER IN P, IT IS VALIDETED  !!!!
func (p Tags) Value() (driver.Value, error) {
    b, err := json.Marshal(p)
    return b, err
}

func (p *Tags) Scan(input any) error {
    return json.Unmarshal(input.([]byte), p)
}

func main() {

    var courseDto Courses
    courseDto.Tag = Tags{"student", "engineer"}
    err := validation.ValidateStruct(&courseDto,
        validation.Field(&courseDto.Tag, validation.Required, validation.Length(1, 3)),
    )
    fmt.Println(err)
}

Something strange is coming in function value !!! If p without pointer can't pass validate!

func (p Tags) Value() (driver.Value, error) {
    //b, err := json.Marshal(p)
    return nil, nil
}

But, if add pointer, it pass!

ghost commented 2 years ago

It's not a bug, just when validating the sql will use Value function