networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
858 stars 325 forks source link

inconsistent path in ValidationMessage #831

Closed peter-ponzel closed 9 months ago

peter-ponzel commented 1 year ago

hello,

this issue has already been raised before → https://github.com/networknt/json-schema-validator/issues/615 unfortunately it was not really solved.

using the following json and json-schema

json.json ```json { "meta": { "label": "2", "tags": [ "A" ], "rating": "RPG" } } ```
schema.json ```json { "$id": "http://localhost:8080/schemas/schema.json", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": [ "meta" ], "properties": { "meta": { "type": "object", "required": [ "label", "tags", "rating", "officialRating" ], "properties": { "label": { "type": "string", "minLength": 2 }, "tags": { "type": "array", "uniqueItems": true, "items": { "type": "string", "enum": [ "action", "comedy", "drama", "fantasy" ] } }, "rating": { "$ref": "#/definitions/rating" }, "officialRating": { "$ref": "#/definitions/rating" } } } }, "definitions": { "rating": { "type": "string", "enum": [ "R", "PG-13", "PG", "G" ] } } } ```

will give the following results - depending on which PathType is used

response using PathType.JSON_POINTER ```json { "validationMessages": [ { "schemaPath": "#/properties/meta/required", "message": "/meta.officialRating: is missing but it is required", "path": "/meta", "arguments[0]": "officialRating" }, { "schemaPath": "#/definitions/rating/enum", "message": "/meta/rating: does not have a value in the enumeration [R, PG-13, PG, G]", "path": "/meta/rating", "arguments[0]": null }, { "schemaPath": "#/properties/meta/properties/label/minLength", "message": "/meta/label: must be at least 2 characters long", "path": "/meta/label", "arguments[0]": null }, { "schemaPath": "#/properties/meta/properties/tags/items/enum", "message": "/meta/tags/0: does not have a value in the enumeration [action, comedy, drama, fantasy]", "path": "/meta/tags/0", "arguments[0]": null } ] } ```
response using PathType.JSON_PATH or PathType.JSON_LEGACY ```json { "validationMessages": [ { "schemaPath": "#/properties/meta/required", "message": "$.meta.officialRating: is missing but it is required", "path": "$.meta", "arguments[0]": "officialRating" }, { "schemaPath": "#/definitions/rating/enum", "message": "$.meta.rating: does not have a value in the enumeration [R, PG-13, PG, G]", "path": "$.meta.rating", "arguments[0]": null }, { "schemaPath": "#/properties/meta/properties/label/minLength", "message": "$.meta.label: must be at least 2 characters long", "path": "$.meta.label", "arguments[0]": null }, { "schemaPath": "#/properties/meta/properties/tags/items/enum", "message": "$.meta.tags[0]: does not have a value in the enumeration [action, comedy, drama, fantasy]", "path": "$.meta.tags[0]", "arguments[0]": null } ] } ```

in some cases path points to the expected error field - but for instance when the ValidatorTypeCode is either ADDITIONAL_PROPERTIES or REQUIRED then the corresponding field can be found in arguments[0] and is not part of the path itself. however - when looking at the message it does look like it's always "pointing" to the expected error field .

is there any way to get the "full path" - other than parsing the message ?

on that note - could it be that this /meta.officialRating is not intended ? couldn't find anything regarding dots in json pointer, see rfc6901

best regards, peter

environement openjdk 17.0.7 2023-04-18 com.networknt:json-schema-validator:1.0.84

peter-ponzel commented 1 year ago

any feedback is welcome :upside_down_face:

fdutton commented 1 year ago

@peter-ponzel Good observations. The validation messages could definitely be improved and we have other requests including one to implement the format described in the JSON Schema specification.

I'm currently focused on ensuring complete coverage of the specification so I have not addressed these yet. Pull-requests are welcome.

See #828 #680 #401 #299 and #286

peter-ponzel commented 1 year ago

wow - I see .. just hit the tip of the iceberg :sweat_smile: