As far as the code is concerned, you're doing it right, and you don't need the ValidateMetaSchema option. That option validates the schema against its metaschema while you're validating other JSON data against the schema.

For example, if I have deserialized your last example

{
  "$schema": ""http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5", 
    "prop6"
  ]
}

into JsonSchema and I'm using that to validate some other JSON data, then that option will add a secondary validation of the schema against the draft 7 metaschema. If the schema is invalid for some reason (for example, in draft 7 "$defs": 42 would be ignored, but draft 2020-12 it's invalid), then the $schema keyword would raise an error that would be included in the output.

In your case, you'd be directly validating the draft 7 metaschema against its metaschema (which is just itself). But we already know that the draft 7 metaschema is valid against itself, so this is just an extra check that's unnecessary. Go ahead and leave that option off.


In a comment, you asked if there was a way to raise an error if unknown keywords were present. There is no such option.

However, what you can do is check the schema.Keywords property for any that are of type UnrecognizedKeyword. If there are any, then you have extra data in the schema.

Be mindful that schemas can nest, though, so you'll need to check each level.

{
  "allOf": [
    { "unrecognized": "keyword" }
  ]
}

Here, you'll need to go find the AllOfKeyword and check its subschemas for UnrecognizedKeywords.


Aside from that, I'm going to expand on @Clemens' answer a bit to explain why your examples are coming back as valid.

{
  "f": "a"
}

When validated against a metaschema, this JSON is going to produce the same validation result as your second example {} because (as @Clemens mentioned) JSON Schema ignores unknown keywords. Since f isn't a recognized keyword, it's ignored by validation. (There's an annotation collected for f the output, though.)

Because this has no validation keywords in it, it will validate all JSON instances. Technically this is a valid schema, although it doesn't do much.

{
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5",
    "prop6"
  ]
}

Here, you're requiring certain properties to be present if the JSON instance is an object. But if the instance is not an object, required has no effect. You'll probably want to constrain values more with "type": "object".

{
  "$schema": ""http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5", 
    "prop6"
  ]
}

Here you have all the pieces in place, and what you probably expect can work. The JSON is still a valid draft 7 schema (it's also valid for draft 2020-12).

In order for the schema to be invalid, you'd have to put a valid in for a defined keyword that it doesn't support, like giving maximum a string value. In this case, the schema will fail validation.

That said, if you were to try to deserialize invalid schema JSON into a JsonSchema model, the serializer will throw an exception because there's some validation that occurs during deserialization.

I think your approach of validating the schema JSON against a meta-schema is better than letting the serializer throw an exception, but you'll want to be sure you validate against the metaschema that's represented in the $schema keyword. (So don't validate a draft 2020-12 schema against the draft 7 metaschema.)

Answer from gregsdennis on Stack Overflow
🌐
JSON Schema Validator
jsonschemavalidator.net
JSON Schema Validator - Newtonsoft
An online, interactive JSON Schema validator. Supports JSON Schema Draft 3, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft 2020-12.
🌐
Rust
docs.rs β€Ί jsonschema
jsonschema - Rust
The jsonschema crate offers two main approaches to validation: one-off validation and reusable validators. When external references are involved, the validator can be constructed using either blocking or non-blocking I/O. For simple use cases where you need to validate an instance against a ...
Discussions

c# - How can I validate that a JSON Schema is valid against the JSON Schema Standard) with JsonSchema.Net - Stack Overflow
I've been struggling a lot trying to validate a json schema against a meta-schema (check if the json actually follows the JSON Schema Standard). I tried to follow the documentation link , link. And... More on stackoverflow.com
🌐 stackoverflow.com
Some thoughts around Elixir JSON Schema Validator
I always wondered, can you use ecto for that? More on reddit.com
🌐 r/elixir
10
8
February 2, 2021
The Last Breaking Change | JSON Schema Blog
always fun to see the json guys struggling with the previous generation's xml issues. Not that I particularly like either, you understand, and honestly json is obviously less crushingly verbose, it's just amusing. People do seem to have to learn the hard way that a lot of the "complicated" stuff xml had was there for a reason. Wonder what format the next generation will be insisting is better that xml and json. https://en.wikipedia.org/wiki/Unique_Particle_Attribution https://lists.w3.org/Archives/Public/www-tag/2004Aug/att-0010/NRMVersioningProposal.html More on reddit.com
🌐 r/programming
270
529
March 5, 2023
JSON schema generator
I just searched google and these 2 popped up: JSON Schema Tool Free Online JSON to JSON Schema Converter Both seem to work fine More on reddit.com
🌐 r/vscode
4
17
September 20, 2021
🌐
Liquid Technologies
liquid-technologies.com β€Ί online-json-schema-validator
Free Online JSON Validator (JSON Schema)
The Free Community Edition of Liquid Studio comes with an advanced JSON Editor, packed with many helpful features including JSON document validation. Data Security Warning - Tick this box to confirm you understand all data is stored on our servers ...
🌐
Packet Coders
tools.packetcoders.io β€Ί json-schema-validator
Online JSON Schema Validator Tool - Validate JSON Data
Validate and check JSON structures using JSON Schema for data validation, compliance, and automation workflows.
🌐
Mockoon
mockoon.com β€Ί tools β€Ί json-schema-validator
Mockoon - Online JSON Schema validator
This tool allows you to validate your JSON data against a JSON schema.
Top answer
1 of 3
4

As far as the code is concerned, you're doing it right, and you don't need the ValidateMetaSchema option. That option validates the schema against its metaschema while you're validating other JSON data against the schema.

For example, if I have deserialized your last example

{
  "$schema": ""http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5", 
    "prop6"
  ]
}

into JsonSchema and I'm using that to validate some other JSON data, then that option will add a secondary validation of the schema against the draft 7 metaschema. If the schema is invalid for some reason (for example, in draft 7 "$defs": 42 would be ignored, but draft 2020-12 it's invalid), then the $schema keyword would raise an error that would be included in the output.

