-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from WeihanLi/json-schema-extensions
Add JsonSchemaExtensions
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
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
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); | ||
} | ||
} | ||
} |