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.78k stars 224 forks source link

Details on error #142

Closed maeglindeveloper closed 3 years ago

maeglindeveloper commented 3 years ago

Hi everyone,

I'm actually wondering if there is any way with go-ozzo to have some details on errors but in a more structured way ? Let me explain:

Let say I have the following code and an input like this with some validation

type MyInput struct {
    Email string
}

func (i MyInput) Validate() error {
    return validation.ValidateStruct(&i,
        validation.Field(&i.Email, is.Email, validation.Length(30, 100)),
    )
}

If for instance the Length rule failed, I will have an object of type validation.Errors which is basically a map[string]error. Of course it is fine, but is there any way to have something more structured ?

For instance could we have / imagine something map[string]ValidationError with the definition of ValidationError like this ?

type ValidationError struct {
    Code       string
    Message    string
    Parameters map[string]interface{}
}

Which can be easier to use / analyse that a simple string ?

As an example:

What we have now (json way)

{
    "Email":"the length must be between 30 and 100. "
}

What I would like :) (json way)

{
    "Email": {
        "Message":"the length must be between 30 and 100. ",
        "Code": "LenghtError",
        "Parameters": {
            "min": 30,
            "max": 100,
            "current": 4,
        }
    }
}

This is a total opened topic of course, all advices are really welcomed :+1:

maeglindeveloper commented 3 years ago

I closed this one since there is a duplicate unfortunetely ... sorry for this https://github.com/go-ozzo/ozzo-validation/issues/141