RicoSuter / NJsonSchema

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

How can I create a schema that uses URLs as $ref value? #1722

Open s2quake opened 1 month ago

s2quake commented 1 month ago

Hi I would like to use NJsonSchema to generate the following schema. However, no matter what I tried, I was unable to generate the following schema.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "$ref": "https://json.schemastore.org/appsettings.json"
    }
  ]
}

The code below is the code I tried to create the schema like above. How can I create a schema like the one above?

public string ToJson()
{
    var appsettingsUrl = "https://json.schemastore.org/appsettings.json";
    var appsettingsSchema = new JsonSchema
    {
        DocumentPath = appsettingsUrl,
        Id = appsettingsUrl,
    };

    var appSchema = new JsonSchema
    {
        SchemaVersion = "http://json-schema.org/draft-07/schema#",
        AllOf =
        {
            new JsonSchema
            {
                Reference = appsettingsSchema,
            },
        },
    };

    return appSchema.ToJson();
}