public static JsonSchemaValidationException Validate(HashSet <string> required,
                                                      Dictionary <string, JsonSchema> properties,
                                                      JsonSchemaValidationContext c, T o)
 {
     prepareValidator();
     return(s_validator.Validate(required, properties, c, o));
 }
                public void Serialize(JsonObjectValidator objectValidator,
                                      IFormatter f, JsonSchemaValidationContext c, T o)
                {
                    // Validates fields
                    var validationResults = new Dictionary <string, ValidationResult>();

                    GenericValidator <T> .ValidationResults(
                        objectValidator.Required, objectValidator.Properties,
                        c, o, validationResults);

                    // Serialize fields
                    f.BeginMap(objectValidator.Properties.Count());
                    foreach (var property in objectValidator.Properties)
                    {
                        var fieldName = property.Key;
                        var schema    = property.Value;

                        string[] deps = null;
                        objectValidator.Dependencies.TryGetValue(fieldName, out deps);

                        FieldSerializer fs;
                        if (m_serializers.TryGetValue(fieldName, out fs))
                        {
                            fs(schema, c, f, o, validationResults, deps);
                        }
                    }

                    f.EndMap();
                }
                public JsonSchemaValidationException ValidateProperty(
                    HashSet <string> required,
                    KeyValuePair <string, JsonSchema> property,
                    JsonSchemaValidationContext c,
                    T o,
                    out bool isIgnorable
                    )
                {
                    var fieldName = property.Key;
                    var schema    = property.Value;

                    isIgnorable = false;

                    FieldValidator fv;

                    if (m_validators.TryGetValue(fieldName, out fv))
                    {
                        var isRequired = required != null && required.Contains(fieldName);

                        bool isMemberIgnorable;
                        var  ex = fv(schema, c, o, out isMemberIgnorable);
                        if (ex != null)
                        {
                            isIgnorable = !isRequired && isMemberIgnorable;

                            if (isRequired || // required fields must be checked
                                c.EnableDiagnosisForNotRequiredFields)
                            {
                                return(ex);
                            }
                        }
                    }

                    return(null);
                }
 internal static void ValidationResults(HashSet <string> required,
                                        Dictionary <string, JsonSchema> properties,
                                        JsonSchemaValidationContext c, T o,
                                        Dictionary <string, ValidationResult> results)
 {
     prepareValidator();
     s_validator.ValidationResults(required, properties, c, o, results);
 }
            public static void Serialize(JsonObjectValidator objectValidator,
                                         IFormatter f, JsonSchemaValidationContext c, T value)
            {
                if (s_serializer == null)
                {
                    s_serializer = new Serializer();
                }

                s_serializer.Serialize(objectValidator, f, c, value);
            }
示例#6
0
 public JsonSchemaValidationException Validate <T>(JsonSchemaValidationContext c, T o)
 {
     if (Values.Contains(GenericCast <T, int> .Cast(o)))
     {
         return(null);
     }
     else
     {
         return(new JsonSchemaValidationException(c, string.Format("{0} is not valid enum", o)));
     }
 }
示例#7
0
        public void HasDictionaryObjectValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var s = JsonSchema.FromType <HasDictionary>();
                Assert.Null(s.Validator.Validate(c, new HasDictionary()));
            }

            Assert.True(c.IsEmpty());
        }
示例#8
0
        public void StringEnumValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = JsonStringEnumValidator.Create(new string[] { "a", "b" }, EnumSerializationType.AsString);
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, "c"));
            }

            Assert.True(c.IsEmpty());
        }
示例#9
0
        public void IntEnumValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonIntEnumValidator();
                v.Values = new int[] { 1, 2 };
                Assert.Null(v.Validate(c, 1));
                Assert.NotNull(v.Validate(c, 3));
            }

            Assert.True(c.IsEmpty());
        }
        public JsonSchemaValidationException Validate <T>(JsonSchemaValidationContext c, T o)
        {
            if (o == null)
            {
                return(new JsonSchemaValidationException(c, "null"));
            }

            if (Properties.Count < MinProperties)
            {
                return(new JsonSchemaValidationException(c, "no properties"));
            }

            return(GenericValidator <T> .Validate(Required, Properties, c, o));
        }
示例#11
0
 private static void ListSerializer <U>(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c,
                                        List <U> list)
 {
     f.BeginList(list.Count);
     {
         //int i = 0;
         foreach (var x in list)
         //using (c.Push(i++))
         {
             v.Serialize(f, c, x);
         }
     }
     f.EndList();
 }
