openapijsonschemajson-schema-validator

Use OpenAPI 3.1 Schema to validate an OpenAPI spec


I've been using the OpenAPI 3.0 schema to validate OpenAPI 3.0 specifications. However, I don't seem to be able to use the OpenAPI 3.1 schema to validate an OpenAPI 3.1 specification.

With OpenAPI 3.0 I can use this online JSON Validator with the schema/2021-09-28 schema as from the OpenAPI Initiative Registry.

For OpenAPI 3.1 I downloaded the schema-base/2022-10-07 schema from this same page. When I paste it into the online JSON Validator and run it against an OpenAPI spec, it reports the following error:

Could not read the JSON Schema : virtual://server/schema.json Error when resolving schema reference '#/$defs/dialect'. Path 'properties.jsonSchemaDialect', line 9, position 26.

In my .NET code I've tried doing the JSON validation using both JsonSchema.Net and Newtonsoft.Json.Schema. Neither of these libraries are able to load the JSON Schema as they fail to resolve this schema reference: https://spec.openapis.org/oas/3.1/schema/2022-10-07#/$defs/parameter/dependentSchemas/schema/$defs/styles-for-path

What am I missing?


Solution

  • I got the validation working in .NET by using JsonSchema.Net.OpenApi as suggested by @Jeremy Fiel and @gregsdennis. Thank you!

    I created a fresh .NET 8.0 console application and installed JsonSchema.Net.OpenApi 3.1.0.

    In order to get it to work I had to do the following:

    Here's my working C# code. The OpenAPI-3.1-Schema.json contains the schema-base/2022-02-27 schema from the OpenAPI Initiative Registry.

    using System.Text.Json.Nodes;
    using Json.Schema;
    using OpenApi = Json.Schema.OpenApi;
    
    OpenApi.Vocabularies.Register();
    SchemaRegistry.Global.Register(OpenApi.MetaSchemas.OpenApiDialect);
    SchemaRegistry.Global.Register(OpenApi.MetaSchemas.DocumentSchema);
    
    var openApiSchema = JsonSchema.FromFile("OpenAPI-3.1-Schema.json");
    
    using var definitionStream = File.OpenRead("MyDefinitionUsingOpenApi3.1.json");
    var jsonNode = JsonNode.Parse(definitionStream);
    var evaluationResults = openApiSchema.Evaluate(jsonNode);
    
    if (!evaluationResults.IsValid)
    {
        Console.WriteLine("Validation Failed!");
    }