-
-
Notifications
You must be signed in to change notification settings - Fork 149
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 #270 from gregsdennis/schema/unknown-keywords
added a keyword impl for unrecognized keywords instead of otherdata
- Loading branch information
Showing
8 changed files
with
224 additions
and
59 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
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,46 @@ | ||
using System.Linq; | ||
using System.Text.Json; | ||
using Json.More; | ||
using NUnit.Framework; | ||
|
||
namespace Json.Schema.Tests; | ||
|
||
public class UnrecognizedKeywordTests | ||
{ | ||
[Test] | ||
public void FooIsNotAKeyword() | ||
{ | ||
var schemaText = "{\"foo\": \"bar\"}"; | ||
|
||
var schema = JsonSerializer.Deserialize<JsonSchema>(schemaText); | ||
|
||
Assert.AreEqual(1, schema!.Keywords!.Count); | ||
Assert.IsInstanceOf<UnrecognizedKeyword>(schema.Keywords.First()); | ||
} | ||
|
||
[Test] | ||
public void FooProducesAnAnnotation() | ||
{ | ||
var schemaText = "{\"foo\": \"bar\"}"; | ||
|
||
var schema = JsonSerializer.Deserialize<JsonSchema>(schemaText); | ||
|
||
var result = schema!.Validate("{}", new ValidationOptions { OutputFormat = OutputFormat.Detailed }); | ||
|
||
Assert.IsTrue(result.IsValid); | ||
Assert.AreEqual(1, result.Annotations.Count()); | ||
Assert.IsTrue("bar".AsJsonElement().IsEquivalentTo((JsonElement)result.Annotations.First().Value)); | ||
} | ||
|
||
[Test] | ||
public void FooIsIncludedInSerialization() | ||
{ | ||
var schemaText = "{\"foo\":\"bar\"}"; | ||
|
||
var schema = JsonSerializer.Deserialize<JsonSchema>(schemaText); | ||
|
||
var reText = JsonSerializer.Serialize(schema); | ||
|
||
Assert.AreEqual(schemaText, reText); | ||
} | ||
} |
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
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
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
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
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,92 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Json.More; | ||
|
||
namespace Json.Schema; | ||
|
||
/// <summary> | ||
/// Handles unrecognized keywords. | ||
/// </summary> | ||
[SchemaDraft(Draft.Draft6)] | ||
[SchemaDraft(Draft.Draft7)] | ||
[SchemaDraft(Draft.Draft201909)] | ||
[SchemaDraft(Draft.Draft202012)] | ||
[JsonConverter(typeof(UnrecognizedKeywordJsonConverter))] | ||
public class UnrecognizedKeyword : IJsonSchemaKeyword, IEquatable<UnrecognizedKeyword> | ||
{ | ||
/// <summary> | ||
/// The name or key of the keyword. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// The value of the keyword. | ||
/// </summary> | ||
public JsonElement Value { get; } | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="UnrecognizedKeyword"/>. | ||
/// </summary> | ||
/// <param name="name">The name of the keyword.</param> | ||
/// <param name="value">Whether items should be unique.</param> | ||
public UnrecognizedKeyword(string name, JsonElement value) | ||
{ | ||
Name = name; | ||
Value = value.Clone(); | ||
} | ||
|
||
/// <summary> | ||
/// Provides validation for the keyword. | ||
/// </summary> | ||
/// <param name="context">Contextual details for the validation process.</param> | ||
public void Validate(ValidationContext context) | ||
{ | ||
context.EnterKeyword(Name); | ||
context.LocalResult.SetAnnotation(Name, Value); | ||
context.LocalResult.Pass(); | ||
context.ExitKeyword(Name, context.LocalResult.IsValid); | ||
} | ||
|
||
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary> | ||
/// <param name="other">An object to compare with this object.</param> | ||
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns> | ||
public bool Equals(UnrecognizedKeyword? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return Name == other.Name && Value.IsEquivalentTo(other.Value); | ||
} | ||
|
||
/// <summary>Determines whether the specified object is equal to the current object.</summary> | ||
/// <param name="obj">The object to compare with the current object.</param> | ||
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns> | ||
public override bool Equals(object obj) | ||
{ | ||
return Equals(obj as UnrecognizedKeyword); | ||
} | ||
|
||
/// <summary>Serves as the default hash function.</summary> | ||
/// <returns>A hash code for the current object.</returns> | ||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return (Name.GetHashCode() * 397) ^ Value.GetHashCode(); | ||
} | ||
} | ||
} | ||
|
||
internal class UnrecognizedKeywordJsonConverter : JsonConverter<UnrecognizedKeyword> | ||
{ | ||
public override UnrecognizedKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException("Unrecognized keywords should be handled manually during JsonSchema deserialization."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, UnrecognizedKeyword value, JsonSerializerOptions options) | ||
{ | ||
writer.WritePropertyName(value.Name); | ||
writer.WriteValue(value.Value); | ||
} | ||
} |
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