In your case, you'd be directly validating the draft 7 metaschema against its metaschema (which is just itself). But we already know that the draft 7 metaschema is valid against itself, so this is just an extra check that's unnecessary. Go ahead and leave that option off.


In a comment, you asked if there was a way to raise an error if unknown keywords were present. There is no such option.

However, what you can do is check the schema.Keywords property for any that are of type UnrecognizedKeyword. If there are any, then you have extra data in the schema.

Be mindful that schemas can nest, though, so you'll need to check each level.

{
  "allOf": [
    { "unrecognized": "keyword" }
  ]
}

Here, you'll need to go find the AllOfKeyword and check its subschemas for UnrecognizedKeywords.


Aside from that, I'm going to expand on @Clemens' answer a bit to explain why your examples are coming back as valid.

{
  "f": "a"
}

When validated against a metaschema, this JSON is going to produce the same validation result as your second example {} because (as @Clemens mentioned) JSON Schema ignores unknown keywords. Since f isn't a recognized keyword, it's ignored by validation. (There's an annotation collected for f the output, though.)

Because this has no validation keywords in it, it will validate all JSON instances. Technically this is a valid schema, although it doesn't do much.

{
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5",
    "prop6"
  ]
}

Here, you're requiring certain properties to be present if the JSON instance is an object. But if the instance is not an object, required has no effect. You'll probably want to constrain values more with "type": "object".

{
  "$schema": ""http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "prop1",
    "prop2",
    "prop3",
    "prop4",
    "prop5", 
    "prop6"
  ]
}

Here you have all the pieces in place, and what you probably expect can work. The JSON is still a valid draft 7 schema (it's also valid for draft 2020-12).

In order for the schema to be invalid, you'd have to put a valid in for a defined keyword that it doesn't support, like giving maximum a string value. In this case, the schema will fail validation.

That said, if you were to try to deserialize invalid schema JSON into a JsonSchema model, the serializer will throw an exception because there's some validation that occurs during deserialization.

I think your approach of validating the schema JSON against a meta-schema is better than letting the serializer throw an exception, but you'll want to be sure you validate against the metaschema that's represented in the $schema keyword. (So don't validate a draft 2020-12 schema against the draft 7 metaschema.)

2 of 3
3

Your examples are not covered by validation against the meta schemas. The meta schemas use an open model and there is also no semantic checking. You would need a JSON schema linter like the one coming with JSONBuddy (https://www.json-buddy.com), also available at json-schema-linter.com for quick testing.

🌐
Jsonschema
jsonschema.dev
JSON Schema Validation
JSON Schema Validation, in browser, using ajv, with linting, and shareable schemas
Find elsewhere
🌐
jsonschema
python-jsonschema.readthedocs.io β€Ί en β€Ί latest β€Ί validate
Schema Validation - jsonschema 4.26.1.dev25+gad0a1b301 documentation
An object representing the validator’s meta schema (the schema that describes valid schemas in the given version). ... A jsonschema.TypeChecker that will be used when validating type keywords in JSON schemas.
🌐
Google
developers.google.com β€Ί search central β€Ί schema markup testing tool | google search central
Schema Markup Testing Tool | Google Search Central | Google for Developers
Google recommends that you start with the Rich Results Test to see what Google rich results can be generated for your page. For generic schema validation, use the Schema Markup Validator to test all types of schema.org markup, without Google-specific validation.
🌐
JSON Schema
json-schema.org
JSON Schema
Discover JSON Schema tooling to help your organization leverage the benefits of JSON Schema. Because JSON Schema is much more than a Specification, it is a vibrant ecosystem of Validators, Generators, Linters, and other JSON Schema Utilities made by this amazing Community.Explore
🌐
JSONLint
jsonlint.com β€Ί json-schema
JSON Schema Validator - Validate JSON Against Schema | JSONLint | JSONLint
Validate your JSON data against a JSON Schema. Catch structural errors, type mismatches, and missing required fields before they cause bugs.
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
JSONLint is the free online validator, json formatter, and json beautifier tool for JSON, a lightweight data-interchange format.
🌐
JSON Formatter
jsonformatter.gg β€Ί json-schema-validator
JSON Schema Validator Online | jsonformatter.gg
Free online JSON schema validator. Validate JSON instantly with secure local processing.
🌐
Linangdata
linangdata.com β€Ί json-schema-validator
JSON Schema Validator - Online Validation Tool
Free online JSON Schema validator tool. Instantly validate JSON documents against any schema with detailed error reporting. Perfect for developers working with JSON APIs and configurations.
🌐
Assertible
assertible.com β€Ί json-schema-validation
JSON Schema Validation API : Assertible
Validate your JSON documents using our simple and free JSON Schema Draft 4 validation API.
🌐
Ajv
ajv.js.org
Ajv JSON schema validator
It allows implementing complex data validation logic via declarative schemas for your JSON data, without writing code.
🌐
JSON Formatter
jsonformatter.org
Best JSON Formatter and JSON Validator: Online JSON Formatter
JSON Validator Online checks the integrity/syntax of the JSON data based on JavaScript Object Notation (JSON) Data Interchange Format Specifications (RFC).
🌐
JSON Crack
jsoncrack.com
JSON Crack | Online JSON Viewer - Transform your data into interactive graphs
Your data is never stored on our servers. Everything happens on your device. ... JSON Crack is an online JSON viewer tool designed to visualize and analyze various data formats, including JSON, YAML, CSV, XML and more.