Skip to content

Commit

Permalink
replicate second example
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Jun 7, 2023
1 parent ebeca54 commit 8eda592
Showing 1 changed file with 119 additions and 75 deletions.
194 changes: 119 additions & 75 deletions JsonSchema.Tests/BaseDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,158 @@
using FluentAssertions;
using NUnit.Framework;

namespace Json.Schema.Tests
namespace Json.Schema.Tests;

public class BaseDocumentTests
{
public class BaseDocumentTests
[Test]
public void SchemasEmbeddedInJsonCanBeReferenced_Valid()
{
[Test]
public void SchemasEmbeddedInJsonCanBeReferenced_Valid()
{
JsonSchema targetSchema = new JsonSchemaBuilder()
.Type(SchemaValueType.Integer);
JsonSchema targetSchema = new JsonSchemaBuilder()
.Type(SchemaValueType.Integer);

var json = new JsonObject
var json = new JsonObject
{
["prop1"] = "foo",
["prop2"] = new JsonArray
{
["prop1"] = "foo",
["prop2"] = new JsonArray
{
"bar",
JsonSerializer.SerializeToNode(targetSchema)
}
};
"bar",
JsonSerializer.SerializeToNode(targetSchema)
}
};

var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};
var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};

var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);

JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop2/1");
JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop2/1");

JsonNode instance = 42;
JsonNode instance = 42;

var result = subjectSchema.Evaluate(instance, options);
var result = subjectSchema.Evaluate(instance, options);

result.AssertValid();
}
result.AssertValid();
}

[Test]
public void SchemasEmbeddedInJsonCanBeReferenced_Invalid()
[Test]
public void SchemasEmbeddedInJsonCanBeReferenced_Invalid()
{
JsonSchema targetSchema = new JsonSchemaBuilder()
.Type(SchemaValueType.Integer);

var json = new JsonObject
{
["prop1"] = "foo",
["prop2"] = new JsonArray
{
"bar",
JsonSerializer.SerializeToNode(targetSchema)
}
};

var options = new EvaluationOptions
{
JsonSchema targetSchema = new JsonSchemaBuilder()
.Type(SchemaValueType.Integer);
OutputFormat = OutputFormat.List
};

var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);

JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop2/1");

JsonNode instance = "baz"!;

var result = subjectSchema.Evaluate(instance, options);

var json = new JsonObject
result.AssertInvalid();
}

[Test]
public void ReferencesFromWithinEmbeddedSchemas()
{
var json = new JsonObject
{
["prop1"] = "foo",
["prop2"] = new JsonArray
{
["prop1"] = "foo",
["prop2"] = new JsonArray
"bar",
new JsonObject
{
"bar",
JsonSerializer.SerializeToNode(targetSchema)
["type"] = "integer"
}
};

var options = new EvaluationOptions
},
["prop3"] = new JsonObject
{
OutputFormat = OutputFormat.List
};
["$ref"] = "#/prop2/1"
}
};

var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};

var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);

JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop2/1");
JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop3");

JsonNode instance = "baz"!;
JsonNode instance = 42;

var result = subjectSchema.Evaluate(instance, options);
var result = subjectSchema.Evaluate(instance, options);

result.AssertInvalid();
}
result.IsValid.Should().BeTrue();
}

[Test]
public void ReferencesFromWithinEmbeddedSchemas()
[Test]
public void NestedReferencesFromWithinEmbeddedSchemas()
{
var json = new JsonObject
{
var json = new JsonObject
["prop1"] = "foo",
["prop2"] = new JsonArray
{
"bar",
new JsonObject
{
["type"] = "integer"
}
},
["prop3"] = new JsonObject
{
["prop1"] = "foo",
["prop2"] = new JsonArray
["properties"] = new JsonObject
{
"bar",
new JsonObject
["data"] = new JsonObject
{
["type"] = "integer"
["$ref"] = "#/prop2/1"
}
},
["prop3"] = new JsonObject
{
["$ref"] = "#/prop2/1"

}
};
}
};

var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};
var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};

var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);

JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop3");
JsonSchema subjectSchema = new JsonSchemaBuilder()
.Ref("http://localhost:1234/doc#/prop3");

JsonNode instance = 42;
JsonNode instance = new JsonObject { ["data"] = 42 };

var result = subjectSchema.Evaluate(instance, options);
var result = subjectSchema.Evaluate(instance, options);

result.IsValid.Should().BeTrue();
}
result.IsValid.Should().BeTrue();
}
}
}

0 comments on commit 8eda592

Please sign in to comment.