public void OneOf_NoneValid() { JSchema schema = JSchema.Parse(@"{ ""type"": ""string"", ""oneOf"" : [ { ""type"": ""object"" }, { ""maxLength"": 4 } ] }"); JToken json = JToken.Parse(@"""foo foo"""); IList <ValidationError> errors; Assert.IsFalse(json.IsValid(schema, out errors)); Assert.AreEqual(1, errors.Count); ValidationError error = errors.Single(); StringAssert.AreEqual("JSON is valid against no schemas from 'oneOf'. Path '', line 1, position 9.", error.GetExtendedMessage()); Assert.AreEqual(2, error.ChildErrors.Count); StringAssert.AreEqual(@"String 'foo foo' exceeds maximum length of 4. Path '', line 1, position 9.", error.ChildErrors[0].GetExtendedMessage()); StringAssert.AreEqual(@"Invalid type. Expected Object but got String. Path '', line 1, position 9.", error.ChildErrors[1].GetExtendedMessage()); }
public void UniqueItems_NestedDuplicate() { JSchema schema = new JSchema { UniqueItems = true, Items = { new JSchema { UniqueItems = true } } }; schema.ItemsPositionValidation = false; JArray a = new JArray( new JArray(1, 2), new JArray(1, 1), new JArray(3, 4), new JArray(1, 2), new JArray(1, 1) ); IList <string> errorMessages; Assert.IsFalse(a.IsValid(schema, out errorMessages)); Assert.AreEqual(4, errorMessages.Count); Assert.AreEqual("Non-unique array item at index 1. Path '[1][1]'.", errorMessages[0]); Assert.AreEqual("Non-unique array item at index 3. Path '[3]'.", errorMessages[1]); Assert.AreEqual("Non-unique array item at index 1. Path '[4][1]'.", errorMessages[2]); Assert.AreEqual("Non-unique array item at index 4. Path '[4]'.", errorMessages[3]); }
public void Format_Hostname_InvalidCharacters() { JSchema s = JSchema.Parse(@"{""format"": ""hostname""}"); JToken t = JToken.Parse(@"""not_a_valid_host_name"""); Assert.IsFalse(t.IsValid(s)); }
public void IfThenElse_ComplexThenChildErrors() { JSchema s = JSchema.Parse(@"{ ""if"": { ""properties"": { ""value"": { ""type"": ""integer"" } } }, ""then"": { ""properties"": { ""value"": { ""maximum"": -10 } } }, ""else"": { ""type"": ""null"" } }"); JToken t = JToken.Parse(@"{""value"":1}"); Assert.IsFalse(t.IsValid(s, out IList <ValidationError> validationErrors)); Assert.AreEqual(1, validationErrors.Count); Assert.AreEqual("JSON does not match schema from 'then'.", validationErrors[0].Message); Assert.AreEqual(ErrorType.Then, validationErrors[0].ErrorType); Assert.AreEqual(1, validationErrors[0].ChildErrors.Count); Assert.AreEqual("Integer 1 exceeds maximum value of -10.", validationErrors[0].ChildErrors[0].Message); }
public void Test() { JObject clientJson = JObject.Parse(Data); JSchema schema = JSchema.Parse(Schema); var subSchema = schema.Properties["testEnum"].Items[0].Properties["testEnum1"]; Assert.AreEqual("test", subSchema.Description); var subSchemaRef = subSchema.Ref; Assert.AreEqual(JSchemaType.Object, subSchemaRef.Type); var enums = subSchemaRef.Properties["value"].Enum; Assert.AreEqual("1", (string)enums[0]); Assert.AreEqual("2", (string)enums[1]); Assert.AreEqual("3", (string)enums[2]); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual("JSON does not match schema from '$ref'.", errorMessages[0].Message); Assert.AreEqual(@"Value ""21"" is not defined in enum.", errorMessages[0].ChildErrors[0].Message); }
public void Format_IPv4_Hex() { JSchema s = JSchema.Parse(@"{""format"": ""ipv4""}"); JToken t = JToken.Parse("'0x7f000001'"); Assert.IsFalse(t.IsValid(s)); }
public void TestInvalidAddresses() { for (int i = 0; i < InvalidAddresses.Length; i++) { Assert.IsFalse(EmailHelpers.Validate(InvalidAddresses[i]), "Invalid Address #{0}", i); } }
public void Test() { JSchema schema = JSchema.Parse(schemaJson); JObject o = JObject.Parse(@"{ ""each"": { ""pricing"": { ""0"": { ""priceType"": ""1"", ""pricePer"": ""2"", ""price"": 2000000000000000000000000000, ""currency"": ""INR"" }, ""1"": { ""priceType"": ""1"", ""pricePer"": ""2"", ""price"": 200, ""currency"": ""INR"" }, ""2"": { ""priceType"": ""1"", ""pricePer"": ""2"", ""price"": 200, ""currency"": ""INR"" } } } }"); IList <string> errors; Assert.IsFalse(o.IsValid(schema, out errors)); Assert.AreEqual("Integer 2000000000000000000000000000 is not a multiple of 0.01. Path 'each.pricing.0.price', line 7, position 45.", errors[0]); }
public void Test_CastError() { JObject clientJson = JObject.Parse(@"{""Lorem_862"":{}}"); JSchema schema = JSchema.Parse(@"{ ""propertyNames"": { ""$recursiveRef"": ""#"" }, ""then"": { ""$recursiveRef"": ""#"" }, ""readOnly"": false, ""unevaluatedProperties"": { ""$recursiveRef"": ""#"" }, ""dependencies"": { ""Lorem_862"": { ""$recursiveRef"": ""#"" } }, ""description"": ""in adipisicing nisi incididunt ut"", ""$id"": ""<mHl~v0f*#"" }"); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[0].Message); Assert.AreEqual(ErrorType.Dependencies, errorMessages[0].ErrorType); }
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() { string json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""properties"": { ""data"": { ""type"": ""array"", ""items"": { ""oneOf"": [ { ""$ref"": ""#/definitions/sub1"" }, { ""$ref"": ""#/definitions/sub2"" } ] } } }, ""required"": [ ""data"" ], ""definitions"": { ""base"": { ""type"": ""object"" }, ""sub1"": { ""allOf"": [ { ""$ref"": ""#/definitions/base"" } ] }, ""sub2"": { ""allOf"": [ { ""$ref"": ""#/definitions/base"" } ] } } }"; JSchema s = JSchema.Parse(json); JObject o = JObject.Parse(@"{ ""data"": [ null ] }"); IList <ValidationError> errors; Assert.IsFalse(o.IsValid(s, out errors)); Assert.AreEqual(1, errors.Count); Assert.AreEqual("JSON is valid against no schemas from 'oneOf'.", errors[0].Message); }
public void ValidateData_Failure() { byte[] testData = Encoding.UTF8.GetBytes("Hello world TEH HACKZOR!"); byte[] signature = Convert.FromBase64String("sAx4uDJZFOKcbhj+a65RdCVUF0E/kvluG7FnwGYWRIWNay3r/A7xw5p46shtT0HJrYKApIel00pY7rCdI/OVBP2VvCNvMiTD9VtY6LzMwOXcNKgBDAiSp/cu/ZvqQO7dCALmlBffj+5uKMtThbAgLN+i74wpI7Zt+2kwvQKbbR4="); bool valid = CryptographyHelpers.ValidateData(testData, signature); Assert.IsFalse(valid); }
public void Test_WithProperties_Ref() { JObject clientJson = JObject.Parse(Json); JSchema schema = JSchema.Parse(SchemaRefWithProperties); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[0].Message); Assert.AreEqual(ErrorType.Dependencies, errorMessages[0].ErrorType); }
public void Test_AdditionalConditionals() { JToken clientJson = JToken.Parse("1"); JSchema schema = JSchema.Parse(SchemaMultipleConditionals); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual(2, errorMessages.Count); Assert.AreEqual("JSON is valid against no schemas from 'oneOf'.", errorMessages[0].Message); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[1].Message); }
public void Test_TypeMismatch() { JObject clientJson = JObject.Parse("{}"); JSchema schema = JSchema.Parse(Schema); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual(2, errorMessages.Count); Assert.AreEqual("Invalid type. Expected Integer but got Object.", errorMessages[0].Message); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[1].Message); }
public void Test_TypeMatch() { JToken clientJson = JToken.Parse("1"); JSchema schema = JSchema.Parse(Schema); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual(1, errorMessages.Count); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[0].Message); Assert.AreEqual(ErrorType.Not, errorMessages[0].ErrorType); }
public void ComplexEnum() { JSchema schema = JSchema.Parse(@"{""enum"": [6, ""foo"", [], true, {""foo"": 12}]}"); JObject json = JObject.Parse(@"{""foo"": false}"); IList <string> errorMessages; Assert.IsFalse(json.IsValid(schema, out errorMessages)); Assert.AreEqual(1, errorMessages.Count); StringAssert.AreEqual(@"Value {""foo"":false} is not defined in enum. Path '', line 1, position 1.", errorMessages[0]); }
public void Test() { JObject clientJson = JObject.Parse(Json); JSchema schema = JSchema.Parse(Schema); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual(1, errorMessages.Count); Assert.AreEqual("JSON does not match any schemas from 'anyOf'.", errorMessages[0].Message); Assert.AreEqual("Conditional schema has a circular dependency and can't be evaluated.", errorMessages[0].ChildErrors[2].Message); Assert.AreEqual(ErrorType.AnyOf, errorMessages[0].ChildErrors[2].ErrorType); }
public void MissingDependency_Multiple() { JSchema schema = JSchema.Parse(@"{ ""dependencies"": {""quux"": [""foo"", ""bar""]} }"); JToken json = JToken.Parse(@"{""foo"": 1, ""quux"": 2}"); IList <string> errorMessages; Assert.IsFalse(json.IsValid(schema, out errorMessages)); Assert.AreEqual(1, errorMessages.Count); StringAssert.AreEqual(@"Dependencies for property 'quux' failed. Missing required keys: bar. Path '', line 1, position 1.", errorMessages[0]); }
public void UniqueItems_SimpleDuplicate() { JSchema schema = new JSchema(); schema.UniqueItems = true; JArray a = new JArray(1, 2, 3, 2, 2); IList <string> errorMessages; Assert.IsFalse(a.IsValid(schema, out errorMessages)); Assert.AreEqual(2, errorMessages.Count); Assert.AreEqual("Non-unique array item at index 3. Path '[3]'.", errorMessages[0]); Assert.AreEqual("Non-unique array item at index 4. Path '[4]'.", errorMessages[1]); }
public void AdditionalPropertiesSchema() { JSchema schema = JSchema.Parse(@"{ ""properties"": {""foo"": {}, ""bar"": {}}, ""additionalProperties"": {""type"": ""boolean""} }"); JObject json = JObject.Parse(@"{""foo"" : 1, ""bar"" : 2, ""quux"" : 12}"); IList <string> errorMessages; Assert.IsFalse(json.IsValid(schema, out errorMessages)); Assert.AreEqual(1, errorMessages.Count); StringAssert.AreEqual(@"Invalid type. Expected Boolean but got Integer. Path 'quux', line 1, position 34.", errorMessages[0]); }
public void AdditionalItemsSchema() { JSchema schema = JSchema.Parse(@"{ ""items"": [], ""additionalItems"": {""type"": ""integer""} }"); JArray json = JArray.Parse(@"[ 1, 2, 3, ""foo"" ]"); IList <string> errorMessages; Assert.IsFalse(json.IsValid(schema, out errorMessages)); Assert.AreEqual(1, errorMessages.Count); StringAssert.AreEqual(@"Invalid type. Expected Integer but got String. Path '[3]', line 1, position 16.", errorMessages[0]); }
public void UniqueItems_ComplexDuplicate() { JSchema schema = new JSchema(); schema.UniqueItems = true; JArray a = new JArray(1, new JObject(new JProperty("value", "value!")), 3, 2, new JObject(new JProperty("value", "value!")), 4, 2, new JObject(new JProperty("value", "value!"))); IList <string> errorMessages; Assert.IsFalse(a.IsValid(schema, out errorMessages)); Assert.AreEqual(3, errorMessages.Count); Assert.AreEqual("Non-unique array item at index 4. Path '[4]'.", errorMessages[0]); Assert.AreEqual("Non-unique array item at index 6. Path '[6]'.", errorMessages[1]); Assert.AreEqual("Non-unique array item at index 7. Path '[7]'.", errorMessages[2]); }
public void ValidateSimpleType() { string json = @"{ ""values"": [ ""1"", ""[1]"", ""\""A string!\"""", 123, ""{\""prop1\"":bad!}"" ] }"; JSchemaReaderSettings settings = new JSchemaReaderSettings { Validators = new List <JsonValidator> { new JsonFormatValidator() } }; JSchema schema = JSchema.Parse(@"{ ""type"": ""object"", ""properties"": { ""values"": { ""type"": ""array"", ""items"": { ""type"": ""string"", ""format"": ""json"" } } } }", settings); JObject o = JObject.Parse(json); IList <ValidationError> errors; Assert.IsFalse(o.IsValid(schema, out errors)); Assert.AreEqual(2, errors.Count); Assert.AreEqual(@"Invalid type. Expected String but got Integer.", errors[0].Message); Assert.AreEqual(ErrorType.Type, errors[0].ErrorType); Assert.AreEqual("#/properties/values/items/0", errors[0].SchemaId.OriginalString); Assert.AreEqual(@"String is not JSON: Unexpected character encountered while parsing value: b. Path 'prop1', line 1, position 9.", errors[1].Message); Assert.AreEqual(ErrorType.Validator, errors[1].ErrorType); Assert.AreEqual("#/properties/values/items/0", errors[1].SchemaId.OriginalString); }
public void Test_DraftUnset_NoMatch() { var s = JSchema.Parse(originalSchemaJson); var writtenJson = s.ToString(); StringAssert.AreEqual(@"{ ""definitions"": {}, ""type"": ""object"", ""$ref"": ""#"" }", writtenJson); JToken t = JToken.Parse("[]"); Assert.IsFalse(t.IsValid(s)); }
public void Test() { JObject clientJson = JObject.Parse(Json); JSchema schema = JSchema.Parse(Schema); bool valid = clientJson.IsValid(schema, out IList <ValidationError> errorMessages); Assert.IsFalse(valid); Assert.AreEqual(1, errorMessages.Count); Assert.AreEqual("JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0, 1.", errorMessages[0].Message); Assert.AreEqual(2, errorMessages[0].ChildErrors.Count); Assert.AreEqual("Required properties are missing from object: baseUrl, redirectUris.", errorMessages[0].ChildErrors[0].Message); Assert.AreEqual("JSON does not match schema from 'then'.", errorMessages[0].ChildErrors[1].Message); Assert.AreEqual(1, errorMessages[0].ChildErrors[1].ChildErrors.Count); Assert.AreEqual("Required properties are missing from object: baseUrl, redirectUris.", errorMessages[0].ChildErrors[1].ChildErrors[0].Message); }
public void Test() { // Arrange JSchema schema = JSchema.Parse(JsonSchema); JObject o = JObject.Parse(Data); // Act bool valid = o.IsValid(schema, out IList <ValidationError> errors); // Assert Assert.IsFalse(valid); Assert.AreEqual(1, errors.Count); Assert.AreEqual("JSON does not match all schemas from 'allOf'. Invalid schema indexes: 1.", errors[0].Message); Assert.AreEqual(1, errors[0].ChildErrors.Count); Assert.AreEqual("JSON does not match schema from 'then'.", errors[0].ChildErrors[0].Message); Assert.AreEqual("JSON does not match any schemas from 'anyOf'.", errors[0].ChildErrors[0].ChildErrors[0].Message); }
public void Test_NestedConditionalSchemas() { JObject o = JObject.Parse(@"{ ""type"": ""generic2"", ""content"": [ { ""type"": ""generic3"" } ] }"); JSchema s = JSchema.Parse(NestedConditionalSchemas); Assert.IsFalse(o.IsValid(s, out IList <ValidationError> errors)); Assert.AreEqual("JSON does not match any schemas from 'anyOf'.", errors[0].Message); Assert.AreEqual("JSON does not match any schemas from 'anyOf'.", errors[0].ChildErrors[0].Message); Assert.AreEqual(2, errors[0].ChildErrors[0].ChildErrors.Count); }
public void Test() { JSchema s = JSchema.Parse(@"{ ""properties"": { ""EmailCcList"": { ""description"": ""The email addresses "", ""type"": ""string"" } } }"); JObject entityJObject = new JObject(); entityJObject.Add("EmailCcList", new JValue((string)null)); Assert.IsFalse(entityJObject.IsValid(s, out IList <string> errors)); Assert.AreEqual(1, errors.Count); Assert.AreEqual("Invalid type. Expected String but got Null. Path 'EmailCcList'.", errors[0]); }
public void Example() { #region Usage string schemaJson = @"{ 'description': 'Collection of non-primary colors', 'type': 'array', 'items': { 'allOf': [ { '$ref': '#/definitions/hexColor' } ], 'not': { 'enum': ['#FF0000','#00FF00','#0000FF'] } }, 'definitions': { 'hexColor': { 'type': 'string', 'pattern': '^#[A-Fa-f0-9]{6}$' } } }"; JSchema schema = JSchema.Parse(schemaJson); JArray colors = JArray.Parse(@"[ '#DAA520', // goldenrod '#FF69B4', // hot pink '#0000FF', // blue 'Black' ]"); IList <ValidationError> errors; bool valid = colors.IsValid(schema, out errors); // Message - JSON is valid against schema from 'not'. Path '[2]', line 4, position 24. // SchemaId - #/items/0 // Message - JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '[3]', line 5, position 22. // SchemaId - #/items/0 // Message - String 'Black' does not match regex pattern '^#[A-Fa-f0-9]{6}$'. Path '[3]', line 5, position 22. // SchemaId - #/definitions/hexColor #endregion Assert.IsFalse(valid); }