示例#12
0
            public static void Serialize(JsonStringEnumValidator validator,
                                         IFormatter f, JsonSchemaValidationContext c, T o)
            {
                if (s_serializer == null)
                {
                    var t = typeof(T);
                    if (t.IsEnum)
                    {
                        s_serializer = (vv, ff, cc, oo) =>
                        {
                            var value = Enum.GetName(t, oo);
                            if (vv.SerializationType == EnumSerializationType.AsLowerString)
                            {
                                value = value.ToLower();
                            }
                            else if (vv.SerializationType == EnumSerializationType.AsUpperString)
                            {
                                value = value.ToUpper();
                            }
                            ff.Value(value);
                        }
                    }
                    ;
                    else if (t == typeof(string))
                    {
                        s_serializer = (vv, ff, cc, oo) =>
                        {
                            var value = GenericCast <T, string> .Cast(oo);

                            if (vv.SerializationType == EnumSerializationType.AsLowerString)
                            {
                                value = value.ToLower();
                            }
                            else if (vv.SerializationType == EnumSerializationType.AsUpperString)
                            {
                                value = value.ToUpper();
                            }
                            ff.Value(value);
                        }
                    }
                    ;
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                s_serializer(validator, f, c, o);
            }
示例#13
0
        public void TestHasDeps()
        {
            var obj = new HasDepsTest();

            var s = JsonSchema.FromType <HasDepsTest>();
            {
                var c = new JsonSchemaValidationContext(obj);
                Assert.Null(s.Validator.Validate(c, s));
            }
            var actual = s.Serialize(obj);

            var expected = @"{""X"":0,""Y"":0}";

            Assert.AreEqual(expected, actual);
        }
示例#14
0
        public void TestObjectNestedWithNull()
        {
            var obj = new ObjectNestedTest();

            var s = JsonSchema.FromType <ObjectNestedTest>();
            {
                var c = new JsonSchemaValidationContext(obj);
                Assert.Null(s.Validator.Validate(c, s));
            }
            var actual = s.Serialize(obj);

            var expected = @"{}";

            Assert.AreEqual(expected, actual);
        }
示例#15
0
        public void ObjectValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var s = JsonSchema.FromType <Hoge>();
                Assert.Null(s.Validator.Validate(c, new Hoge {
                    Value = 1
                }));
                Assert.NotNull(s.Validator.Validate(c, new Hoge {
                    Value = 0
                }));
            }

            Assert.True(c.IsEmpty());
        }
                public JsonSchemaValidationException Validate(
                    HashSet <string> required,
                    Dictionary <string, JsonSchema> properties,
                    JsonSchemaValidationContext c, T o)
                {
                    foreach (var kv in properties)
                    {
                        bool isIgnorable;
                        var  ex = ValidateProperty(required, kv, c, o, out isIgnorable);
                        if (ex != null && !isIgnorable)
                        {
                            return(ex);
                        }
                    }

                    return(null);
                }
示例#17
0
        public void ObjectValidatorForNotRequiredWithIgnorable()
        {
            {
                var c = new JsonSchemaValidationContext("test")
                {
                    EnableDiagnosisForNotRequiredFields = false, // Default behaviour
                };

                var s = JsonSchema.FromType <NotRequiredWithIgnorable>();
                // An error is not returned because Value is not 'Required' and the diagnosis is not enabled
                Assert.Null(s.Validator.Validate(c, new NotRequiredWithIgnorable {
                    Value = 0
                }));

                Assert.True(c.IsEmpty());
            }

            {
                var c = new JsonSchemaValidationContext("test")
                {
                    EnableDiagnosisForNotRequiredFields = true,
                };

                var s = JsonSchema.FromType <NotRequiredWithIgnorable>();
                Assert.NotNull(s.Validator.Validate(c, new NotRequiredWithIgnorable {
                    Value = 0
                }));

                Assert.True(c.IsEmpty());
            }

            {
                var c = new JsonSchemaValidationContext("test")
                {
                    EnableDiagnosisForNotRequiredFields = true,
                };

                var s = JsonSchema.FromType <NotRequiredWithIgnorable>();
                // An error is NOT returned even though diagnosis is enabled because of an ignorable value is matched
                Assert.Null(s.Validator.Validate(c, new NotRequiredWithIgnorable {
                    Value = -1
                }));

                Assert.True(c.IsEmpty());
            }
        }
