-Added PatternProperties, ExclusiveMinimum and ExclusiveMaximum support to JSON schema
This commit is contained in:
@@ -976,7 +976,7 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void sdfsdf()
|
||||
public void NoAdditionalProperties()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""type"":""array"",
|
||||
@@ -1013,6 +1013,69 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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!""
|
||||
}";
|
||||
|
||||
Json.Schema.ValidationEventArgs validationEventArgs = null;
|
||||
|
||||
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
|
||||
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
|
||||
reader.Schema = JsonSchema.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. Line 4, position 10.", validationEventArgs.Message);
|
||||
|
||||
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. Line 5, position 8.", 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());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExtendedComplex()
|
||||
{
|
||||
|
||||
@@ -217,5 +217,64 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Property 'g' has not been defined and the schema does not allow additional properties. Line 1, position 5.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 10 equals maximum value of 10 and exclusive maximum is true.")]
|
||||
public void ExclusiveMaximum_Int()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Float 10.1 equals maximum value of 10.1 and exclusive maximum is true.")]
|
||||
public void ExclusiveMaximum_Float()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10.1;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 10 equals minimum value of 10 and exclusive minimum is true.")]
|
||||
public void ExclusiveMinimum_Int()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Float 10.1 equals minimum value of 10.1 and exclusive minimum is true.")]
|
||||
public void ExclusiveMinimum_Float()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10.1;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 10 is not evenly divisible by 3.")]
|
||||
public void DivisibleBy_Int()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.DivisibleBy = 3;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,21 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
Assert.AreEqual(true, schema.Required);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
string json = @"{
|
||||
""exclusiveMinimum"":true,
|
||||
""exclusiveMaximum"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(true, schema.ExclusiveMinimum);
|
||||
Assert.AreEqual(true, schema.ExclusiveMaximum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadOnly()
|
||||
{
|
||||
@@ -433,5 +448,22 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PatternProperties()
|
||||
{
|
||||
string json = @"{
|
||||
""patternProperties"": {
|
||||
""[abc]"": { ""id"":""Blah"" }
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.IsNotNull(schema.PatternProperties);
|
||||
Assert.AreEqual(1, schema.PatternProperties.Count);
|
||||
Assert.AreEqual("Blah", schema.PatternProperties["[abc]"].Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,5 +377,50 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
Assert.AreEqual(schema.Properties["x"].Options[1], "Asc");
|
||||
Assert.AreEqual(schema.Properties["x"].Options[-1], "Desc");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.ExclusiveMinimum = true;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""exclusiveMinimum"": true,
|
||||
""exclusiveMaximum"": true
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_PatternProperties()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PatternProperties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{ "[abc]", new JsonSchema() }
|
||||
};
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""patternProperties"": {
|
||||
""[abc]"": {}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user