public void ObjectTest() { var data = @" { 'a':1, 'b':2, 'c': 3 } "; MPJson obj = MPJson.Object( MPJson.Property("a", 1), MPJson.Property("b", 2), MPJson.Property("c", 3) ); var parsed = MPJson.Parse(data); MPJson empty = MPJson.Parse("{}"); MPJson singleton = MPJson.Parse(" { 'a':1 } "); Assert.Equal(3, obj.Length); Assert.Equal(3, parsed.Length); Assert.Equal(1, parsed["a"]); Assert.Equal(2, parsed["b"]); Assert.Equal(3, parsed["c"]); Assert.Equal(MPJson.Undefined, parsed["d"]); Assert.Equal(obj, parsed); Assert.Equal(1, singleton.Length); Assert.Equal(0, empty.Length); Assert.Equal(JsonType.Object, empty.Type); }
public void SchemaEnumEmptyArrayIsInvalidTest() { var schema = new MPSchema( MPJson.Object( MPJson.Property("enum", MPJson.Array()) ) ); Assert.False(schema.IsValid); Assert.False(schema.Validate(true)); }
public void SchemaEnumWrongTypeIsInvalidTest() { var schema = new MPSchema( MPJson.Object( MPJson.Property("enum", "Hello") ) ); Assert.False(schema.IsValid); Assert.False(schema.Validate("Hello")); }
public void EveythingSchemaTests() { var schema = new MPSchema(true); Assert.True(schema.IsValid); Assert.True(schema.IsValid); Assert.True(schema.Validate(true)); Assert.True(schema.Validate(123)); Assert.True(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Array())); }
public void NothingSchemaTests() { var schema = new MPSchema(false); Assert.True(schema.IsValid); Assert.True(schema.IsValid); Assert.False(schema.Validate(true)); Assert.False(schema.Validate("string")); Assert.False(schema.Validate(123)); Assert.False(schema.Validate(MPJson.Object())); Assert.False(schema.Validate(MPJson.Array())); }
public void RequiredTest() { var schema = SingletonSchema("required", MPJson.Array("a", "c")); Assert.True(schema.IsValid); Assert.False(schema.Validate(MPJson.Object())); Assert.False(schema.Validate(MPJson.Object(a1))); Assert.False(schema.Validate(MPJson.Object(b2, c3, d4))); Assert.True(schema.Validate(MPJson.Object(a1, c3))); Assert.True(schema.Validate(MPJson.Object(a1, b2, c3, d4))); Assert.False(SingletonSchema("required", "true").IsValid); Assert.False(SingletonSchema("required", MPJson.Object()).IsValid); }
public void ExclusiveMinimumTest() { var schema = SimpleSchema("exclusiveMinimum", 3); Assert.True(schema.IsValid); Assert.False(SimpleSchema("minimum", MPJson.Object()).IsValid); Assert.False(schema.Validate(3)); Assert.True(schema.Validate(4)); Assert.True(schema.Validate(4.5)); Assert.True(schema.Validate("123")); Assert.False(schema.Validate(1)); Assert.False(schema.Validate(0)); Assert.False(schema.Validate(-1)); }
public void CheckStringCoercionDisabled() { MPSchema schema = MPJson.Object("type", "number", "maximum", 3); var jsonValidator = new JsonValidator(); Assert.False(jsonValidator.Validate(schema, "1")); Assert.True(jsonValidator.Validate(schema, 1)); Assert.False(jsonValidator.Validate(schema, "3")); Assert.True(jsonValidator.Validate(schema, 3)); Assert.False(jsonValidator.Validate(schema, "4")); Assert.False(jsonValidator.Validate(schema, 4)); Assert.False(jsonValidator.Validate(schema, "fails")); }
public void TypeObjectSchemaTests() { var schema = new MPSchema(MPJson.Object(MPJson.Property("type", MPJson.Array("object")))); Assert.True(schema.IsValid); Assert.False(schema.Validate(123.2)); Assert.False(schema.Validate(54)); Assert.False(schema.Validate(false)); Assert.False(schema.Validate(true)); Assert.False(schema.Validate(MPJson.Null)); Assert.False(schema.Validate("x")); Assert.True(schema.Validate(MPJson.Object())); Assert.False(schema.Validate(MPJson.Array())); }
public void ItemsListValidationTest() { var schema = SingletonSchema("items", MPJson.Object(MPJson.Property("type", "number"))); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Array())); Assert.True(schema.Validate(MPJson.Array(1))); Assert.True(schema.Validate(MPJson.Array(1, 2))); Assert.False(schema.Validate(MPJson.Array(1, "2"))); Assert.False(schema.Validate(MPJson.Array("2"))); Assert.False(SingletonSchema("items", 1).IsValid); Assert.True(SingletonSchema("items", true).IsValid); Assert.True(SingletonSchema("items", false).IsValid); }
public void PropertiesTest() { var m2 = MPJson.Object("multipleOf", 2); var m3 = MPJson.Object("multipleOf", 3); var m5 = MPJson.Object("multipleOf", 5); MPSchema schema = MPJson.Object("properties", MPJson.Object("m2", m2, "m3", m3, "m5", m5)); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Object("m2", 2))); Assert.True(schema.Validate(MPJson.Object("m5", 5))); Assert.True(schema.Validate(MPJson.Object("m2", 2, "m3", 3))); Assert.True(schema.Validate(MPJson.Object("m2", 4, "m3", 9, "m5", 25))); Assert.False(schema.Validate(MPJson.Object("m2", 3))); Assert.False(schema.Validate(MPJson.Object("m2", 2, "m3", 3, "m5", 7))); }
public void PropertyNamesTest() { var schema = SingletonSchema("propertyNames", MPJson.Object( MPJson.Property("pattern", "a|c") )); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Object(a1))); Assert.False(schema.Validate(MPJson.Object(b2, c3, d4))); Assert.True(schema.Validate(MPJson.Object(a1, c3))); Assert.False(schema.Validate(MPJson.Object(a1, b2, c3, d4))); Assert.False(SingletonSchema("propertyNames", "true").IsValid); Assert.False(SingletonSchema("propertyNames", MPJson.Array()).IsValid); }
public void ContainsTest() { var schema = SingletonSchema("contains", MPJson.Object(MPJson.Property("type", "number"))); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Array(1))); Assert.True(schema.Validate(MPJson.Array(3, 4, 4))); Assert.True(schema.Validate(MPJson.Array("a", 2))); Assert.True(schema.Validate(MPJson.Array(1, "b"))); Assert.False(schema.Validate(MPJson.Array())); Assert.False(schema.Validate(MPJson.Array("a"))); Assert.False(schema.Validate(MPJson.Array("a", "b", "c"))); Assert.True(SingletonSchema("contains", true).IsValid); Assert.False(SingletonSchema("contains", "true").IsValid); }
public void CheckStringCoercesToNumber() { MPSchema schema = MPJson.Object("type", "number", "maximum", 3); var jsonValidator = new JsonValidator() { CoerceStringsToValues = true }; Assert.True(jsonValidator.Validate(schema, "1")); Assert.True(jsonValidator.Validate(schema, 1)); Assert.True(jsonValidator.Validate(schema, "3")); Assert.True(jsonValidator.Validate(schema, 3)); Assert.False(jsonValidator.Validate(schema, "4")); Assert.False(jsonValidator.Validate(schema, 4)); Assert.False(jsonValidator.Validate(schema, "fails")); }
public void MinContainsTest() { MPSchema schema = MPJson.Object("contains", MPJson.Object(MPJson.Property("type", "number")), "minContains", 2); Assert.True(schema.IsValid); Assert.False(schema.Validate(MPJson.Array(1))); Assert.True(schema.Validate(MPJson.Array(1, 2))); Assert.False(schema.Validate(MPJson.Array("a", 2))); Assert.True(schema.Validate(MPJson.Array("a", 2, 3))); Assert.False(schema.Validate(MPJson.Array(1, "b"))); Assert.True(schema.Validate(MPJson.Array(1, "a", 2, 3))); Assert.True(schema.Validate(MPJson.Array(3, 4, 4))); Assert.False(schema.Validate(MPJson.Array())); Assert.False(schema.Validate(MPJson.Array("a"))); Assert.False(schema.Validate(MPJson.Array("a", "b", "c"))); }
public void SchemaEnumTest() { var schema = new MPSchema( MPJson.Object( MPJson.Property("enum", MPJson.Array(true, 54, MPJson.Array())) ) ); Assert.True(schema.IsValid); Assert.False(schema.Validate(123.2)); Assert.True(schema.Validate(54)); Assert.False(schema.Validate(false)); Assert.True(schema.Validate(true)); Assert.False(schema.Validate(MPJson.Null)); Assert.False(schema.Validate("x")); Assert.False(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Array())); }
public void TypeCombinedSchemaTests() { var schema = new MPSchema( MPJson.Object( MPJson.Property("type", MPJson.Array("integer", "array", "string")) ) ); Assert.True(schema.IsValid); Assert.False(schema.Validate(123.2)); Assert.True(schema.Validate(54)); Assert.False(schema.Validate(false)); Assert.False(schema.Validate(true)); Assert.False(schema.Validate(MPJson.Null)); Assert.True(schema.Validate("x")); Assert.False(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Array())); }
public void MinItemsTest() { var schema = SingletonSchema("minProperties", 3); Assert.True(schema.IsValid); Assert.False(schema.Validate(MPJson.Object())); Assert.False(schema.Validate(MPJson.Object(a1, b2))); Assert.True(schema.Validate(MPJson.Object(a1, b2, c3))); Assert.True(schema.Validate(MPJson.Object(a1, b2, c3, d4))); // Any non-object should validate Assert.True(schema.Validate(0)); Assert.False(SingletonSchema("minProperties", -1).IsValid); Assert.False(SingletonSchema("minProperties", 1.5).IsValid); Assert.False(SingletonSchema("minProperties", "1").IsValid); Assert.False(SingletonSchema("minProperties", MPJson.Array()).IsValid); }
public void ObjectTest() { MPJson obj = MPJson.Object( MPJson.Property("a", 1), MPJson.Property("b", 2), MPJson.Property("c", 3) ); Assert.Equal(JsonType.Object, obj.Type); Assert.True(obj.HasValue); Assert.False(obj.IsUndefined); Assert.Equal(1.0, obj["a"]); Assert.Equal(2.0, obj["b"]); Assert.Equal(3.0, obj["c"]); Assert.Equal(JsonType.Undefined, obj[0].Type); Assert.Equal(JsonType.Undefined, obj["d"].Type); Assert.Equal("{ \"a\":1, \"b\":2, \"c\":3 }", obj.ToString()); Assert.Equal(3, obj.Keys.Count()); }
public void DependentRequiredTest(string keyword) { var m2 = MPJson.Object("multipleOf", 2); var m3 = MPJson.Object("multipleOf", 3); MPSchema schema = MPJson.Object( "properties", MPJson.Object("m2", m2, "m3", m3), keyword, MPJson.Object("m5", MPJson.Array("m3"))); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Object("m2", 2))); Assert.True(schema.Validate(MPJson.Object("m2", 2, "m3", 3))); Assert.True(schema.Validate(MPJson.Object("m2", 4, "m3", 9, "m5", 25))); Assert.True(schema.Validate(MPJson.Object("m5", 5, "m3", 3))); Assert.True(schema.Validate(MPJson.Object("m2", 2, "m3", 3, "m5", 7))); Assert.True(schema.Validate(MPJson.Object("m7", 7))); Assert.False(schema.Validate(MPJson.Object("m2", 3))); Assert.False(schema.Validate(MPJson.Object("m5", 5, "m2", 2))); }
public void AdditionalItemsFalseValidationTest() { MPSchema schema = MPJson.Object( MPJson.Property("items", MPJson.Array( MPJson.Object(MPJson.Property("type", "number")), MPJson.Object(MPJson.Property("type", "string")), MPJson.Object(MPJson.Property("type", "boolean")) )), MPJson.Property("additionalItems", false) ); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Array(0))); Assert.True(schema.Validate(MPJson.Array(2, "b"))); Assert.True(schema.Validate(MPJson.Array(1, "a", true))); Assert.False(schema.Validate(MPJson.Array(3, "c", false, "x"))); Assert.False(schema.Validate(MPJson.Array(3, "c", false, MPJson.Null))); }
public void ItemsTupleValidationTest() { var schema = SingletonSchema("items", MPJson.Array( MPJson.Object(MPJson.Property("type", "number")), MPJson.Object(MPJson.Property("type", "string")), MPJson.Object(MPJson.Property("type", "boolean")) )); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Array(0))); Assert.True(schema.Validate(MPJson.Array(2, "b"))); Assert.True(schema.Validate(MPJson.Array(1, "a", true))); Assert.True(schema.Validate(MPJson.Array(3, "c", false, "x"))); Assert.True(schema.Validate(MPJson.Array(3, "c", false, MPJson.Null))); Assert.False(schema.Validate(MPJson.Array("a"))); Assert.False(schema.Validate(MPJson.Array(2, 3))); Assert.False(schema.Validate(MPJson.Array(1, "a", 2))); Assert.False(schema.Validate(MPJson.Array("a", "c", false, "x"))); }
public void PatternPropertiesTest() { var m2 = MPJson.Object("multipleOf", 2); var m3 = MPJson.Object("multipleOf", 3); var m5 = MPJson.Object("multipleOf", 5); MPSchema schema = MPJson.Object( "patternProperties", MPJson.Object("2", m2, "3", m3), "additionalProperties", m5); Assert.True(schema.IsValid); Assert.True(schema.Validate(MPJson.Object())); Assert.True(schema.Validate(MPJson.Object("m2", 2))); Assert.True(schema.Validate(MPJson.Object("m5", 5))); Assert.True(schema.Validate(MPJson.Object("m2", 2, "m3", 3))); Assert.True(schema.Validate(MPJson.Object("m2", 4, "m3", 9, "m5", 25))); Assert.True(schema.Validate(MPJson.Object("m23", 6))); Assert.True(schema.Validate(MPJson.Object("m23", 6, "m5", 5))); Assert.False(schema.Validate(MPJson.Object("m23", 5, "m5", 5))); Assert.False(schema.Validate(MPJson.Object("m2", 3))); Assert.False(schema.Validate(MPJson.Object("m2", 2, "m3", 3, "m5", 7))); Assert.False(schema.Validate(MPJson.Object("m7", 7))); }
public void IfThenElseTest() { MPSchema schema = new MPSchema( MPJson.Object( MPJson.Property("if", m2), MPJson.Property("then", m3), MPJson.Property("else", m5) )); Assert.True(schema.IsValid); // if T then T else T Assert.True(schema.Validate(0)); Assert.True(schema.Validate(30)); Assert.True(schema.Validate(60)); Assert.True(schema.Validate("Not a number")); // if T then T else F Assert.True(schema.Validate(6)); // if T then F else T Assert.False(schema.Validate(10)); // if T then F else F Assert.False(schema.Validate(2)); // if F then T else T Assert.True(schema.Validate(15)); // if F then T else F Assert.False(schema.Validate(21)); // if F then T else F Assert.False(schema.Validate(3)); // if F then F else F Assert.False(schema.Validate(1)); }
public void JsonMatchesObjectsTest() { MPJson j1 = MPJson.Object(); MPJson j3 = MPJson.Object( MPJson.Property("a", 1), MPJson.Property("b", 2), MPJson.Property("c", 3) ); MPJson j3b = MPJson.Object( MPJson.Property("c", 3), MPJson.Property("b", 2), MPJson.Property("a", 1) ); MPJson j3c = MPJson.Object( MPJson.Property("c", 1), MPJson.Property("b", 2), MPJson.Property("a", 3) ); Assert.NotEqual(j1, j3); Assert.NotEqual(j3, j3c); Assert.Equal(j3, j3b); }
MPSchema SimpleSchema(string keyword, MPJson json) { return(new MPSchema(MPJson.Object(MPJson.Property(keyword, json)))); }
MPSchema SingletonSchema(string keyword, MPJson json) { return(MPJson.Object(MPJson.Property(keyword, json))); }