public static string Serialize <T>(this JsonSchema s, T o, JsonSchemaValidationContext c = null)
        {
            var f = new JsonFormatter();

            s.Serialize(f, o, c);
            return(f.ToString());
        }
Esempio n. 2
0
        public JsonSchemaValidationException Validate <T>(JsonSchemaValidationContext c, T o)
        {
            if (o == null)
            {
                return(new JsonSchemaValidationException(c, "null"));
            }

            var value = o as string;

            if (value.All(x => Char.IsWhiteSpace(x)))
            {
                return(new JsonSchemaValidationException(c, "whitespace"));
            }

            if (MinLength.HasValue && value.Length < MinLength)
            {
                return(new JsonSchemaValidationException(c, string.Format("minlength: {0}<{1}", value.Length, MinLength.Value)));
            }
            if (MaxLength.HasValue && value.Length > MaxLength)
            {
                return(new JsonSchemaValidationException(c, string.Format("maxlength: {0}>{1}", value.Length, MaxLength.Value)));
            }

            if (Pattern != null && !Pattern.IsMatch(value))
            {
                return(new JsonSchemaValidationException(c, string.Format("pattern: {0} not match {1}", Pattern, value)));
            }

            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 void Serialize(JsonFormatter f, JsonSchemaValidationContext c, Object o)
        {
            // validate properties
            m_validValueMap.Clear();
            foreach (var kv in Properties)
            {
                var value = o.GetValueByKey(kv.Key);
                var v     = kv.Value.Validator;
                using (c.Push(kv.Key))
                {
                    if (v != null && v.Validate(c, value) == null)
                    {
                        m_validValueMap.Add(kv.Key, value);
                    }
                }
            }

            using (f.BeginMapDisposable())
            {
                foreach (var kv in Properties)
                {
                    object value;
                    if (!m_validValueMap.TryGetValue(kv.Key, out value))
                    {
                        continue;
                    }

                    string[] dependencies;
                    if (Dependencies.TryGetValue(kv.Key, out dependencies))
                    {
                        // check dependencies
                        bool hasDependencies = true;
                        foreach (var x in dependencies)
                        {
                            if (!m_validValueMap.ContainsKey(x))
                            {
                                hasDependencies = false;
                                break;
                            }
                        }
                        if (!hasDependencies)
                        {
                            continue;
                        }
                    }

                    // key
                    f.Key(kv.Key);

                    // value
                    using (c.Push(kv.Key))
                    {
                        kv.Value.Validator.Serialize(f, c, value);
                    }
                }
            }
        }
Esempio n. 5
0
 public void Serialize(JsonObjectValidator v, IFormatter f, JsonSchemaValidationContext c, T value)
 {
     f.BeginMap(m_fieldSerializers.Count);
     foreach (var s in m_fieldSerializers)
     {
         s(v, f, c, value);
     }
     f.EndMap();
 }
            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);
            }
Esempio n. 7
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"));
            }
        }
Esempio n. 8
0
 public static JsonSchemaValidationException Validate(List <string> required,
                                                      Dictionary <string, JsonSchema> properties,
                                                      JsonSchemaValidationContext c, T o)
 {
     if (s_validator == null)
     {
         s_validator = new ObjectValidator();
     }
     return(s_validator.Validate(required, properties, c, o));
 }
        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());
        }
        public void StringEnumValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonStringEnumValidator();
                v.Values = new string[] { "a", "b" };
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, "c"));
            }
        }
Esempio n. 11
0
 public JsonSchemaValidationException Validate(JsonSchemaValidationContext c, object o)
 {
     if (Values.Contains((int)o))
     {
         return(null);
     }
     else
     {
         return(new JsonSchemaValidationException(c, string.Format("{0} is not valid enum", o)));
     }
 }
Esempio n. 12
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)));
     }
 }
Esempio n. 13
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));
            }
        }
Esempio n. 14
0
        public void Serialize <T>(IFormatter f, T o)
        {
            var c = new JsonSchemaValidationContext(o);

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

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

            Validator.Serialize(f, c, o);
        }
Esempio n. 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
         }));
     }
 }
        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));
        }
        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);
        }
Esempio n. 18
0
 static void ArraySerializer <U>(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, U[] array)
 {
     f.BeginList(array.Length);
     {
         //int i = 0;
         foreach (var x in array)
         {
             //using (c.Push(i++))
             {
                 v.Serialize(f, c, x);
             }
         }
     }
     f.EndList();
 }
        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);
        }
Esempio n. 20
0
 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();
 }
Esempio n. 21
0
        public string Serialize(Object o)
        {
            var c = new JsonSchemaValidationContext(o);

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

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

            var f = new JsonFormatter();

            Validator.Serialize(f, c, o);
            return(f.ToString());
        }
        public void Serialize(JsonFormatter f, JsonSchemaValidationContext c, object o)
        {
            var array = o as IEnumerable;

            using (f.BeginListDisposable())
            {
                int i = 0;
                foreach (var x in array)
                {
                    using (c.Push(i++))
                    {
                        Items.Validator.Serialize(f, c, x);
                    }
                }
            }
        }
        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());
            }
        }
Esempio n. 24
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[] { }));
            }
        }
Esempio n. 25
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);
            }
                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,
                        });
                    }
                }
                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);
                }
        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);
        }
        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);
        }
        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());
            }
        }