Custom error message
deepak191093 opened this issue · 9 comments
I want to return custom error message not predefined as it is not understandable to common people.
can you please elaborate what do you mean by that. may be giving an example would be better
Started working on this feature. Will post pull request tomarrow
looking forward to it.
please check pull request #99
this pull request changes type of ValidationError.Message from string to struct implementing fmt.Stringer
go through the changes and let me know your opinion
closing. seems duplicate of #44
i am using for json schema validation with 2020-12 draft. to send custom message which field should i mention in json schema file. Need a clarification on that.
@deepak191093 it is still work in progress. see issue #44 and PR #99
since this is a breaking change I have to bump the version. I am waiting for your opinion before I can release it
fix is available in v6.0.0-alpha.1
ValidationError.Message
was string
type. now it is changed to fmt.Stringer
.
so you can create custom error message as follows:
import (
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/santhosh-tekuri/jsonschema/v5/msg"
)
func getCustomMessage(err *jsonschema.ValidationError) string {
switch m := err.Message.(type) {
case msg.Type:
return fmt.Sprintf("i am expecting %s, but I got %s", m.Want, m.Got)
default:
return err.Message.String()
}
}
the above code shows how to create custom error message for type mismatch. similarly you can add more cases in switch for customising different error messages. see msg
subpackage for different error message structs.