Esempio n. 1
0
        public void NumberValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonNumberValidator();
                v.Maximum = 0.1;
                Assert.NotNull(v.Validate(c, 1));
                Assert.Null(v.Validate(c, 0.1));
                Assert.Null(v.Validate(c, -1));
            }
            {
                var v = new JsonNumberValidator();
                v.Maximum          = 0.1;
                v.ExclusiveMaximum = true;
                Assert.NotNull(v.Validate(c, 1));
                Assert.NotNull(v.Validate(c, 0.1));
                Assert.Null(v.Validate(c, -1));
            }
            {
                var v = new JsonNumberValidator();
                v.Minimum = 0.1;
                Assert.Null(v.Validate(c, 1));
                Assert.Null(v.Validate(c, 0.1));
                Assert.NotNull(v.Validate(c, -1));
            }
            {
                var v = new JsonNumberValidator();
                v.Minimum          = 0.1;
                v.ExclusiveMinimum = true;
                Assert.Null(v.Validate(c, 1));
                Assert.NotNull(v.Validate(c, 0.1));
                Assert.NotNull(v.Validate(c, -1));
            }

            Assert.True(c.IsEmpty());
        }
        public static IJsonSchemaValidator Create(ValueNodeType valueType,
                                                  Type t = null,
                                                  BaseJsonSchemaAttribute a  = null,
                                                  ItemJsonSchemaAttribute ia = null)
        {
            switch (valueType)
            {
            case ValueNodeType.Integer:
            {
                var v = new JsonIntValidator();

                if (a != null)
                {
                    if (!double.IsNaN(a.Minimum))
                    {
                        v.Minimum = (int)a.Minimum;
                    }
                    if (a.ExclusiveMinimum)
                    {
                        v.ExclusiveMinimum = a.ExclusiveMinimum;
                    }
                    if (!double.IsNaN(a.Maximum))
                    {
                        v.Maximum = (int)a.Maximum;
                    }
                    if (a.ExclusiveMaximum)
                    {
                        v.ExclusiveMaximum = a.ExclusiveMaximum;
                    }
                    if (a.MultipleOf != 0)
                    {
                        v.MultipleOf = (int)a.MultipleOf;
                    }
                }

                return(v);
            }

            case ValueNodeType.Number:
            {
                var v = new JsonNumberValidator();
                if (a != null)
                {
                    if (!double.IsNaN(a.Minimum))
                    {
                        v.Minimum = (int)a.Minimum;
                    }
                    if (a.ExclusiveMinimum)
                    {
                        v.ExclusiveMinimum = a.ExclusiveMinimum;
                    }
                    if (!double.IsNaN(a.Maximum))
                    {
                        v.Maximum = (int)a.Maximum;
                    }
                    if (a.ExclusiveMaximum)
                    {
                        v.ExclusiveMaximum = a.ExclusiveMaximum;
                    }
                    if (a.MultipleOf != 0)
                    {
                        v.MultipleOf = (int)a.MultipleOf;
                    }
                }

                return(v);
            }

            case ValueNodeType.String:
            {
                var v = new JsonStringValidator();
                if (a != null)
                {
                    if (a.Pattern != null)
                    {
                        v.Pattern = new System.Text.RegularExpressions.Regex(a.Pattern);
                    }
                }
                return(v);
            }

            case ValueNodeType.Boolean:
                return(new JsonBoolValidator());

            case ValueNodeType.Array:
            {
                var v = new JsonArrayValidator();
                if (a != null)
                {
                    if (a.MinItems != 0)
                    {
                        v.MinItems = a.MinItems;
                    }
                    if (a.MaxItems != 0)
                    {
                        v.MaxItems = a.MaxItems;
                    }

                    if (t != null)
                    {
                        if (ia == null)
                        {
                            ia = new ItemJsonSchemaAttribute();
                        }

                        Type elementType = null;
                        if (t.IsArray)
                        {
                            elementType = t.GetElementType();
                        }
                        else if (t.GetIsGenericList())
                        {
                            elementType = t.GetGenericArguments().First();
                        }

                        if (elementType != null)
                        {
                            /*
                             * var sub = new JsonSchema
                             * {
                             *  SkipComparison = ia.SkipSchemaComparison,
                             *  Validator = Create(elementType, ia, null)
                             * };
                             */
                            var sub = JsonSchema.FromType(elementType, ia, null);
                            v.Items = sub;
                        }
                    }
                }

                return(v);
            }

            case ValueNodeType.Object:
            {
                if (t.GetIsGenericDictionary())
                {
                    var genericFactory =
                        typeof(JsonDictionaryValidator).GetMethod("Create",
                                                                  BindingFlags.Static | BindingFlags.Public);
                    var factory = genericFactory.MakeGenericMethod(t.GetGenericArguments()[1]);
                    var v       = factory.Invoke(null, null) as IJsonSchemaValidator;
                    return(v);
                }
                else
                {
                    var v = new JsonObjectValidator();
                    if (a != null)
                    {
                        if (a.MinProperties > 0)
                        {
                            v.MinProperties = a.MinProperties;
                        }

                        // props
                        foreach (var prop in GetProperties(t, a.ExportFlags))
                        {
                            v.Properties.Add(prop.Key, prop.Schema);
                            if (prop.Required)
                            {
                                v.Required.Add(prop.Key);
                            }
                            if (prop.Dependencies != null)
                            {
                                v.Dependencies.Add(prop.Key, prop.Dependencies);
                            }
                        }
                    }

                    if (ia != null)
                    {
                        var sub = new JsonSchema
                        {
                            SkipComparison = ia.SkipSchemaComparison,
                            Validator      = Create(typeof(object), ia, null)
                        };
                        v.AdditionalProperties = sub;
                    }

                    return(v);
                }
            }

            default:
                throw new NotImplementedException();
            }
        }