コード例 #1
0
        public void Merge(BaseJsonSchemaAttribute rhs)
        {
            if (rhs == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(Title))
            {
                Title = rhs.Title;
            }
        }
コード例 #2
0
        public static IJsonSchemaValidator Create(JsonValueType valueType,
                                                  Type t = null,
                                                  BaseJsonSchemaAttribute a  = null,
                                                  ItemJsonSchemaAttribute ia = null)
        {
            switch (valueType)
            {
            case JsonValueType.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 JsonValueType.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 JsonValueType.String:
            {
                var v = new JsonStringValidator();
                if (a != null)
                {
                    if (a.Pattern != null)
                    {
                        v.Pattern = new System.Text.RegularExpressions.Regex(a.Pattern);
                    }
                }
                return(v);
            }

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

            case JsonValueType.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 JsonValueType.Object:
            {
                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();
            }
        }
コード例 #3
0
 public static IJsonSchemaValidator Create(Type t, BaseJsonSchemaAttribute a, ItemJsonSchemaAttribute ia)
 {
     return(Create(ToJsonType(t), t, a, ia));
 }
コード例 #4
0
        public static JsonSchema FromType(Type t,
                                          BaseJsonSchemaAttribute a  = null, // field attribute
                                          ItemJsonSchemaAttribute ia = null
                                          )
        {
            // class attribute
            var aa = t.GetCustomAttributes(typeof(JsonSchemaAttribute), true)
                     .FirstOrDefault() as JsonSchemaAttribute;

            if (a != null)
            {
                a.Merge(aa);
            }
            else
            {
                if (aa == null)
                {
                    a = new JsonSchemaAttribute();
                }
                else
                {
                    a = aa;
                }
            }

            if (ia == null)
            {
                ia = t.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true)
                     .FirstOrDefault() as ItemJsonSchemaAttribute;
            }

            IJsonSchemaValidator validator = null;
            bool skipComparison            = a.SkipSchemaComparison;

            if (t == typeof(object))
            {
                skipComparison = true;
            }

            if (a.EnumValues != null)
            {
                validator = JsonEnumValidator.Create(a.EnumValues);
            }
            else if (t.IsEnum)
            {
                validator = JsonEnumValidator.Create(t, a.EnumSerializationType, a.EnumExcludes);
            }
            else
            {
                validator = JsonSchemaValidatorFactory.Create(t, a, ia);
            }

            var schema = new JsonSchema
            {
                Title          = a.Title,
                Description    = a.Description,
                Validator      = validator,
                SkipComparison = skipComparison
            };

            return(schema);
        }
コード例 #5
0
ファイル: JsonSchema.cs プロジェクト: gpsnmeajp/Oredayo4V
        public static JsonSchema FromType(Type t,
                                          BaseJsonSchemaAttribute a  = null, // field attribute
                                          ItemJsonSchemaAttribute ia = null
                                          )
        {
            // class attribute
            var aa = t.GetCustomAttributes(typeof(JsonSchemaAttribute), true)
                     .FirstOrDefault() as JsonSchemaAttribute;

            if (a != null)
            {
                a.Merge(aa);
            }
            else
            {
                if (aa == null)
                {
                    a = new JsonSchemaAttribute();
                }
                else
                {
                    a = aa;
                }
            }

            if (ia == null)
            {
                ia = t.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true)
                     .FirstOrDefault() as ItemJsonSchemaAttribute;
            }

            IJsonSchemaValidator validator = null;
            bool skipComparison            = a.SkipSchemaComparison;

            if (t == typeof(object))
            {
                skipComparison = true;
            }

            if (a.EnumValues != null)
            {
                try
                {
                    validator = JsonEnumValidator.Create(a.EnumValues, a.EnumSerializationType);
                }
                catch (Exception)
                {
                    throw new Exception(String.Join(", ", a.EnumValues.Select(x => x.ToString()).ToArray()));
                }
            }
            else if (t.IsEnum)
            {
                validator = JsonEnumValidator.Create(t, a.EnumSerializationType, a.EnumExcludes);
            }
            else
            {
                validator = JsonSchemaValidatorFactory.Create(t, a, ia);
            }

            var schema = new JsonSchema
            {
                Title                       = a.Title,
                Description                 = a.Description,
                Validator                   = validator,
                SkipComparison              = skipComparison,
                ExplicitIgnorableValue      = a.ExplicitIgnorableValue,
                ExplicitIgnorableItemLength = a.ExplicitIgnorableItemLength,
            };

            return(schema);
        }