-Added PatternProperties, ExclusiveMinimum and ExclusiveMaximum support to JSON schema

This commit is contained in:
JamesNK
2011-01-02 09:19:53 +00:00
parent 3ad33f7329
commit f00d796e03
12 changed files with 463 additions and 85 deletions
@@ -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);
}
}
}