Created
April 10, 2021 08:08
-
-
Save gregsdennis/6919d9d37ef90ef837d66550b131686b to your computer and use it in GitHub Desktop.
Possible approach for incremental schema building
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace Json.Schema | |
{ | |
/// <summary> | |
/// Provides a fluent interface for <see cref="JsonSchemaBuilder"/>. | |
/// </summary> | |
public static class IncrementalJsonSchemaBuilderExtensions | |
{ | |
/// <summary> | |
/// Add to an existing `allOf` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendAllOf(this JsonSchemaBuilder builder, JsonSchema schema) | |
{ | |
var keyword = builder.Get<AllOfKeyword>(); | |
var schemas = keyword?.Schemas.ToList() ?? new List<JsonSchema>(); | |
schemas.Add(schema); | |
builder.Add(new AllOfKeyword(schemas)); | |
return builder; | |
} | |
/// <summary> | |
/// Add to an existing `anyOf` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendAnyOf(this JsonSchemaBuilder builder, JsonSchema schema) | |
{ | |
var keyword = builder.Get<AnyOfKeyword>(); | |
var schemas = keyword?.Schemas.ToList() ?? new List<JsonSchema>(); | |
schemas.Add(schema); | |
builder.Add(new AnyOfKeyword(schemas)); | |
return builder; | |
} | |
/// <summary> | |
/// Add a definition to an existing `definitions` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="name">The definition name.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendDefinition(this JsonSchemaBuilder builder, string name, JsonSchema schema) | |
{ | |
var keyword = builder.Get<DefinitionsKeyword>(); | |
var schemas = keyword?.Definitions.ToDictionary(x => x.Key, x => x.Value) ?? | |
new Dictionary<string, JsonSchema>(); | |
schemas[name] = schema; | |
builder.Add(new DefinitionsKeyword(schemas)); | |
return builder; | |
} | |
/// <summary> | |
/// Add a definition to an existing `$defs` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="name">The definition name.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendDefs(this JsonSchemaBuilder builder, string name, JsonSchema schema) | |
{ | |
var keyword = builder.Get<DefsKeyword>(); | |
var schemas = keyword?.Definitions.ToDictionary(x => x.Key, x => x.Value) ?? | |
new Dictionary<string, JsonSchema>(); | |
schemas[name] = schema; | |
builder.Add(new DefsKeyword(schemas)); | |
return builder; | |
} | |
/// <summary> | |
/// Add to an existing `oneOf` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendOneOf(this JsonSchemaBuilder builder, JsonSchema schema) | |
{ | |
var keyword = builder.Get<OneOfKeyword>(); | |
var schemas = keyword?.Schemas.ToList() ?? new List<JsonSchema>(); | |
schemas.Add(schema); | |
builder.Add(new OneOfKeyword(schemas)); | |
return builder; | |
} | |
/// <summary> | |
/// Add a property to an existing `properties` keyword. | |
/// </summary> | |
/// <param name="builder">The builder.</param> | |
/// <param name="name">The property name.</param> | |
/// <param name="schema">The new schema.</param> | |
/// <returns>The builder.</returns> | |
public static JsonSchemaBuilder AppendProperty(this JsonSchemaBuilder builder, string name, JsonSchema schema) | |
{ | |
var keyword = builder.Get<PropertiesKeyword>(); | |
var schemas = keyword?.Properties.ToDictionary(x => x.Key, x => x.Value) ?? | |
new Dictionary<string, JsonSchema>(); | |
schemas[name] = schema; | |
builder.Add(new PropertiesKeyword(schemas)); | |
return builder; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supports a question about JsonSchema.Net.