xeipuuv / gojsonschema

An implementation of JSON Schema, draft v4 v6 & v7 - Go language
2.54k stars 355 forks source link

Include "description" or "title" in error messages for "not", "oneOf", "anyOf", "allOf" #328

Open ghost opened 3 years ago

ghost commented 3 years ago

We are trying to model various validation steps using not, oneOf, anyOf,and allOf conditions. When the data does not pass schema validation, we get errors such as:

  - http.foo: Must validate one and only one schema (oneOf)
  - http.foo: Must not validate the schema (not)
  - http.foo: Must validate all the schemas (allOf)
  - http.foo: Must validate all the schemas (allOf)
# or 
  - http.foo: Must validate at least one schema (anyOf)
  - http.foo: Must not validate the schema (not)
  - http.foo: Must validate all the schemas (allOf)
  - http.foo: Must validate all the schemas (allOf)

I was thinking it would be helpful to include the "description" and or "title" annotations (if they exist) from the supplied schema in the error message. Is this a reasonable solution that would work well for all users? I can submit a PR if you think that is the right direction to move in.

I am an indirect user of this library (I have a Helm chart, and Helm uses gojsonschema). As such, I don't have options to plug in a custom locale supplying overrides of the error messages.

Thank you for your work on this library! It's very helpful to us... and please accept my apologies for the near-certainty that I misused JSON Schema terminology... I am still somewhat new. 😄

ZRJ026 commented 3 years ago

I want to output the error message defined by myself: schema.json { "$schema": "https://json-schema.org/draft/2019-09/schema", "type": "object", "title": "test", "properties": { "name": { "type": "string", "pattern": "^[a-z]$", "description":"err msg" } } } document.json { "name": "123" } https://github.com/xeipuuv/gojsonschema/blob/master/locales.go#L316-L318 When the regular matching is wrong,how can I output the information in description?Thank you!