本文整理汇总了C#中Newtonsoft.Json.Schema.JSchemaValidatingReader.Read方法的典型用法代码示例。如果您正苦于以下问题:C# JSchemaValidatingReader.Read方法的具体用法?C# JSchemaValidatingReader.Read怎么用?C# JSchemaValidatingReader.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Schema.JSchemaValidatingReader
的用法示例。
在下文中一共展示了JSchemaValidatingReader.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateStream
public void ValidateStream()
{
#region ValidateStream
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties': {
'name': {'type': 'string'},
'hobbies': {
'type': 'array',
'items': {'type': 'string'}
}
}
}";
JSchema schema = JSchema.Parse(schemaJson);
using (StreamReader s = File.OpenText(@"c:\bigdata.json"))
using (JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(s)))
{
// assign schema and setup event handler
reader.Schema = schema;
reader.ValidationEventHandler += (sender, args) =>
{
Console.WriteLine(args.Message);
};
// bigdata.json will be validated without loading the entire document into memory
while (reader.Read())
{
}
}
#endregion
}
示例2: MissingRequiredProperties
public void MissingRequiredProperties()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string""},
""hobbies"":{""type"":""string"",""required"":true},
""age"":{""type"":""integer"",""required"":true}
}
}";
string json = "{'name':'James'}";
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.AreEqual("name", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.AreEqual("Required properties are missing from object: hobbies, age. Path '', line 1, position 16.", validationEventArgs.Message);
Assert.AreEqual("", validationEventArgs.Path);
Assert.AreEqual("hobbies", ((IList<string>)validationEventArgs.ValidationError.Value)[0]);
Assert.AreEqual("age", ((IList<string>)validationEventArgs.ValidationError.Value)[1]);
Assert.IsNotNull(validationEventArgs);
}
示例3: ReadDateTimes_DateParseHandling
public void ReadDateTimes_DateParseHandling()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""string"",
""minLength"":21
}
}";
string json = @"[
""2000-12-02T05:06:02+00:00"",
""2000-12-02T05:06:02Z"",
1 // hi
]";
SchemaValidationEventArgs a = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json))
{
DateParseHandling = DateParseHandling.DateTimeOffset
});
reader.ValidationEventHandler += (sender, args) => { a = args; };
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsNull(a);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.IsNull(a);
reader.ReadAsString();
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsNotNull(a);
StringAssert.AreEqual(@"String '2000-12-02T05:06:02Z' is less than minimum length of 21. Path '[1]', line 3, position 25.", a.Message);
Assert.AreEqual("2000-12-02T05:06:02Z", a.ValidationError.Value);
a = null;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsNotNull(a);
StringAssert.AreEqual("Invalid type. Expected String but got Integer. Path '[2]', line 4, position 4.", a.Message);
Assert.AreEqual(1, a.ValidationError.Value);
a = null;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Comment, reader.TokenType);
Assert.IsNull(a);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNull(a);
}
示例4: BigIntegerDivisibleBy_Success
public void BigIntegerDivisibleBy_Success()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":2
}
}";
string json = "[999999999999999999999999999999999999999999999999999999998]";
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.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsNull(validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
}
示例5: FloatIsNotInEnum
public void FloatIsNotInEnum()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""enum"":[1.1,2.2]
},
""maxItems"":3
}";
string json = "[1.1,2.2,3.0]";
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.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(@"Value 3.0 is not defined in enum. Path '[2]', line 1, position 12.", validationEventArgs.Message);
Assert.AreEqual(3.0d, validationEventArgs.ValidationError.Value);
Assert.AreEqual("[2]", validationEventArgs.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
}
示例6: IntegerLessThanMinimumValue
public void IntegerLessThanMinimumValue()
{
string schemaJson = @"{
""type"":""integer"",
""minimum"":5
}";
string json = "1";
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.Integer, reader.TokenType);
Assert.AreEqual("Integer 1 is less than minimum value of 5. Path '', line 1, position 1.", validationEventArgs.Message);
Assert.AreEqual(1, validationEventArgs.ValidationError.Value);
Assert.AreEqual("#", validationEventArgs.ValidationError.SchemaId.ToString());
Assert.AreEqual(ErrorType.Minimum, validationEventArgs.ValidationError.ErrorType);
Assert.IsNotNull(validationEventArgs);
}
示例7: IntegerGreaterThanMaximumValue_BigInteger
public void IntegerGreaterThanMaximumValue_BigInteger()
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
string json = "99999999999999999999999999999999999999999999999999999999999999999999";
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.Integer, reader.TokenType);
Assert.AreEqual("Integer 99999999999999999999999999999999999999999999999999999999999999999999 exceeds maximum value of 5. Path '', line 1, position 68.", validationEventArgs.Message);
Assert.AreEqual(BigInteger.Parse("99999999999999999999999999999999999999999999999999999999999999999999"), validationEventArgs.ValidationError.Value);
Assert.AreEqual("", validationEventArgs.Path);
Assert.IsNotNull(validationEventArgs);
}
示例8: StringIsNotInEnum
public void StringIsNotInEnum()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""string"",
""enum"":[""one"",""two""]
},
""maxItems"":3
}";
string json = "['one','two','THREE']";
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.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
Assert.AreEqual(@"Value ""THREE"" is not defined in enum. Path '[2]', line 1, position 20.", validationEventArgs.Message);
Assert.AreEqual("THREE", validationEventArgs.ValidationError.Value);
Assert.AreEqual("[2]", validationEventArgs.Path);
}
示例9: ExtendedComplex
public void ExtendedComplex()
{
string first = @"{
""id"":""first"",
""type"":""object"",
""properties"":
{
""firstproperty"":{""type"":""string""},
""secondproperty"":{""type"":""string"",""maxLength"":10},
""thirdproperty"":{
""type"":""object"",
""properties"":
{
""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
}
}
},
""additionalProperties"":{}
}";
string second = @"{
""id"":""second"",
""type"":""object"",
""extends"":{""$ref"":""first""},
""properties"":
{
""secondproperty"":{""type"":""any""},
""thirdproperty"":{
""extends"":{
""properties"":
{
""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
},
""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
},
""type"":""object"",
""properties"":
{
""thirdproperty_firstproperty"":{""pattern"":""hi""}
},
""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
},
""fourthproperty"":{""type"":""string""}
},
""additionalProperties"":false
}";
JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
JSchema firstSchema = JSchema.Parse(first);
resolver.Add(firstSchema.Id, firstSchema.ToString());
JSchema secondSchema = JSchema.Parse(second, resolver);
string json = @"{
'firstproperty':'blahblahblahblahblahblah',
'secondproperty':'secasecasecasecaseca',
'thirdproperty':{
'thirdproperty_firstproperty':'aaa',
'additional':'three'
}
}";
SchemaValidationEventArgs validationEventArgs = null;
List<ValidationError> errors = new List<ValidationError>();
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs = args;
errors.Add(validationEventArgs.ValidationError);
};
reader.Schema = secondSchema;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("firstproperty", reader.Value.ToString());
StringAssert.AreEqual(@"firstproperty", errors[0].Path);
StringAssert.AreEqual(@"Property 'firstproperty' has not been defined and the schema does not allow additional properties. Path 'firstproperty', line 2, position 19.", errors[0].BuildExtendedMessage());
Assert.AreEqual("firstproperty", errors[0].Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("blahblahblahblahblahblah", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("secondproperty", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("secasecasecasecaseca", reader.Value.ToString());
Assert.AreEqual(1, errors.Count);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("thirdproperty", reader.Value.ToString());
Assert.AreEqual(1, errors.Count);
//.........这里部分代码省略.........
示例10: PatternPropertiesNoAdditionalProperties
public void PatternPropertiesNoAdditionalProperties()
{
string schemaJson = @"{
""type"":""object"",
""patternProperties"": {
""hi"": {""type"":""string""},
""ho"": {""type"":""string""}
},
""additionalProperties"": false
}";
string json = @"{
""hi"": ""A string!"",
""hide"": ""A string!"",
""ho"": 1,
""hey"": ""A string!""
}";
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.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Invalid type. Expected String but got Integer. Path 'ho', line 4, position 10.", validationEventArgs.Message);
Assert.AreEqual(1, validationEventArgs.ValidationError.Value);
Assert.AreEqual("#/patternProperties/ho", validationEventArgs.ValidationError.SchemaId.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("Property 'hey' has not been defined and the schema does not allow additional properties. Path 'hey', line 5, position 9.", validationEventArgs.Message);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
示例11: NoAdditionalItems
public void NoAdditionalItems()
{
string schemaJson = @"{
""type"":""array"",
""items"": [{""type"":""string""},{""type"":""integer""}],
""additionalItems"": false
}";
string json = @"[1, 'a', 1234]";
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.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Invalid type. Expected String but got Integer. Path '[0]', line 1, position 2.", validationEventArgs.Message);
Assert.AreEqual(1, validationEventArgs.ValidationError.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("Invalid type. Expected Integer but got String. Path '[1]', line 1, position 7.", validationEventArgs.Message);
Assert.AreEqual("a", validationEventArgs.ValidationError.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Path '[2]', line 1, position 13.", validationEventArgs.Message);
Assert.AreEqual(1234, validationEventArgs.ValidationError.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read());
}
示例12: ExtendsMissingRequiredProperties
public void ExtendsMissingRequiredProperties()
{
string json = "{}";
List<ValidationError> errors = new List<ValidationError>();
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { errors.Add(args.ValidationError); };
reader.Schema = GetExtendedSchema();
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.AreEqual(2, errors.Count);
StringAssert.AreEqual(@"JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '', line 1, position 2.", errors[0].BuildExtendedMessage());
Assert.AreEqual(null, errors[0].Value);
Assert.AreEqual(1, errors[0].ChildErrors.Count);
StringAssert.AreEqual(@"Required properties are missing from object: firstproperty. Path '', line 1, position 2.", errors[0].ChildErrors[0].BuildExtendedMessage());
Assert.AreEqual(new List<string> { "firstproperty" }, errors[0].ChildErrors[0].Value);
StringAssert.AreEqual("Required properties are missing from object: secondproperty. Path '', line 1, position 2.", errors[1].BuildExtendedMessage());
Assert.AreEqual(new List<string> { "secondproperty" }, errors[1].Value);
Assert.AreEqual("second", errors[1].SchemaId.ToString());
}
示例13: ExtendsStringGreaterThanMaximumLength
public void ExtendsStringGreaterThanMaximumLength()
{
string schemaJson = @"{
""extends"":{
""type"":""string"",
""minLength"":5,
""maxLength"":10
},
""maxLength"":9
}";
List<ValidationError> errors = new List<ValidationError>();
string json = "'The quick brown fox jumps over the lazy dog.'";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs = args;
errors.Add(validationEventArgs.ValidationError);
};
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual(2, errors.Count);
StringAssert.AreEqual(@"JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '', line 1, position 46.", errors[0].BuildExtendedMessage());
Assert.AreEqual(null, errors[0].Value);
Assert.AreEqual(1, errors[0].ChildErrors.Count);
StringAssert.AreEqual(@"String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 10. Path '', line 1, position 46.", errors[0].ChildErrors[0].BuildExtendedMessage());
Assert.AreEqual("The quick brown fox jumps over the lazy dog.", errors[0].ChildErrors[0].Value);
Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 9. Path '', line 1, position 46.", errors[1].BuildExtendedMessage());
Assert.AreEqual("The quick brown fox jumps over the lazy dog.", errors[1].Value);
Assert.IsNotNull(validationEventArgs);
}
示例14: DisableAdditionalProperties
public void DisableAdditionalProperties()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string""}
},
""additionalProperties"":false
}";
string json = "{'name':'James','additionalProperty1':null,'additionalProperty2':null}";
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.AreEqual("name", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("additionalProperty1", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, reader.TokenType);
Assert.AreEqual(null, reader.Value);
Assert.IsNotNull(validationEventArgs);
Assert.AreEqual("Property 'additionalProperty1' has not been defined and the schema does not allow additional properties. Path 'additionalProperty1', line 1, position 38.", validationEventArgs.Message);
Assert.AreEqual("additionalProperty1", validationEventArgs.ValidationError.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("additionalProperty2", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Null, reader.TokenType);
Assert.AreEqual(null, reader.Value);
Assert.AreEqual("Property 'additionalProperty2' has not been defined and the schema does not allow additional properties. Path 'additionalProperty2', line 1, position 65.", validationEventArgs.Message);
Assert.AreEqual("additionalProperty2", validationEventArgs.ValidationError.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例15: MissingNonRequiredProperties
public void MissingNonRequiredProperties()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string"",""required"":true},
""hobbies"":{""type"":""string"",""required"":false},
""age"":{""type"":""integer""}
}
}";
string json = "{'name':'James'}";
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.AreEqual("name", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.IsNull(validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsNull(validationEventArgs);
}