Skip to content

Commit

Permalink
Merge pull request #93 from gregsdennis/v1.10.0
Browse files Browse the repository at this point in the history
incremented version
  • Loading branch information
gregsdennis committed Apr 10, 2021
2 parents 2973974 + 7d35815 commit 7a6930e
Show file tree
Hide file tree
Showing 75 changed files with 657 additions and 49 deletions.
1 change: 1 addition & 0 deletions JsonSchema.Tests/Suite/Suite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void Test(TestCollection collection, TestCase test, string fileName, Vali
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
Console.WriteLine();
Console.WriteLine(JsonSerializer.Serialize(collection.Schema, serializerOptions));
Console.WriteLine();
Console.WriteLine(test.Data.ToJsonString());
Expand Down
14 changes: 14 additions & 0 deletions JsonSchema.Tests/TestEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NUnit.Framework;

namespace Json.Schema.Tests
{
[SetUpFixture]
public class TestEnvironment
{
[OneTimeSetUp]
public void Setup()
{
ValidationOptions.Default.Log = new TestLog();
}
}
}
14 changes: 14 additions & 0 deletions JsonSchema.Tests/TestLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Linq;

namespace Json.Schema.Tests
{
public class TestLog : ILog
{
public void Write(Func<string> log, int indent)
{
var indentString = indent == 0 ? null : string.Concat(Enumerable.Repeat(" ", indent));
Console.WriteLine($"{indentString}{log()}");
}
}
}
10 changes: 10 additions & 0 deletions JsonSchema/AdditionalItemsKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,50 @@ public AdditionalItemsKeyword(JsonSchema value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.Array)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}

context.Options.LogIndentLevel++;
var overallResult = true;
var annotation = context.TryGetAnnotation(ItemsKeyword.Name);
if (annotation == null)
{
context.NotApplicable(() => $"No annotations from {ItemsKeyword.Name}.");
context.IsValid = true;
return;
}
context.Log(() => $"Annotation from {ItemsKeyword.Name}: {annotation}.");
if (annotation is bool)
{
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
return;
}
var startIndex = (int) annotation;

for (int i = startIndex; i < context.LocalInstance.GetArrayLength(); i++)
{
context.Log(() => $"Validating item at index {i}.");
var item = context.LocalInstance[i];
var subContext = ValidationContext.From(context,
context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
item);
Schema.ValidateSubschema(subContext);
overallResult &= subContext.IsValid;
context.Log(() => $"Item at index {i} {subContext.IsValid.GetValidityString()}.");
if (!overallResult && context.ApplyOptimizations) break;
}
context.Options.LogIndentLevel--;

if (overallResult)
context.SetAnnotation(Name, true);
context.IsValid = overallResult;
context.ExitKeyword(Name, context.IsValid);
}

private static void ConsolidateAnnotations(IEnumerable<ValidationContext> sourceContexts, ValidationContext destContext)
Expand Down
37 changes: 32 additions & 5 deletions JsonSchema/AdditionalPropertiesKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,59 @@ public AdditionalPropertiesKeyword(JsonSchema value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.Object)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}

context.Options.LogIndentLevel++;
var overallResult = true;
var annotation = context.TryGetAnnotation(PropertiesKeyword.Name);
var evaluatedProperties = (annotation as List<string>)?.ToList() ?? new List<string>();
annotation = context.TryGetAnnotation(PatternPropertiesKeyword.Name);
evaluatedProperties.AddRange(annotation as List<string> ?? Enumerable.Empty<string>());
var annotation = (context.TryGetAnnotation(PropertiesKeyword.Name) as List<string>)?.ToList();
List<string> evaluatedProperties;
if (annotation == null)
{
context.Log(() => $"No annotation from {PropertiesKeyword.Name}.");
evaluatedProperties = new List<string>();
}
else
{
context.Log(() => $"Annotation from {PropertiesKeyword.Name}: [{string.Join(",", annotation.Select(x => $"'{x}'"))}]");
evaluatedProperties = annotation;
}
annotation = (context.TryGetAnnotation(PatternPropertiesKeyword.Name) as List<string>)?.ToList();
if (annotation == null)
context.Log(() => $"No annotation from {PatternPropertiesKeyword.Name}.");
else
{
context.Log(() => $"Annotation from {PatternPropertiesKeyword.Name}: [{string.Join(",", annotation.Select(x => $"'{x}'"))}]");
evaluatedProperties.AddRange(annotation);
}
var additionalProperties = context.LocalInstance.EnumerateObject().Where(p => !evaluatedProperties.Contains(p.Name)).ToList();
evaluatedProperties.Clear();
foreach (var property in additionalProperties)
{
if (!context.LocalInstance.TryGetProperty(property.Name, out var item)) continue;
if (!context.LocalInstance.TryGetProperty(property.Name, out var item))
{
context.Log(() => $"Property '{property.Name}' does not exist. Skipping.");
continue;
}

context.Log(() => $"Validating property '{property.Name}'.");
var subContext = ValidationContext.From(context,
context.InstanceLocation.Combine(PointerSegment.Create($"{property.Name}")),
item);
Schema.ValidateSubschema(subContext);
overallResult &= subContext.IsValid;
context.Log(() => $"Property '{property.Name}' {subContext.IsValid.GetValidityString()}.");
if (subContext.IsValid)
evaluatedProperties.Add(property.Name);
else if (context.ApplyOptimizations) break;
context.NestedContexts.Add(subContext);
}
context.Options.LogIndentLevel--;

