public void Example() { #region Usage JSchema schema = JSchema.Parse(@"{ 'type': 'object', 'properties': { 'name': {'type':'string'}, 'hobbies': { 'type': 'array', 'items': {'type':'string'} } } }"); JObject person = JObject.Parse(@"{ 'name': 'James', 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] }"); IList <string> errorMessages; bool valid = person.IsValid(schema, out errorMessages); Console.WriteLine(valid); // true #endregion Assert.IsTrue(valid); }
public void Example() { #region Usage JSchemaGenerator generator = new JSchemaGenerator(); JSchema schema = generator.Generate(typeof(Computer)); //{ // "type": "object", // "properties": { // "DiskIds": { // "type": "array", // "items": { // "type": "string", // }, // "uniqueItems": true // }, // "ScreenIds": { // "type": "array", // "items": { // "type": "string", // }, // "uniqueItems": true // } // } //} #endregion Assert.IsTrue(schema.Properties["DiskIds"].UniqueItems); #if !NET35 Assert.IsTrue(schema.Properties["ScreenIds"].UniqueItems); #endif }
public void Test() { JSchema schema = JSchema.Parse(Schema); JObject o = JObject.Parse(Data); Assert.IsTrue(o.IsValid(schema)); }
public void Example() { #region Usage // resolver will fetch 'http://schema.org/address.json' with a web request as the parent schema is loaded JSchemaUrlResolver resolver = new JSchemaUrlResolver(); JSchema schema = JSchema.Parse(@"{ 'type': 'object', 'properties': { 'name': {'type':'string'}, 'addresses': { 'type': 'array', 'items': {'$ref': 'http://schema.org/address.json'} } } }", resolver); JToken json = JToken.Parse(@"{ 'name': 'James', 'addresses': [ { 'line1': '99 Smithington Street', 'line2': 'Real Town', 'country': 'USA' } ] }"); IList <string> errorMessages; bool isValid = json.IsValid(schema, out errorMessages); #endregion Assert.IsTrue(isValid); }
public void Test() { var schema = JSchema.Parse(@"{ ""description"" : ""Example Contact Information Array JSON Schema"", ""type"" : ""array"", ""items"" : { ""title"" : ""A Contact Information object"", ""type"" : ""object"", ""properties"" : { ""name"" : { ""type"" : ""string"", ""enum"" : [""home"", ""work"", ""other""] }, ""email"" : { ""type"" : ""string"", ""optional"" : true, ""format"" : ""email"" } }, ""minItems"" : 1, ""maxItems"" : 5 } }"); JArray a = JArray.Parse(@"[ { ""name"": ""work"", ""email"": ""{[email protected]"" } ]"); IList <string> errorMessages; Assert.IsTrue(a.IsValid(schema, out errorMessages)); }
public void Format_Time_TimeZoneOffset() { JSchema s = JSchema.Parse(@"{""format"": ""time""}"); JToken t = JToken.Parse("'18:55:00-04:00'"); Assert.IsTrue(t.IsValid(s)); }
public void Test() { JSchema s = JSchema.Parse(@"{ ""id"": ""http://site.somewhere/entry-schema#"", ""$schema"": ""http://json-schema.org/draft-06/schema#"", ""description"": ""Json Schema for DSP Data Catalog Data Set"", ""type"": ""object"", ""additionalProperties"": false, ""required"": [ ""IsProcessed"" ], ""properties"": { ""IsProcessed"": { ""type"": ""boolean"" } } }"); string json = @"{ ""IsProcessed"" : 5 }"; Result r = Deserialize <Result>(new JsonTextReader(new StringReader(json)), s, out List <string> errors); Assert.IsTrue(r.IsProcessed); Assert.AreEqual(1, errors.Count); Assert.AreEqual("Invalid type. Expected Boolean but got Integer. Path 'IsProcessed', line 2, position 22.", errors[0]); }
public void Format_Time_Milliseconds() { JSchema s = JSchema.Parse(@"{""format"": ""time""}"); JToken t = JToken.Parse("'11:50:00.123'"); Assert.IsTrue(t.IsValid(s)); }
public void Format_Time_UTC() { JSchema s = JSchema.Parse(@"{""format"": ""time""}"); JToken t = JToken.Parse("'18:55:00Z'"); Assert.IsTrue(t.IsValid(s)); }
public void Format_DateTime_Lowercase() { JSchema s = JSchema.Parse(@"{""format"": ""date-time""}"); JToken t = JToken.Parse("'1963-06-19t08:30:06.283185z'"); Assert.IsTrue(t.IsValid(s)); }
public void TestValidInternationalAddresses() { for (int i = 0; i < ValidInternationalAddresses.Length; i++) { Assert.IsTrue(EmailHelpers.Validate(ValidInternationalAddresses[i], true), "Valid International Address #{0}", i); } }
public void UnevaluatedProperties_NotAllowed_NoMatch() { string schemaJson = @"{ ""type"": ""object"", ""properties"": { ""foo"": { ""type"": ""string"" } }, ""unevaluatedProperties"": false }"; string json = "{'bar':true}"; SchemaValidationEventArgs validationEventArgs = null; JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json))); reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; }; reader.Schema = JSchema.Parse(schemaJson); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.StartObject, reader.TokenType); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Boolean, reader.TokenType); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.IsNotNull(validationEventArgs); Assert.AreEqual("Property 'bar' has not been successfully evaluated and the schema does not allow unevaluated properties. Path '', line 1, position 12.", validationEventArgs.Message); Assert.AreEqual(ErrorType.UnevaluatedProperties, validationEventArgs.ValidationError.ErrorType); }
public void Test_Object_Success() { // Arrange JSchema schema = JSchema.Parse(JsonSchema_Object); JArray a = JArray.Parse(@"[ { ""module_id"": ""001"", ""module_name"": ""Reklamacje"", ""module_body"": [ { ""id"": ""001"", ""class"": ""middle"", ""to"": [ { ""id"": ""002"", ""class"": ""simple"", ""value"": ""DUMMY"" } ] } ] } ]"); // Act bool valid = a.IsValid(schema, out IList <ValidationError> errors); // Assert Assert.IsTrue(valid); Assert.AreEqual(0, errors.Count); }
public void Enum_Properties() { JSchema schema = new JSchema { Properties = { { "bar", new JSchema { Enum = { new JValue(1), new JValue(2) } } } } }; JObject o = new JObject( new JProperty("bar", 1) ); IList <string> errorMessages; Assert.IsTrue(o.IsValid(schema, out errorMessages)); Assert.AreEqual(0, errorMessages.Count); o = new JObject( new JProperty("bar", 3) ); Assert.IsFalse(o.IsValid(schema, out errorMessages)); Assert.AreEqual(1, errorMessages.Count); }
public void Test() { JSchema s = JSchema.Parse(SchemaJson); JToken t = JToken.Parse(Json); Assert.IsTrue(t.IsValid(s)); }
public void Example() { #region Usage string schemaJson = @"{ 'description': 'A person', 'type': 'object', 'properties': { 'name': {'type':'string'}, 'hobbies': { 'type': 'array', 'items': {'type':'string'} } } }"; Person p = new Person { Name = "James", Hobbies = new List <string> { ".NET", "Blogging", "Reading", "Xbox", "LOLCATS" } }; StringWriter stringWriter = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(stringWriter); writer.Formatting = Formatting.Indented; JSchemaValidatingWriter validatingWriter = new JSchemaValidatingWriter(writer); validatingWriter.Schema = JSchema.Parse(schemaJson); IList <string> messages = new List <string>(); validatingWriter.ValidationEventHandler += (o, a) => messages.Add(a.Message); JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(validatingWriter, p); Console.WriteLine(stringWriter); // { // "Name": "James", // "Hobbies": [ // ".NET", // "Blogging", // "Reading", // "Xbox", // "LOLCATS" // ] // } bool isValid = (messages.Count == 0); Console.WriteLine(isValid); // true #endregion Assert.IsTrue(isValid); }
public void SchemaDraft4() { string schemaJson = TestHelpers.OpenFileText("Resources/Schemas/schema-draft-v4.json"); JSchema schema = JSchema.Parse(schemaJson); JObject o = JObject.Parse(schemaJson); Assert.IsTrue(o.IsValid(schema)); }
public void UniqueItems_SimpleUnique() { JSchema schema = new JSchema(); schema.UniqueItems = true; JArray a = new JArray(1, 2, 3); Assert.IsTrue(a.IsValid(schema)); }
public void ValidateData() { byte[] testData = Encoding.UTF8.GetBytes("Hello world"); byte[] signature = Convert.FromBase64String("sAx4uDJZFOKcbhj+a65RdCVUF0E/kvluG7FnwGYWRIWNay3r/A7xw5p46shtT0HJrYKApIel00pY7rCdI/OVBP2VvCNvMiTD9VtY6LzMwOXcNKgBDAiSp/cu/ZvqQO7dCALmlBffj+5uKMtThbAgLN+i74wpI7Zt+2kwvQKbbR4="); bool valid = CryptographyHelpers.ValidateData(testData, signature); Assert.IsTrue(valid); }
public void SupCodePoints() { JSchema schema = JSchema.Parse(@"{""maxLength"": 2}"); JToken json = JToken.Parse(@"""\uD83D\uDCA9\uD83D\uDCA9"""); IList <string> errorMessages; Assert.IsTrue(json.IsValid(schema, out errorMessages)); }
public void Test() { JSchema schema = JSchema.Parse(SchemaJson); JArray a = JArray.Parse(Json); bool result = a.IsValid(schema); Assert.IsTrue(result); }
public void Example() { #region Usage JSchema schema = new JSchema { Type = JSchemaType.Object, Properties = { { "name", new JSchema { Type = JSchemaType.String } }, { "hobbies", new JSchema { Type = JSchemaType.Array, Items ={ new JSchema { Type = JSchemaType.String } } } }, } }; string schemaJson = schema.ToString(); Console.WriteLine(schemaJson); // { // "type": "object", // "properties": { // "name": { // "type": "string" // }, // "hobbies": { // "type": "array", // "items": { // "type": "string" // } // } // } // } JObject person = JObject.Parse(@"{ 'name': 'James', 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] }"); bool valid = person.IsValid(schema); Console.WriteLine(valid); // true #endregion Assert.IsTrue(valid); }
public void CloseAlsoClosesUnderlyingWriter() { var underlyingWriter = new JsonWriterStubWithIsClosed(); var validatingWriter = new JSchemaValidatingWriter(underlyingWriter) { CloseOutput = true }; validatingWriter.Close(); Assert.IsTrue(underlyingWriter.IsClosed); }
public void JsonLD() { string jsonLd = @"{ ""@id"": ""http://store.example.com/"", ""@type"": ""Store"", ""name"": ""Links Bike Shop"", ""description"": ""The most \""linked\"" bike store on earth!"", ""product"": [ { ""@id"": ""p:links-swift-chain"", ""@type"": ""Product"", ""name"": ""Links Swift Chain"", ""description"": ""A fine chain with many links."", ""category"": [""cat:parts"", ""cat:chains""], ""price"": ""10.00"", ""stock"": 10 }, { ""@id"": ""p:links-speedy-lube"", ""@type"": ""Product"", ""name"": ""Links Speedy Lube"", ""description"": ""Lubricant for your chain links."", ""category"": [""cat:lube"", ""cat:chains""], ""price"": ""5.00"", ""stock"": 20 } ], ""@context"": { ""Store"": ""http://ns.example.com/store#Store"", ""Product"": ""http://ns.example.com/store#Product"", ""product"": ""http://ns.example.com/store#product"", ""category"": { ""@id"": ""http://ns.example.com/store#category"", ""@type"": ""@id"" }, ""price"": ""http://ns.example.com/store#price"", ""stock"": ""http://ns.example.com/store#stock"", ""name"": ""http://purl.org/dc/terms/title"", ""description"": ""http://purl.org/dc/terms/description"", ""p"": ""http://store.example.com/products/"", ""cat"": ""http://store.example.com/category/"" } }"; JSchema schema = TestHelpers.OpenSchemaFile("Resources/Schemas/jsonld.json"); JObject o = JObject.Parse(jsonLd); Assert.IsTrue(o.IsValid(schema)); }
public void BigNum() { ValidationError error = null; JSchema s = JSchema.Parse(@"{""maximum"": 18446744073709551615}"); JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader("18446744073709551600"))); reader.Schema = s; reader.ValidationEventHandler += (sender, args) => { error = args.ValidationError; }; Assert.IsTrue(reader.Read()); Assert.IsNull(error); }
public void Test_DraftUnset_Match() { var s = JSchema.Parse(originalSchemaJson); var writtenJson = s.ToString(); StringAssert.AreEqual(@"{ ""definitions"": {}, ""type"": ""object"", ""$ref"": ""#"" }", writtenJson); JToken t = JToken.Parse("{'test':'value'}"); Assert.IsTrue(t.IsValid(s)); }
public void Test() { string json = @"{ ""location"" : { ""country_code"" : ""GB"" } }"; JSchema schema = JSchema.Parse(Schema); JObject o = JObject.Parse(json); IList <string> messages; bool valid = o.IsValid(schema, out messages); Assert.IsTrue(valid); }
public void Example() { #region Usage string personSchemaJson = @"{ 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' } } }"; JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver(); resolver.Add(new Uri("person.json", UriKind.RelativeOrAbsolute), personSchemaJson); // the external 'person.json' schema will be found using the resolver // the internal 'salary' schema will be found using the default resolution logic JSchema employeeSchema = JSchema.Parse(@"{ 'type': 'object', 'allOf': [ { '$ref': 'person.json' } ], 'properties': { 'salary': { '$ref': '#/definitions/salary' }, 'jobTitle': { 'type': 'string' } }, 'definitions': { 'salary': { 'type': 'number' } } }", resolver); string json = @"{ 'name': 'James', 'age': 29, 'salary': 9000.01, 'jobTitle': 'Junior Vice President' }"; JObject employee = JObject.Parse(json); bool valid = employee.IsValid(employeeSchema); Console.WriteLine(valid); // true #endregion Assert.IsTrue(valid); }
public void Example() { #region Usage string schemaJson = @"{ 'description': 'A person', 'type': 'object', 'properties': { 'name': {'type':'string'}, 'hobbies': { 'type': 'array', 'items': {'type':'string'} } } }"; string json = @"{ 'name': 'James', 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] }"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader); validatingReader.Schema = JSchema.Parse(schemaJson); IList <string> messages = new List <string>(); validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message); JsonSerializer serializer = new JsonSerializer(); Person p = serializer.Deserialize <Person>(validatingReader); Console.WriteLine(p.Name); // James bool isValid = (messages.Count == 0); Console.WriteLine(isValid); // true #endregion Assert.IsTrue(isValid); }
public void Test_Contains_Valid() { JSchema schema = JSchema.Parse(SchemaJson); JObject o = JObject.Parse(@"{ ""contexts"": [ { ""name"": ""A_name"" }, { ""name"": ""B_name"" } ] }"); bool isValid = o.IsValid(schema, out IList <ValidationError> messages); Assert.IsTrue(isValid); Assert.AreEqual(0, messages.Count); }