Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JeelRajodiya opened this issue Mar 15, 2024 · 4 comments

Comments

@JeelRajodiya
Copy link

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
Copy link
Collaborator

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
Copy link
Author

Thanks a lot!
Should I close the issue?

@jdesrosiers
Copy link
Collaborator

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants