Beispiel #1
0
        public void AnyOfWithBooleanSchemasAllFalse(string desc, string data, bool expected)
        {
            // anyOf with boolean schemas, all false
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ false, false ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void AdditionalPropertiesBeingFalseDoesNotAllowOtherProperties(string desc, string data, bool expected)
        {
            // additionalProperties being false does not allow other properties
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':false, 'patternProperties':{ '^v':{ } }, 'properties':{ 'bar':{ }, 'foo':{ } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void NonASCIIPatternWithAdditionalProperties(string desc, string data, bool expected)
        {
            // non-ASCII pattern with additionalProperties
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':false, 'patternProperties':{ '^á':{ } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #4
0
        public void RemoteRefContainingRefsItself(string desc, string data, bool expected)
        {
            // remote ref, containing refs itself
            Console.Error.WriteLine(desc);
            string   schemaData = "{ '$ref':'http://json-schema.org/draft-07/schema#' }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #5
0
        public void RefToBooleanSchemaFalse(string desc, string data, bool expected)
        {
            // $ref to boolean schema false
            Console.Error.WriteLine(desc);
            string   schemaData = "{ '$ref':'#/definitions/bool', 'definitions':{ 'bool':false } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #6
0
        public void RelativePointerRefToObject(string desc, string data, bool expected)
        {
            // relative pointer ref to object
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'properties':{ 'bar':{ '$ref':'#/properties/foo' }, 'foo':{ 'type':'integer' } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #7
0
        public void NestedRefs(string desc, string data, bool expected)
        {
            // nested refs
            Console.Error.WriteLine(desc);
            string   schemaData = "{ '$ref':'#/definitions/c', 'definitions':{ 'a':{ 'type':'integer' }, 'b':{ '$ref':'#/definitions/a' }, 'c':{ '$ref':'#/definitions/b' } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #8
0
        public void PatternIsNotAnchored(string desc, string data, bool expected)
        {
            // pattern is not anchored
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'pattern':'a+' }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #9
0
        public void ByNumber(string desc, string data, bool expected)
        {
            // by number
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'multipleOf':1.5 }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #10
0
        public void AnyOfTest(string desc, string data, bool expected)
        {
            // anyOf
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ { 'type':'integer' }, { 'minimum':2 } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #11
0
        public void AnyOfWithBaseSchema(string desc, string data, bool expected)
        {
            // anyOf with base schema
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ { 'maxLength':2 }, { 'minLength':4 } ], 'type':'string' }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #12
0
        public void NestedAnyOfToCheckValidationSemantics(string desc, string data, bool expected)
        {
            // nested anyOf, to check validation semantics
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ { 'anyOf':[ { 'type':'null' } ] } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #13
0
        public void AnyOfWithOneEmptySchema(string desc, string data, bool expected)
        {
            // anyOf with one empty schema
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ { 'type':'number' }, { } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #14
0
        public void AnyOfComplexTypes(string desc, string data, bool expected)
        {
            // anyOf complex types
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'anyOf':[ { 'properties':{ 'bar':{ 'type':'integer' } }, 'required':[ 'bar' ] }, { 'properties':{ 'foo':{ 'type':'string' } }, 'required':[ 'foo' ] } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #15
0
        public void ValidationOfEMailAddresses(string desc, string data, bool expected)
        {
            // validation of e-mail addresses
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'format':'email' }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft4
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void AdditionalPropertiesAllowsASchemaWhichShouldValidate(string desc, string data, bool expected)
        {
            // additionalProperties allows a schema which should validate
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':{ 'type':'boolean' }, 'properties':{ 'bar':{ }, 'foo':{ } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #17
0
        public void RootPointerRef(string desc, string data, bool expected)
        {
            // root pointer ref
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':false, 'properties':{ 'foo':{ '$ref':'#' } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void AdditionalPropertiesCanExistByItself(string desc, string data, bool expected)
        {
            // additionalProperties can exist by itself
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':{ 'type':'boolean' } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #19
0
        public void RelativePointerRefToArray(string desc, string data, bool expected)
        {
            // relative pointer ref to array
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'items':[ { 'type':'integer' }, { '$ref':'#/items/0' } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void AdditionalPropertiesAreAllowedByDefault(string desc, string data, bool expected)
        {
            // additionalProperties are allowed by default
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'properties':{ 'bar':{ }, 'foo':{ } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #21
0
        public void RefOverridesAnySiblingKeywords(string desc, string data, bool expected)
        {
            // ref overrides any sibling keywords
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'definitions':{ 'reffed':{ 'type':'array' } }, 'properties':{ 'foo':{ '$ref':'#/definitions/reffed', 'maxItems':2 } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
        public void AdditionalPropertiesShouldNotLookInApplicators(string desc, string data, bool expected)
        {
            // additionalProperties should not look in applicators
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'additionalProperties':{ 'type':'boolean' }, 'allOf':[ { 'properties':{ 'foo':{ } } } ] }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #23
0
        public void PropertyNamedRefThatIsNotAReference(string desc, string data, bool expected)
        {
            // property named $ref that is not a reference
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'properties':{ '$ref':{ 'type':'string' } } }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #24
0
        public void ValidationOfRelativeJSONPointersRJP(string desc, string data, bool expected)
        {
            // validation of Relative JSON Pointers (RJP)
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'format':'relative-json-pointer' }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft7
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #25
0
        public void MaxPropertiesValidation(string desc, string data, bool expected)
        {
            // maxProperties validation
            Console.Error.WriteLine(desc);
            string   schemaData = "{ 'maxProperties':2 }";
            MPJson   schemaJson = MPJson.Parse(schemaData);
            MPJson   json       = MPJson.Parse(data);
            MPSchema schema     = schemaJson;
            var      validator  = new JsonValidator {
                Strict = true, Version = SchemaVersion.Draft6
            };
            bool actual = validator.Validate(schema, json);

            Assert.Equal(expected, actual);
        }
Beispiel #26
0
        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"));
        }
Beispiel #27
0
        public void TypeNullSchemaTests()
        {
            var schema = new MPSchema(
                MPJson.Object(
                    MPJson.Property("type", MPJson.Array("null"))
                    )
                );

            Assert.True(schema.IsValid);
            Assert.True(schema.Validate(MPJson.Null));
            Assert.False(schema.Validate("x"));
            Assert.False(schema.Validate(123));
            Assert.False(schema.Validate(true));
            Assert.False(schema.Validate(MPJson.Object()));
            Assert.False(schema.Validate(MPJson.Array()));
        }
Beispiel #28
0
        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);
        }
Beispiel #29
0
        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);
        }
Beispiel #30
0
        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)));
        }