go-ozzo / ozzo-validation

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

Configurable valuer interface #155

Open oliy opened 3 years ago

oliy commented 3 years ago

In my application, I actually don't want sql.Valuer to be expanding my values, since I have a struct that expands to a JSON object in the DB. When I run validations, this means that it's validating against the JSON string and not the actual object. I'd like to be able to turn off this translation. Even better would be a way to pass in the interface or function that is able to convert specific validation objects.

Thearas commented 2 years ago

Hi @oliy, I met the same problem as you. Currently, the Indirect() works on all the type who implements driver.Valuer. This doesn't make much sense, I mean sometimes, the output of driver.Valuer.Value() is what we write directly to DB, but not for validation, right?

e.g.

type MySice []MyStruct

func (s MySice) Value() (driver.Value, error) {
    return json.Marshal(s)
}

type Foo struct {
    MySice
}

// This will serialize `MySice` into JSON and get error on `validation.Length(1, 3)`.
validation.ValidateStruct(foo, validation.Field(&foo.MySice, validation.Length(1, 3)))

Would you like to give us some advice? @qiangxue