Skip to content

Commit

Permalink
Merge pull request #238 from WeihanLi/json-schema-extensions
Browse files Browse the repository at this point in the history
Add JsonSchemaExtensions
  • Loading branch information
gregsdennis committed Mar 24, 2022
2 parents c100969 + 93f27fe commit e3eaef6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions JsonSchema/JsonSchemaExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json;

namespace Json.Schema
{
/// <summary>
/// Some extensions for <see cref="JsonSchema"/>
/// </summary>
public static class JsonSchemaExtensions
{
/// <summary>
/// Validate JsonDocument with jsonSchema
/// </summary>
/// <param name="jsonSchema">jsonSchema</param>
/// <param name="jsonDocument">jsonDocument to be validated</param>
/// <param name="validationOptions">validationOptions for validate</param>
/// <returns>The validation result <see cref="ValidationResults"/> for the provided jsonDocument</returns>
public static ValidationResults Validate(this JsonSchema jsonSchema, JsonDocument jsonDocument, ValidationOptions? validationOptions = null)
{
return jsonSchema.Validate(jsonDocument.RootElement, validationOptions);
}

/// <summary>
/// Validate json string with jsonSchema
/// </summary>
/// <param name="jsonSchema">jsonSchema</param>
/// <param name="jsonString">json string to be validated</param>
/// <param name="validationOptions">validationOptions for validate</param>
/// <returns>The validation result <see cref="ValidationResults"/> for the provided json string</returns>
public static ValidationResults Validate(this JsonSchema jsonSchema, string jsonString, ValidationOptions? validationOptions = null)
{
using var jsonDocument = JsonDocument.Parse(jsonString);
return jsonSchema.Validate(jsonDocument, validationOptions);
}
}
}

0 comments on commit e3eaef6

Please sign in to comment.