santhosh-tekuri/jsonschema

error is missing instanceLocation

Opened this issue · 1 comments

When given a bad JSON file the location of the error is not specified in the error.

$ go run github.com/santhosh-tekuri/jsonschema/cmd/jv@latest --output detailed comment.schema bad.json
instance bad.json: failed
{
    "valid": false,
    "keywordLocation": "",
    "instanceLocation": "",
    "errors": [
        {
            "valid": false,
            "keywordLocation": "/additionalProperties",
            "instanceLocation": "",
            "error": "additional properties 'foo' not allowed"
        }
    ]
}
exit status 1

What is expected

The Location fields provide the location of the error, in this case the location of the foo property.

What happens instead

The instanceLocation field is empty:

    "instanceLocation": "",

Reproducer

cat > bad.json <<EOF
{
    "index": "5",
    "foo": "i",
	"poster_id": "1",
        "created": "1985-04-12T23:20:50.52Z",
        "updated": "1986-04-12T23:20:50.52Z",
	"content": "comment_content_5"
}
EOF
cat > good.json <<EOF
{
        "index": "5",
	"poster_id": "1",
        "created": "1985-04-12T23:20:50.52Z",
        "updated": "1986-04-12T23:20:50.52Z",
	"content": "comment_content_5"
}
EOF
cat > comment.schema <<EOF
{
    "title": "Comment",
    "type": "object",
    "additionalProperties": false,
    "properties": {
		"index": {
			"description": "Unique identifier of the comment.",
			"type": "string"
		},
		"poster_id": {
			"description": "Unique identifier of the comment author.",
			"type": "string"
		},
		"created": {
			"description": "Creation time.",
			"type": "string",
			"format": "date-time"
		},
		"updated": {
			"description": "Last update time.",
			"type": "string",
			"format": "date-time"
		},
		"content": {
			"description": "Markdown content of the comment.",
			"type": "string"
		}
    },
    "required": [
	"index",
	"poster_id",
	"created",
	"updated",
	"content"
    ],

    "$schema": "http://json-schema.org/draft-04/schema#",
    "$id": "https://lab.forgefriends.org/friendlyforgeformat/f3-schemas/-/blob/v1.0/comment.json",
    "$$target": "comment.json"
}
EOF

go run github.com/santhosh-tekuri/jsonschema/cmd/jv@latest --output detailed comment.schema good.json
# schema comment.schema: ok
#
# instance good.json: ok
go run github.com/santhosh-tekuri/jsonschema/cmd/jv@latest --output detailed comment.schema bad.json
# schema comment.schema: ok

# instance bad.json: failed
# {
#     "valid": false,
#     "keywordLocation": "",
#     "instanceLocation": "",
#     "errors": [
#         {
#             "valid": false,
#             "keywordLocation": "/additionalProperties",
#             "instanceLocation": "",
#             "error": "additional properties 'foo' not allowed"
#         }
#     ]
# }
# exit status 1

This is intentional. let us say the instance is:

{
    "index": "5",
    "foo": "I",
    "bar": "K",
    "poster_id": "1",
    "created": "1985-04-12T23:20:50.52Z",
    "updated": "1986-04-12T23:20:50.52Z",
    "content": "comment_content_5"
}

the generated error will be:

{
    "valid": false,
    "keywordLocation": "",
    "instanceLocation": "",
    "errors": [
        {
            "valid": false,
            "keywordLocation": "/additionalProperties",
            "instanceLocation": "",
            "error": "additional properties 'foo', 'bar' not allowed"
        }
    ]
}

rather than two errors as shown below:

{
    "valid": false,
    "keywordLocation": "",
    "instanceLocation": "",
    "errors": [
        {
            "valid": false,
            "keywordLocation": "/additionalProperties",
            "instanceLocation": "/foo",
            "error": "additional properties 'foo', not allowed"
        },
        {
            "valid": false,
            "keywordLocation": "/additionalProperties",
            "instanceLocation": "/bar",
            "error": "additional properties 'bar' not allowed"
        }
    ]
}

we are collecting all invalid additionalProperties and producing single error message where instanceLocation points to the object containing those invalid additionalProperties.

BTW, "instanceLocation": "" means the document root, which is valid jsonpath