if (overallResult)
{
Expand All @@ -84,6 +110,7 @@ public void Validate(ValidationContext context)
context.SetAnnotation(Name, evaluatedProperties);
}
context.IsValid = overallResult;
context.ExitKeyword(Name, context.IsValid);
}

private static void ConsolidateAnnotations(IEnumerable<ValidationContext> sourceContexts, ValidationContext destContext)
Expand Down
4 changes: 4 additions & 0 deletions JsonSchema/AllOfKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ public AllOfKeyword(IEnumerable<JsonSchema> values)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
var overallResult = true;
for (var i = 0; i < Schemas.Count; i++)
{
context.Log(() => $"Processing {Name}[{i}]...");
var schema = Schemas[i];
var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
schema.ValidateSubschema(subContext);
overallResult &= subContext.IsValid;
context.Log(() => $"{Name}[{i}] {subContext.IsValid.GetValidityString()}.");
if (!overallResult && context.ApplyOptimizations) break;
context.NestedContexts.Add(subContext);
}

context.ConsolidateAnnotations();
context.IsValid = overallResult;
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/AnchorKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public AnchorKeyword(string anchor)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
}

void IAnchorProvider.RegisterAnchor(SchemaRegistry registry, Uri currentUri, JsonSchema schema)
Expand Down
5 changes: 5 additions & 0 deletions JsonSchema/AnyOfKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,24 @@ public AnyOfKeyword(IEnumerable<JsonSchema> values)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
var overallResult = false;
for (var i = 0; i < Schemas.Count; i++)
{
context.Log(() => $"Processing {Name}[{i}]...");
var schema = Schemas[i];
var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
schema.ValidateSubschema(subContext);
overallResult |= subContext.IsValid;
context.Log(() => $"{Name}[{i}] {subContext.IsValid.GetValidityString()}.");
if (overallResult && context.ApplyOptimizations) break;
context.NestedContexts.Add(subContext);
}

if (overallResult)
context.ConsolidateAnnotations();
context.IsValid = overallResult;
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/CommentKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public CommentKeyword(string value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.SetAnnotation(Name, Value);
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
}

/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/ConstKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public ConstKeyword(JsonElement value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.IsValid = Value.IsEquivalentTo(context.LocalInstance);
if (!context.IsValid)
context.Message = "Expected value to match given value";
context.ExitKeyword(Name, context.IsValid);
}

/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
Expand Down
3 changes: 3 additions & 0 deletions JsonSchema/ContainsKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public ContainsKeyword(JsonSchema value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.Array)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}
Expand All @@ -68,6 +70,7 @@ public void Validate(ValidationContext context)
context.SetAnnotation(Name, found);
else
context.Message = "Expected array to contain at least one item that matched the schema, but it did not";
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down
3 changes: 3 additions & 0 deletions JsonSchema/ContentMediaEncodingKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public ContentMediaEncodingKeyword(string value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.String)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}

context.SetAnnotation(Name, Value);
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
}

/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
Expand Down
3 changes: 3 additions & 0 deletions JsonSchema/ContentMediaTypeKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public ContentMediaTypeKeyword(string value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.String)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}

context.SetAnnotation(Name, Value);
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
}

/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
Expand Down
3 changes: 3 additions & 0 deletions JsonSchema/ContentSchemaKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public ContentSchemaKeyword(JsonSchema value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
if (context.LocalInstance.ValueKind != JsonValueKind.String)
{
context.WrongValueKind(context.LocalInstance.ValueKind);
context.IsValid = true;
return;
}
Expand All @@ -51,6 +53,7 @@ public void Validate(ValidationContext context)
context.NestedContexts.Add(subContext);
context.IsValid = subContext.IsValid;
context.ConsolidateAnnotations();
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/DefaultKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public DefaultKeyword(JsonElement value)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.SetAnnotation(Name, Value);
context.IsValid = true;
context.ExitKeyword(Name, context.IsValid);
}

/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/DefinitionsKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public DefinitionsKeyword(IReadOnlyDictionary<string, JsonSchema> values)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.IsValid = true;
context.Ignore = true;
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema/DefsKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public DefsKeyword(IReadOnlyDictionary<string, JsonSchema> values)
/// <param name="context">Contextual details for the validation process.</param>
public void Validate(ValidationContext context)
{
context.EnterKeyword(Name);
context.IsValid = true;
context.Ignore = true;
context.ExitKeyword(Name, context.IsValid);
}

IRefResolvable? IRefResolvable.ResolvePointerSegment(string? value)
Expand Down

0 comments on commit 7a6930e

Please sign in to comment.