1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package openapi3_test
import (
"fmt"
"github.com/getkin/kin-openapi/openapi3"
)
func ExampleSetSchemaErrorMessageCustomizer() {
loader := openapi3.NewLoader()
spc := `
components:
schemas:
Something:
type: object
properties:
field:
title: Some field
type: string
`[1:]
doc, err := loader.LoadFromData([]byte(spc))
if err != nil {
panic(err)
}
opt := openapi3.SetSchemaErrorMessageCustomizer(func(err *openapi3.SchemaError) string {
return fmt.Sprintf(`field "%s" should be string`, err.Schema.Title)
})
err = doc.Components.Schemas["Something"].Value.Properties["field"].Value.VisitJSON(123, opt)
fmt.Println(err.Error())
// Output: field "Some field" should be string
}
|