public void Issue49_RequiredAndAllOfInSingleSchema() { var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\issue49.json").AdjustForOS(); var expected = new JsonSchema() .Title("JSON schema for Something") .Schema("http://json-schema.org/draft-04/schema#") .Definition("something", new JsonSchema() .Type(JsonSchemaType.Object) .Required("name") .AllOf(new JsonSchema() .Property("name", new JsonSchema().Type(JsonSchemaType.String))) ) .Type(JsonSchemaType.Array) .Description("An array of somethings.") .Items(new JsonSchema().Ref("#/definitions/something")); var schema = JsonSchemaRegistry.Get(fileName); Assert.AreEqual(expected, schema); var schemaJson = schema.ToJson(_serializer); var expectedJson = expected.ToJson(_serializer); Console.WriteLine(schemaJson); Assert.AreEqual(expectedJson, schemaJson); }
public void Issue50_MultipleSchemaInSubFoldersShouldReferenceRelatively() { string path = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue50A.json").AdjustForOS(); var schema = JsonSchemaRegistry.Get(path); var json = new JsonObject { ["text"] = "something", ["refa"] = new JsonObject { ["text"] = "something else", ["refb"] = new JsonObject { ["refd"] = new JsonObject { ["refe"] = new JsonObject { ["test"] = "test" }, ["text"] = "test" } } } }; var results = schema.Validate(json); results.AssertValid(); }
public void RefResolvesToInternallyStoredSchema() { var source = new JsonSchema() .Id("http://schema.org/source") .AllOf(new JsonSchema().Ref("http://schema.org/target")); var target = new JsonSchema() .Id("http://schema.org/target") .Property("test", true) .Required("test"); JsonSchemaRegistry.Register(source); JsonSchemaRegistry.Register(target); try { var instance = new JsonObject { ["test"] = "literally anything" }; var results = source.Validate(instance); results.AssertValid(); } finally { JsonSchemaRegistry.Unregister(source); JsonSchemaRegistry.Unregister(target); } }
public void Issue269_DownloadOverrideNotWorking(string draft, int expectedDownloadCount) { try { var schemaDirPath = $"{AppDomain.CurrentDomain.BaseDirectory}/Files/Issue269/{draft}/"; var schemaUriPrefix = "http://localhost/"; var jsonData = JsonValue.Parse("{\"propertyAffectedFromSubSchema\": {}}"); var serializer = new JsonSerializer(); var downloadCount = 0; JsonSchemaOptions.Download = uri => { downloadCount++; Console.WriteLine($"Downloading {uri}"); var localSchemaPath = uri.Replace(schemaUriPrefix, schemaDirPath); //Set Breakpoint var filePath = $"{localSchemaPath}.schema.json"; return(File.ReadAllText(filePath)); }; JsonSchemaRegistry.Clear(); var instanceSchemaFilePath = $"{System.IO.Path.Combine(schemaDirPath, "schemas/1.1.0/instance")}.schema.json"; var schemaJson = JsonValue.Parse(File.ReadAllText(instanceSchemaFilePath)); var schema = serializer.Deserialize <JsonSchema>(schemaJson); var validationResults = schema.Validate(jsonData); Assert.AreEqual(expectedDownloadCount, downloadCount); } finally { JsonSchemaOptions.Download = null !; } }
public void Issue173_ReferencedSchemaInParentFolder() { // this links to the commit after the one that submitted the schema files. // otherwise we have a paradox of trying to know the commit hash before the commit is created. var baseUri = "https://raw.githubusercontent.com/gregsdennis/Manatee.Json/c264db5c75478e0a33269baba7813901829f8244/Manatee.Json.Tests/Files/"; var schema = (JsonSchema07)JsonSchemaRegistry.Get($"{baseUri}Issue173/BaseSchema.json"); var invalid = new JsonObject { ["localProp"] = new JsonArray { 150, "hello", 6 } }; var valid = new JsonObject { ["localProp"] = new JsonArray { 1, 2, 3, 4 } }; var result = schema.Validate(invalid); result.AssertInvalid(); result = schema.Validate(valid); result.AssertValid(); }
/// <summary> /// Used register any subschemas during validation. Enables look-forward compatibility with <code>$ref</code> keywords. /// </summary> /// <param name="baseUri">The current base URI</param> /// <param name="localRegistry">The local registry</param> public void RegisterSubschemas(Uri baseUri, JsonSchemaRegistry localRegistry) { foreach (JsonSchema schema in Values) { schema.RegisterSubschemas(baseUri, localRegistry); } }
public void Issue58_UriReferenceSchemaTest() { const string coreSchemaUri = "http://example.org/Issue58RefCore.json"; const string childSchemaUri = "http://example.org/Issue58RefChild.json"; var coreSchemaPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue58RefCore.json").AdjustForOS(); var childSchemaPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue58RefChild.json").AdjustForOS(); string coreSchemaText; string childSchemaText; using (TextReader reader = File.OpenText(coreSchemaPath)) { coreSchemaText = reader.ReadToEnd(); } using (TextReader reader = File.OpenText(childSchemaPath)) { childSchemaText = reader.ReadToEnd(); } var requestedUris = new List <string>(); try { JsonSchemaOptions.Download = uri => { requestedUris.Add(uri); switch (uri) { case coreSchemaUri: return(coreSchemaText); case childSchemaUri: return(childSchemaText); } return(coreSchemaText); }; var schema = JsonSchemaRegistry.Get(childSchemaUri); var testJson = new JsonObject(); testJson["myProperty"] = "http://example.org/"; //Console.WriteLine(testJson); //Console.WriteLine(schema.ToJson(null).GetIndentedString()); var result = schema.Validate(testJson); result.AssertValid(); Assert.AreEqual(requestedUris[0], childSchemaUri); Assert.AreEqual(requestedUris[1], coreSchemaUri); } finally { JsonSchemaOptions.Download = null; } }
public void Issue45a_Utf8SupportInReferenceSchemaEnums() { var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\baseSchema.json").AdjustForOS(); const string jsonString = "{\"prop1\": \"ændring\", \"prop2\": {\"prop3\": \"ændring\"}}"; var schema = JsonSchemaRegistry.Get(fileName); var json = JsonValue.Parse(jsonString); var result = schema.Validate(json); result.AssertValid(); }
private static IJsonSchema _GetFileSchema(SchemaAttribute attribute) { var uri = attribute.Source; if (!Uri.TryCreate(uri, UriKind.Absolute, out _)) { uri = System.IO.Path.Combine(Directory.GetCurrentDirectory(), uri); } var schema = JsonSchemaRegistry.Get(uri); return(schema); }
public void Issue232_RefsNotResolvedAgainstId() { var resistanceSchema = new JsonSchema() .Schema(MetaSchemas.Draft07.Id) .Id("http://localhost/Resistance.schema.json") .Type(JsonSchemaType.Object) .Required("t_PulseTimePeriod") .Property("t_PulseTimePeriod", new JsonSchema().Ref("./definitions.schema.json#definitions/t_PulseTimePeriod")) .AdditionalProperties(false); var definitionsSchema = new JsonSchema() .Schema(MetaSchemas.Draft07.Id) .Id("http://localhost/definitions.schema.json") .Definition("1DVector", new JsonSchema() .Id("#1DVector") .Type(JsonSchemaType.Array) .MinItems(1) .Items(new JsonSchema() .AnyOf(new JsonSchema().Type(JsonSchemaType.Number), new JsonSchema().Type(JsonSchemaType.Null)) .Default(JsonValue.Null)) .Examples(1.1, 2.3, 4.5)) .Definition("t_PulseTimePeriod", new JsonSchema() .Id("#t_PulseTimePeriod") .Type(JsonSchemaType.Object) .Title("The t_PulseTimePeriod Schema") .Required("values") .Property("values", new JsonSchema().Ref("#/definitions/1DVector"))); JsonSchemaRegistry.Register(resistanceSchema); JsonSchemaRegistry.Register(definitionsSchema); var instance = new JsonObject { ["t_PulseTimePeriod"] = new JsonObject { ["values"] = new JsonArray() } }; var format = JsonSchemaOptions.OutputFormat; try { JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Detailed; var result = resistanceSchema.Validate(instance); result.AssertInvalid(); } finally { JsonSchemaOptions.OutputFormat = format; } }
public void RecursiveRefResolvesOuterAnchor_BrokenChain() { var source = new JsonSchema() .Id("http://schema.org/source") .RecursiveAnchor(true) .OneOf(new JsonSchema() .Property("nested", new JsonSchema() .Ref("http://schema.org/middle")) .Required("nested"), new JsonSchema() .Property("test", true) .Required("test")); var middle = new JsonSchema() .Id("http://schema.org/middle") .OneOf(new JsonSchema() .Ref("http://schema.org/target"), new JsonSchema() .Property("middle", false) .Required("middle")); var target = new JsonSchema() .Id("http://schema.org/target") .RecursiveAnchor(true) .AllOf(new JsonSchema().RecursiveRefRoot()); JsonSchemaRegistry.Register(source); JsonSchemaRegistry.Register(middle); JsonSchemaRegistry.Register(target); try { var instance = new JsonObject { ["nested"] = new JsonObject { ["test"] = "literally anything" } }; var results = source.Validate(instance); results.AssertValid(); } finally { JsonSchemaRegistry.Unregister(source); JsonSchemaRegistry.Unregister(middle); JsonSchemaRegistry.Unregister(target); } }
public void Issue49_RequiredAndAllOfInSingleSchema() { var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\issue49.json").AdjustForOS(); var expected = new JsonSchema04 { Title = "JSON schema for Something", Schema = "http://json-schema.org/draft-04/schema#", Definitions = new Dictionary <string, IJsonSchema> { ["something"] = new JsonSchema04 { Type = JsonSchemaType.Object, AllOf = new[] { new JsonSchema04 { Properties = new Dictionary <string, IJsonSchema> { ["name"] = new JsonSchema04 { Type = JsonSchemaType.String } } } }, Required = new List <string> { "name" } } }, Type = JsonSchemaType.Array, Description = "An array of somethings.", Items = new JsonSchemaReference("#/definitions/something", typeof(JsonSchema04)) }; var schema = JsonSchemaRegistry.Get(fileName); Assert.AreEqual(expected, schema); var schemaJson = schema.ToJson(null); var expectedJson = expected.ToJson(null); Console.WriteLine(schemaJson); Assert.AreEqual(expectedJson, schemaJson); }
public void Issue173_ReferencedSchemaInParentFolder() { try { // this links to the commit after the one that submitted the schema files. // otherwise we have a paradox of trying to know the commit hash before the commit is created. var baseUri = "https://raw.githubusercontent.com/gregsdennis/Manatee.Json/c264db5c75478e0a33269baba7813901829f8244/Manatee.Json.Tests/Files/"; var schema = JsonSchemaRegistry.Get($"{baseUri}Issue173/BaseSchema.json"); var invalid = new JsonObject { ["localProp"] = new JsonArray { 150, "hello", 6 } }; var valid = new JsonObject { ["localProp"] = new JsonArray { 1, 2, 3, 4 } }; var result = schema.Validate(invalid); result.AssertInvalid(); result = schema.Validate(valid); result.AssertValid(); } catch (WebException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any() || e.InnerExceptions.OfType <HttpRequestException>().Any()) { Assert.Inconclusive(); } throw; } }
public static OpenApiDocument ManateeRegisterJsonSchema(string _specLocation) { var exFiles = ""; foreach (var file in Directory.GetFiles(_specLocation)) { //var textFromOpenApiSchemaFile = File.ReadAllText(file); try { JsonSchemaRegistry.Register(JsonSchemaRegistry.Get(file)); } catch (Exception ex) { exFiles += file + "\n\n"; exFiles += ex.ToString() + "\n\n"; } } return(null); }
public void Issue45b_Utf8SupportInReferenceSchemaEnums() { var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\baseSchema.json").AdjustForOS(); const string jsonString = "{\"prop1\": \"ændring\", \"prop2\": {\"prop3\": \"ændring\"}}"; var schema = JsonSchemaRegistry.Get(fileName); var json = JsonValue.Parse(jsonString); var result = schema.Validate(json); foreach (var error in result.NestedResults) { Console.WriteLine(error); } Console.WriteLine(schema.ToJson(_serializer)); var refSchema = schema.Properties()["prop2"].RefResolved(); Console.WriteLine(refSchema.ToJson(_serializer)); Console.WriteLine(json); result.AssertValid(); }
/// <summary> /// Used register any subschemas during validation. Enables look-forward compatibility with <code>$ref</code> keywords. /// </summary> /// <param name="baseUri">The current base URI</param> /// <param name="localRegistry">The local registry</param> public void RegisterSubschemas(Uri baseUri, JsonSchemaRegistry localRegistry) { // Not yet implemented }