swaggest / rest

Web services with OpenAPI and JSON Schema done quick in Go
https://pkg.go.dev/github.com/swaggest/rest
MIT License
335 stars 17 forks source link

Question: validation errors #197

Closed betzerra closed 3 months ago

betzerra commented 3 months ago

Hi! I'm trying to build a custom error middleware and I noticed that I'm getting some validation errors squashed into one item.

I got:

[
  "#: missing properties: \"foo\", \"bar\""
]

while I was expecting:

[
  "missing property: \"foo\"",
  "missing property:\"bar\""
]

when calling Fields(). Is that something I could easily override or setup?

vearutop commented 3 months ago

hi, the error messages are not necessarily combined or separated by property, they are created with the context of particular validation check (mostly constraint keyword and related parts of value)

here is how this particular error is created: https://github.com/santhosh-tekuri/jsonschema/blob/v3.1.0/schema.go#L298

        if len(s.Required) > 0 {
            var missing []string
            for _, pname := range s.Required {
                if _, ok := v[pname]; !ok {
                    missing = append(missing, strconv.Quote(pname))
                }
            }
            if len(missing) > 0 {
                errors = append(errors, validationError("required", "missing properties: %s", strings.Join(missing, ", ")))
            }
        }

It seems there is no easy way to customize that.

betzerra commented 3 months ago

Thank you for your answer. Helped me a lot :-)