hyperjump-io / json-schema

JSON Schema Validation, Annotation, and Bundling. Supports Draft 04, 06, 07, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1
https://json-schema.hyperjump.io/
MIT License
221 stars 21 forks source link

[Help Wanted] How can I get descriptive error messages when the schema is invalid. #56

Open JeelRajodiya opened 7 months ago

JeelRajodiya commented 7 months ago

I have the following schema

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "array",
    "items": {
        "type": "string"
    }
}

and following data

[1,2,3]

Now when I validate the schema, the I get the following error

{"keyword":"https://json-schema.org/keyword/items","absoluteKeywordLocation":"http://example.com/schemas/string#/items","instanceLocation":"#","valid":false,
"errors":[{"keyword":"https://json-schema.org/evaluation/validate","absoluteKeywordLocation":"http://example.com/schemas/string#/items","instanceLocation":"#/0","valid":false,
"errors":[{"keyword":"https://json-schema.org/keyword/type","absoluteKeywordLocation":"http://example.com/schemas/string#/items/type","instanceLocation":"#/0","valid":false,"errors":[]}]}]}

My code

image

What I am looking for is a descriptive error similar to what cfworker/json-schema throws

Items did not match schema. Instance type "number" is invalid. Expected "string".

jdesrosiers commented 7 months ago

I never implemented descriptive error messaging. The theory was that the standard JSON Schema output format, would allow people to write tools to consume that format to give you nice error messaging that makes sense for your specific use case. There are a couple such implementations that use AJV's proprietary output format. There's one designed for CLI consumption and another designed for API responses. Unfortunately, no one ever built things like that for the standard output format, so for now there isn't really something I can point you to solve this problem. I'll have to give up waiting for someone else to do it and do it myself, but I haven't had time yet.

I recently shared with another user some code to get started if you want to see how to build out some messaging yourself.

JeelRajodiya commented 7 months ago

Thanks a lot! Should I close the issue?

jdesrosiers commented 7 months ago

I think you can leave it open. It will help others with the same question find the answer they're looking for. I'll close it in the future if/when the situation changes.

GabenGar commented 7 months ago

The reason no one (including the AJV maintainer) has written the standard message parser is because it is a humongous task once you realize it is pretty useless without multilang support. And multilang support brings its own can of worms: bloated bundle size for client and exploding string cache for server to name a few.