RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.38k stars 532 forks source link

Unable to Validate JSON Schemas #1667

Open James-King-Shell opened 8 months ago

James-King-Shell commented 8 months ago

When attempting to validate a JSON schema itself using the JSON Schema schema or meta-schema e.g. from https://json-schema.org/draft/2020-12/schema, an error occurs when loading this schema into NJsonSchema due to an issue with the const keyword in a referenced schema.

Repro

var schemaJson = @"
{
  ""$id"": ""https://example.com/person.schema.json"",
  ""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
  ""title"": ""Person"",
  ""type"": ""object"",
  ""properties"": {
    ""firstName"": {
      ""type"": ""string"",
      ""description"": ""The person's first name.""
    },
    ""lastName"": {
      ""type"": ""string"",
      ""description"": ""The person's last name.""
    },
    ""age"": {
      ""description"": ""Age in years which must be equal to or greater than zero."",
      ""type"": ""integer"",
      ""minimum"": 0
    }
  }
}";

// Load this schema
var schema = await JsonSchema.FromJsonAsync(schemaJson);

// Load the JSON Schema schema using the schema version e.g. https://json-schema.org/draft/2020-12/schema
var metaSchema = await JsonSchema.FromUrlAsync(schema.SchemaVersion);

// Validate the JSON schema using the meta schema
var validator = new JsonSchemaValidator();
var result = validator.Validate(schemaJson, metaSchema);

The line JsonSchema.FromUrlAsync(schema.SchemaVersion); results in the following exception:

image

The problem seems to arise from the meta/validation referenced schema which, when NJsonSchema resolves and attempts to load it. blows up when it encounters const: true

image