示例#18
0
        public void HasRequiredStringObjectValidator()
        {
            {
                var c = new JsonSchemaValidationContext("test")
                {
                    EnableDiagnosisForNotRequiredFields = true,
                };

                var s = JsonSchema.FromType <HasRequiredStringObject>();

                Assert.NotNull(s.Validator.Validate(c, new HasRequiredStringObject()));
                Assert.Null(s.Validator.Validate(c, new HasRequiredStringObject {
                    s = ""
                }));

                Assert.True(c.IsEmpty());
            }
        }
                public void ValidationResults
                    (HashSet <string> required,
                    Dictionary <string, JsonSchema> properties,
                    JsonSchemaValidationContext c, T o,
                    Dictionary <string, ValidationResult> results)
                {
                    foreach (var kv in properties)
                    {
                        bool isIgnorable;
                        var  ex = ValidateProperty(required, kv, c, o, out isIgnorable);

                        results.Add(kv.Key, new ValidationResult
                        {
                            IsIgnorable = isIgnorable,
                            Ex          = ex,
                        });
                    }
                }
示例#20
0
        public void TestNestedRequired()
        {
            var obj = new NestedRequiredTestParent()
            {
                C = new NestedRequiredTestChild(),
            };

            var s = JsonSchema.FromType <NestedRequiredTestParent>();
            {
                var c = new JsonSchemaValidationContext(obj);
                Assert.Null(s.Validator.Validate(c, s));
            }
            var actual = s.Serialize(obj);

            var expected = @"{""C"":{}}";

            Assert.AreEqual(expected, actual);
        }
示例#21
0
        public void TestObjectNested()
        {
            var obj = new ObjectNestedTest()
            {
                C = new CheckConstraintsTest(),
            };

            var s = JsonSchema.FromType <ObjectNestedTest>();
            {
                var c = new JsonSchemaValidationContext(obj);
                Assert.Null(s.Validator.Validate(c, s));
            }
            var actual = s.Serialize(obj);

            var expected = @"{""C"":{""X"":0}}";

            Assert.AreEqual(expected, actual);
        }
示例#22
0
        public void TestCheckConstraints()
        {
            var obj = new CheckConstraintsTest()
            {
                X = 0,
                Y = 0, // Will be excluded because 0 doesn't satisfy a requirement of "Minimum = 10"
            };

            var s = JsonSchema.FromType <CheckConstraintsTest>();
            {
                var c = new JsonSchemaValidationContext(obj);
                Assert.Null(s.Validator.Validate(c, s));
            }
            var actual = s.Serialize(obj);

            var expected = @"{""X"":0}";

            Assert.AreEqual(expected, actual);
        }
示例#23
0
        public void IntValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonIntValidator();
                v.Maximum = 0;
                Assert.NotNull(v.Validate(c, 1));
                Assert.Null(v.Validate(c, 0));
                Assert.Null(v.Validate(c, -1));
            }
            {
                var v = new JsonIntValidator();
                v.Maximum          = 0;
                v.ExclusiveMaximum = true;
                Assert.NotNull(v.Validate(c, 1));
                Assert.NotNull(v.Validate(c, 0));
                Assert.Null(v.Validate(c, -1));
            }
            {
                var v = new JsonIntValidator();
                v.Minimum = 0;
                Assert.Null(v.Validate(c, 1));
                Assert.Null(v.Validate(c, 0));
                Assert.NotNull(v.Validate(c, -1));
            }
            {
                var v = new JsonIntValidator();
                v.Minimum          = 0;
                v.ExclusiveMinimum = true;
                Assert.Null(v.Validate(c, 1));
                Assert.NotNull(v.Validate(c, 0));
                Assert.NotNull(v.Validate(c, -1));
            }
            {
                var v = new JsonIntValidator();
                v.MultipleOf = 4;
                Assert.Null(v.Validate(c, 4));
                Assert.NotNull(v.Validate(c, 5));
            }

            Assert.True(c.IsEmpty());
        }
        public JsonSchemaValidationException Validate <S>(JsonSchemaValidationContext c, S o)
        {
            if (o == null)
            {
                return(new JsonSchemaValidationException(c, "null"));
            }

            var d = o as IDictionary <string, T>;

            if (d == null)
            {
                return(new JsonSchemaValidationException(c, "not dictionary"));
            }

            if (Required != null)
            {
                foreach (var x in Required)
                {
                    using (c.Push(x))
                    {
                        // ToDo
                    }
                }
            }

            if (AdditionalProperties != null)
            {
                foreach (var kv in d)
                {
                    using (c.Push(kv.Key))
                    {
                        var result = AdditionalProperties.Validator.Validate(c, kv.Value);
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
            }

            return(null);
        }
示例#25
0
        public void Serialize <T>(IFormatter f, T o, JsonSchemaValidationContext c = null)
        {
            if (c == null)
            {
                c = new JsonSchemaValidationContext(o)
                {
                    EnableDiagnosisForNotRequiredFields = true,
                }
            }
            ;

            var ex = Validator.Validate(c, o);

            if (ex != null)
            {
                throw ex;
            }

            Validator.Serialize(f, c, o);
        }
示例#26
0
        public void ArrayValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonArrayValidator();
                v.MaxItems = 1;
                Assert.Null(v.Validate(c, new object[] { 0 }));
                Assert.NotNull(v.Validate(c, new object[] { 0, 1 }));
            }

            {
                var v = new JsonArrayValidator();
                v.MinItems = 1;
                Assert.Null(v.Validate(c, new object[] { 0 }));
                Assert.NotNull(v.Validate(c, new object[] { }));
            }

            Assert.True(c.IsEmpty());
        }
