private void LogicKeywordsRequiredNonEmptyArrayTest() { Assert.False(SimpleSchema("allOf", true).IsValid); Assert.False(SimpleSchema("anyOf", true).IsValid); Assert.False(SimpleSchema("oneOf", true).IsValid); Assert.False(SimpleSchema("allOf", MPJson.Array()).IsValid); Assert.False(SimpleSchema("anyOf", MPJson.Array()).IsValid); Assert.False(SimpleSchema("oneOf", MPJson.Array()).IsValid); }
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 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 StringFormatTest() { var schema = SimpleSchema("format", "unknown"); Assert.True(schema.IsValid); Assert.True(schema.Validate("xyz")); Assert.True(schema.Validate(123)); Assert.False(SimpleSchema("format", 1).IsValid); Assert.False(SimpleSchema("format", MPJson.Array()).IsValid); }
public void StringPatternTest() { var schema = SimpleSchema("pattern", @"\d+"); Assert.True(schema.IsValid); Assert.True(schema.Validate(0)); Assert.True(schema.Validate("1")); Assert.True(schema.Validate("12")); Assert.False(schema.Validate("")); Assert.False(SimpleSchema("pattern", 1.5).IsValid); Assert.False(SimpleSchema("pattern", MPJson.Array()).IsValid); }
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 MaximumTest() { var schema = SimpleSchema("maximum", 3); Assert.True(schema.IsValid); Assert.False(SimpleSchema("minimum", MPJson.Array()).IsValid); Assert.True(schema.Validate(3)); Assert.False(schema.Validate(4)); Assert.False(schema.Validate(4.5)); Assert.True(schema.Validate("123")); Assert.True(schema.Validate(1)); Assert.True(schema.Validate(0)); Assert.True(schema.Validate(-1)); }
public void AnyOfTest() { var schema = SimpleSchema("anyOf", MPJson.Array(m2, m3, m5)); Assert.True(schema.IsValid); Assert.True(schema.Validate(0)); Assert.False(schema.Validate(1)); Assert.True(schema.Validate(2)); Assert.True(schema.Validate(15)); Assert.True(schema.Validate(21)); Assert.True(schema.Validate(30)); Assert.True(schema.Validate(60)); Assert.True(schema.Validate("Not a number")); }
public void StringMinLengthTest() { var schema = SimpleSchema("minLength", 3); Assert.True(schema.IsValid); Assert.False(schema.Validate("")); Assert.False(schema.Validate("ab")); Assert.True(schema.Validate("abc")); Assert.True(schema.Validate("abcd")); Assert.True(schema.Validate(0)); Assert.False(SimpleSchema("minLength", 1.5).IsValid); Assert.False(SimpleSchema("minLength", "1").IsValid); Assert.False(SimpleSchema("minLength", MPJson.Array()).IsValid); }
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 MinItemsTest() { var schema = SingletonSchema("minItems", 3); Assert.True(schema.IsValid); Assert.False(schema.Validate(MPJson.Array())); Assert.False(schema.Validate(MPJson.Array(1, 2))); Assert.True(schema.Validate(MPJson.Array(1, 2, 3))); Assert.True(schema.Validate(MPJson.Array(1, 2, 3, 4))); Assert.True(schema.Validate(0)); Assert.False(SingletonSchema("minItems", 1.5).IsValid); Assert.False(SingletonSchema("minItems", "1").IsValid); Assert.False(SingletonSchema("minItems", MPJson.Array()).IsValid); }
public void UniqueItemsTest() { var unique = SingletonSchema("uniqueItems", true); Assert.True(unique.IsValid); Assert.True(unique.Validate(MPJson.Array())); Assert.True(unique.Validate(MPJson.Array(1))); Assert.True(unique.Validate(MPJson.Array(1, 2))); Assert.True(unique.Validate(MPJson.Array(3, 4, 5))); Assert.False(unique.Validate(MPJson.Array(3, 4, 4))); Assert.False(unique.Validate(MPJson.Array(5, 5))); Assert.False(SingletonSchema("uniqueItems", "true").IsValid); Assert.False(SingletonSchema("uniqueItems", MPJson.Array()).IsValid); }
public void ArrayTest() { var data = @" [ 1, 2, 3 ] "; MPJson array = MPJson.Array(1, 2, 3); var parsed = MPJson.Parse(data); Assert.Equal(3, array.Length); Assert.Equal(3, parsed.Length); Assert.Equal(1, parsed[0]); Assert.Equal(2, parsed[1]); Assert.Equal(3, parsed[2]); Assert.Equal(MPJson.Undefined, parsed[3]); Assert.Equal(array, parsed); }
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 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 OneOfTest() { var schema = SimpleSchema("oneOf", MPJson.Array(m2, m3, m5)); Assert.True(schema.IsValid); Assert.False(schema.Validate(0)); Assert.False(schema.Validate(1)); Assert.True(schema.Validate(2)); Assert.False(schema.Validate(15)); Assert.True(schema.Validate(21)); Assert.False(schema.Validate(30)); Assert.False(schema.Validate(60)); // Wow! This is surprising but true!! Assert.False(schema.Validate("Not a number")); }
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 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 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 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 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 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 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 ArrayTest() { object[] array = new object[] { 1.0, 2.0, 3.0 }; MPJson json1 = MPJson.From(array); MPJson json2 = MPJson.From(array.Clone()); MPJson ja = MPJson.Array(1.0, 2.0, 3.0); Assert.Equal(JsonType.Array, json1.Type); Assert.Equal(JsonType.Array, json2.Type); Assert.Equal(JsonType.Array, ja.Type); Assert.True(json1.HasValue); Assert.False(json1.IsUndefined); Assert.Equal(json1, json2); Assert.Equal(json1, ja); Assert.Equal(1.0, ja[0]); Assert.Equal(2.0, ja[1]); Assert.Equal(3.0, ja[2]); Assert.Equal(JsonType.Undefined, json1["x"].Type); Assert.Equal("[ 1, 2, 3 ]", ja.ToString()); Assert.Equal(3, ja.Count()); Assert.Empty(ja.Keys); }