public void Online(IJsonSchema schema) { try { // TODO: Catch web exceptions and assert inconclusive. var localSchemaJson = schema.ToJson(null); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = JsonSchemaFactory.FromJson(onlineSchemaJson); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); Assert.AreEqual(onlineSchema, schema); onlineValidation.AssertValid(); localValidation.AssertValid(); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (WebException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any()) { Assert.Inconclusive(); } throw; } }
public void DeserializeSchema_TypePropertyIsArray_Issue14() { JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema04>(); var text = "{\"type\":\"object\",\"properties\":{\"home\":{\"type\":[\"object\",\"null\"],\"properties\":{\"street\":{\"type\":\"string\"}}}}}"; var json = JsonValue.Parse(text); var expected = new JsonSchema04 { Type = JsonSchemaType.Object, Properties = new Dictionary <string, IJsonSchema> { ["home"] = new JsonSchema04 { Type = JsonSchemaType.Object | JsonSchemaType.Null, Properties = new Dictionary <string, IJsonSchema> { ["street"] = new JsonSchema04 { Type = JsonSchemaType.String } } } } }; var actual = JsonSchemaFactory.FromJson(json); Assert.AreEqual(expected, actual); }
public void QuasiSchema7IfFalseElseFalse() { JsonSchemaFactory.RegisterExtendedSchema(QuasiSchema7.MetaQuasi.Id, () => new QuasiSchema7(), QuasiSchema7.MetaQuasi); JsonSchemaPropertyValidatorFactory.RegisterValidator(new QuasiSchema7IfThenElseValidator()); var schema = new QuasiSchema7 { Schema = QuasiSchema7.MetaQuasi.Id, If = new QuasiSchema7 { Type = JsonSchemaType.Integer }, Then = JsonSchema06.False, Else = new QuasiSchema7 { Type = JsonSchemaType.String } }; JsonValue json = false; var results = schema.Validate(json); results.AssertInvalid(); json = QuasiSchema7.MetaQuasi.ToJson(new JsonSerializer()); Console.WriteLine(json.GetIndentedString()); }
public ActionParameterSchemas(ApplicationModel applicationModel, HypermediaExtensionsOptions hypermediaOptions) { var actionParameterTypes = applicationModel.ActionParameterTypes.Values.Select(_ => _.Type); schemaByTypeName = actionParameterTypes.ToImmutableDictionary( t => t.BeautifulName(), t => JsonSchemaFactory.Generate(t).GetAwaiter().GetResult(), hypermediaOptions.CaseSensitiveParameterMatching ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase ); }
public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root) { var errors = new List <SchemaValidationError>(); var definitions = json.Object["definitions"]; if (definitions.Type != JsonValueType.Object) { errors.Add(new SchemaValidationError("definitions", "Property 'definitions' must contain an object.")); } var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType()); foreach (var value in definitions.Object.Values) { errors.AddRange(metaSchema.Validate(value).Errors); } return(new SchemaValidationResults(errors)); }
static void Main(string[] args) { var json = new JsonObject { ["test1"] = 1, ["test2"] = "hello" }; var serializer = new JsonSerializer(); var obj = serializer.Deserialize <ITest>(json); System.Console.WriteLine(obj.Test1); System.Console.WriteLine(obj.Test2); var schema04json = JsonSchema04.MetaSchema.ToJson(null); var schema04 = JsonSchemaFactory.FromJson(schema04json); System.Console.ReadLine(); }
public void DeclaredTypeWithDeclaredEnum_Issue15() { JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema04>(); var text = "{\"type\":\"string\",\"enum\":[\"FeatureCollection\"]}"; var json = JsonValue.Parse(text); var expected = new JsonSchema04 { Type = JsonSchemaType.String, Enum = new List <EnumSchemaValue> { new EnumSchemaValue("FeatureCollection") } }; var actual = JsonSchemaFactory.FromJson(json); Assert.AreEqual(expected, actual); }
public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root) { var errors = new List <SchemaValidationError>(); var definitions = json.Object["definitions"]; if (definitions.Type != JsonValueType.Object) { var message = SchemaErrorMessages.Definitions.ResolveTokens(new Dictionary <string, object> { ["value"] = json }); errors.Add(new SchemaValidationError(schema, "definitions", message)); } var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType()); foreach (var value in definitions.Object.Values) { errors.AddRange(metaSchema.Validate(value).Errors); } return(new SchemaValidationResults(errors)); }
private static void _Run <T>(string fileName, JsonValue testJson, JsonValue schemaJson) where T : IJsonSchema { try { JsonSchemaFactory.SetDefaultSchemaVersion <T>(); var test = _serializer.Deserialize <SchemaTest>(testJson); var schema = _serializer.Deserialize <IJsonSchema>(schemaJson); var results = schema.Validate(test.Data); if (test.Valid != results.Valid) { Console.WriteLine(string.Join("\n", results.Errors)); } Assert.AreEqual(test.Valid, results.Valid); } catch (Exception) { Console.WriteLine(fileName); throw; } }
public void QuasiSchema7IfTrueThenTrue() { JsonSchemaFactory.RegisterExtendedSchema(QuasiSchema7.MetaQuasi.Id, () => new QuasiSchema7(), QuasiSchema7.MetaQuasi); JsonSchemaPropertyValidatorFactory.RegisterValidator(new QuasiSchema7IfThenElseValidator()); var schema = new QuasiSchema7 { Schema = QuasiSchema7.MetaQuasi.Id, If = new QuasiSchema7 { Type = JsonSchemaType.Integer }, Then = new QuasiSchema7 { Minimum = 10 }, Else = JsonSchema06.False }; JsonValue json = 15; var results = schema.Validate(json); results.AssertValid(); }
public ActionResult CustomerQueryType() { var schema = JsonSchemaFactory.Generate(typeof(CustomerQuery)); return(Ok(schema)); }
public ActionResult NewAddressType() { var schema = JsonSchemaFactory.Generate(typeof(NewAddress)); return(Ok(schema)); }
protected override async Task When() { schema = await JsonSchemaFactory.GenerateSchemaAsync(typeof(MyParameter)).ConfigureAwait(false); }
public T Deserialize <T>(JsonValue json, JsonSerializer serializer) { var value = JsonSchemaFactory.FromJson(json); return((T)value); }
public void Run07(string fileName, JsonValue testJson, JsonValue schemaJson, string testname) { JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema07>(); _Run <JsonSchema07>(fileName, testJson, schemaJson); }
public ActionResult CreateCustomerParametersType() { var schema = JsonSchemaFactory.Generate(typeof(CreateCustomerParameters)); return(Ok(schema)); }
public async Task <ActionResult> NewAddressType() { var schema = await JsonSchemaFactory.Generate(typeof(NewAddress)).ConfigureAwait(false); return(Ok(schema)); }