示例#27
0
            public static void Serialize(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, T o)
            {
                if (s_serializer == null)
                {
                    var        t = typeof(T);
                    MethodInfo g = null;
                    if (t.IsArray)
                    {
                        var mi = typeof(JsonArrayValidator).GetMethod("ArraySerializer",
                                                                      BindingFlags.Static | BindingFlags.NonPublic);
                        g = mi.MakeGenericMethod(t.GetElementType());
                    }
                    else if (t.GetIsGenericList())
                    {
                        // ToDo: IList
                        var mi = typeof(JsonArrayValidator).GetMethod("ListSerializer",
                                                                      BindingFlags.Static | BindingFlags.NonPublic);
                        g = mi.MakeGenericMethod(t.GetGenericArguments());
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    var compiled = (Action <
                                        IJsonSchemaValidator,
                                        IFormatter,
                                        JsonSchemaValidationContext,
                                        T>)
                                   GenericInvokeCallFactory.StaticAction <
                        IJsonSchemaValidator,
                        IFormatter,
                        JsonSchemaValidationContext,
                        T>(g);
                    s_serializer = new Serializer(compiled);
                }

                s_serializer(v, f, c, o);
            }
示例#28
0
        public void StringValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonStringValidator();
                Assert.Null(v.Validate(c, ""));
                Assert.Null(v.Validate(c, "a"));
            }

            {
                var v = new JsonStringValidator();
                v.MinLength = 1;
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, ""));
            }
            {
                var v = new JsonStringValidator();
                v.MaxLength = 1;
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, "ab"));
            }
            {
                var v = new JsonStringValidator();
                v.Pattern = new System.Text.RegularExpressions.Regex("abc");
                Assert.Null(v.Validate(c, "abc"));
                Assert.NotNull(v.Validate(c, "ab"));
            }
            {
                var v = new JsonStringValidator();
                v.Pattern = new System.Text.RegularExpressions.Regex("ab+");
                Assert.Null(v.Validate(c, "abb"));
                Assert.Null(v.Validate(c, "ab"));
                Assert.NotNull(v.Validate(c, "a"));
            }

            Assert.True(c.IsEmpty());
        }
        public void Serialize <S>(IFormatter f, JsonSchemaValidationContext c, S o)
        {
            // validate properties
            m_validValueMap.Clear();

            var dict = o as Dictionary <string, T>;

            f.BeginMap(dict.Count);
            {
                foreach (var kv in dict)
                {
                    // key
                    f.Key(kv.Key);

                    // value
                    //using (c.Push(kv.Key))
                    {
                        AdditionalProperties.Validator.Serialize(f, c, kv.Value);
                    }
                }
            }
            f.EndMap();
        }
示例#30
0
        public void HasArrayObjectValidator()
        {
            {
                var c = new JsonSchemaValidationContext("test")
                {
                    EnableDiagnosisForNotRequiredFields = true,
                };

                var s = JsonSchema.FromType <HasArrayOBject>();

                Assert.Null(s.Validator.Validate(c, new HasArrayOBject {
                    xs = new float[] { }
                }));
                Assert.Null(s.Validator.Validate(c, new HasArrayOBject {
                    xs = new float[] { 0.5f }
                }));
                Assert.NotNull(s.Validator.Validate(c, new HasArrayOBject {
                    xs = new float[] { 1.5f }
                }));

                Assert.True(c.IsEmpty());
            }
        }