Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Validate examples within the schema #1281

Answered by Julian
cc-stjm asked this question in Q&A
Discussion options

Is there any way (in the main library or check-jsonschema) to allow you to validate the examples within the JSON schema? I want to ensure the examples provided in the schema are actually valid

You must be logged in to vote

Hi there!

I assume you mean "I want to ensure that things in the examples keyword are valid under the schema they live inside"?
That's behavior not part of the specification (which basically says to do nothing with that keyword), though of course it's reasonable to want it.

There's not a public API built in at the moment for walking schemas (to e.g. find instances of the keyword) if you want to do this dynamically without knowledge of the specific schema -- such an API will come some day.

In the interim of course though you could either write the (fairly simple) function which walks a schema looking for the keyword, and just call validate.

Replies: 2 comments 2 replies

Comment options

Hi there!

I assume you mean "I want to ensure that things in the examples keyword are valid under the schema they live inside"?
That's behavior not part of the specification (which basically says to do nothing with that keyword), though of course it's reasonable to want it.

There's not a public API built in at the moment for walking schemas (to e.g. find instances of the keyword) if you want to do this dynamically without knowledge of the specific schema -- such an API will come some day.

In the interim of course though you could either write the (fairly simple) function which walks a schema looking for the keyword, and just call validate.

You must be logged in to vote
2 replies
Comment options

Thanks, I'll give that a try.

Comment options

(I moved this to a discussion though maybe I shouldn't have as I'm not sure there's an existing issue covering the schema walking API, so if you have any trouble feel free to either follow up and/or re-open one there for that!)

Answer selected by cc-stjm
Comment options

In case it helps anyone else, I've lashed together a jq based script that'll extract top level examples & test them.

check-jsonschema -v --color always --check-metaschema *.json 
rm -r examples
mkdir -p examples
for schemafile in *.json; do
 number_of_examples=$(jq 'if has("examples") then .examples | length else 0 end' "$schemafile")
 if [[ $number_of_examples -eq 0 ]]; then
 continue
 fi
 for i in $(seq $number_of_examples); do
 index=$( expr $i - 1);
 filename="examples/${schemafile%.*}_${index}.json"
 jq ".examples[$index]" "$schemafile" > $filename
 done;
 check-jsonschema -v --color always --schemafile "$schemafile" examples/${schemafile%.*}_*.json
done;

It needs a little more work to count as a CI error if there are invalid examples, but it's hopefully a useful start.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1279 on July 03, 2024 15:33.

AltStyle によって変換されたページ (->オリジナル) /