예제 #1
0
        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()));
        }
예제 #2
0
        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()));
        }
예제 #3
0
        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")));
        }
예제 #4
0
        public void SchemaEnumWrongTypeIsInvalidTest()
        {
            var schema = new MPSchema(
                MPJson.Object(
                    MPJson.Property("enum", "Hello")
                    )
                );

            Assert.False(schema.IsValid);
            Assert.False(schema.Validate("Hello"));
        }
예제 #5
0
        public void SchemaEnumEmptyArrayIsInvalidTest()
        {
            var schema = new MPSchema(
                MPJson.Object(
                    MPJson.Property("enum", MPJson.Array())
                    )
                );

            Assert.False(schema.IsValid);
            Assert.False(schema.Validate(true));
        }
예제 #6
0
        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)));
        }
예제 #7
0
        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()));
        }
예제 #8
0
        public void SchemaConstArrayTest()
        {
            var schema = new MPSchema(
                MPJson.Object(
                    MPJson.Property("const", MPJson.Array(true, 54))
                    )
                );

            Assert.True(schema.IsValid);
            Assert.True(schema.Validate(MPJson.Array(true, 54)));
            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.False(schema.Validate(MPJson.Object()));
            Assert.False(schema.Validate(MPJson.Array()));
        }
예제 #9
0
        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)));
        }
예제 #10
0
        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()));
        }
예제 #11
0
        public void TestSchemaFilesWithRef(string root, string schemaFile, string jsonFile, bool valid)
        {
            string schemaPath = Path.Combine(AppContext.BaseDirectory, root, schemaFile);
            string jsonPath   = Path.Combine(AppContext.BaseDirectory, root, jsonFile);

            MPJson json = MPJson.Parse(File.ReadAllText(jsonPath));

            Assert.True(json.HasValue);

            MPSchema schema = MPJson.Parse(File.ReadAllText(schemaPath));

            Assert.True(schema.IsValid);

            var validated = schema.Validate(json);

            Assert.Equal(valid, validated);
        }
예제 #12
0
        public void AdditionalPropertiesTest()
        {
            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),
                "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.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)));
        }
예제 #13
0
        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));
        }