xeipuuv / gojsonschema

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

Doesn't provide line number of error #185

Open akhildtiwari opened 6 years ago

akhildtiwari commented 6 years ago

Don't see line number specified by the validator, would it be possible to add?

nmarley commented 6 years ago

I don't think JSON is required to have lines at all, so not sure if I'd agree this makes sense to add.

johandorland commented 6 years ago

When the JSON is parsed the line number information is lost. The errors do have a context which returns where in the JSON structure the error occurred. It would be possible to reconstruct the line number by looking up the line number of that JSON structure yourself, but I'm not sure of the added value by including that by default in this library itself.

handrews commented 6 years ago

There's been some talk with the JSON Schema spec of suggesting (although likely not mandating) output and error formats, and we'd almost certainly go with a JSON Pointer for indicating where in the document the error occurred. Possibly some sort of JSON Pointer over the in-memory structure rather than documents, so given

{
    "type": "object",
    "properties": {
        "foo": {"$ref": "#"},
        "bar": {"type": "integer"}
    }
}

with instance

{"foo": {"foo": {"bar": "whut"}}}

you'd have things like /properties/foo/$ref/properties/foo/$ref/properties/bar to show you that you found an error three levels down a recursive schema instead of just /properties/bar which only tells you where in the schema file the error happened.

This is a lot more portable and robust to things like minification than line numbers.

ghostsquad commented 5 years ago

I just recently started using this, and I'd love more information in the error messages.