Ejemplo n.º 1
0
        private void Composite(CompositionType compositionType, List <JsonSchema> composition)
        {
            switch (compositionType)
            {
            case CompositionType.AllOf:
                if (composition.Count == 1)
                {
                    // inheritance
                    if (Validator == null)
                    {
                        Validator = composition[0].Validator;
                    }
                    else
                    {
                        Validator.Merge(composition[0].Validator);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }

                break;

            case CompositionType.AnyOf:
                if (Validator == null)
                {
                    if (composition.Count == 1)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        Validator = JsonEnumValidator.Create(composition, EnumSerializationType.AsString);
                    }
                }

                //throw new NotImplementedException();
                break;

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public void Parse(IFileSystemAccessor fs, ListTreeNode <JsonValue> root, string Key)
        {
            m_context.Push(Key);

            var compositionType = default(CompositionType);
            var composition     = new List <JsonSchema>();

            foreach (var kv in root.ObjectItems())
            {
                switch (kv.Key.GetString())
                {
                case "$schema":
                    Schema = kv.Value.GetString();
                    break;

                case "$ref":
                {
                    var refFs = fs.Get(kv.Value.GetString());

                    // parse JSON
                    var json    = refFs.ReadAllText();
                    var refRoot = JsonParser.Parse(json);

                    Parse(refFs, refRoot, "$ref");
                }
                break;

                    #region Annotation

                case "title":
                    Title = kv.Value.GetString();
                    break;

                case "description":
                    Description = kv.Value.GetString();
                    break;

                case "default":
                    Default = kv.Value;
                    break;

                    #endregion

                    #region Validation

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
                case "type":
                    if (Validator == null)
                    {
                        Validator = JsonSchemaValidatorFactory.Create(kv.Value.GetString());
                    }
                    break;

                case "enum":
                    Validator = JsonEnumValidator.Create(kv.Value);
                    break;

                case "const":
                    break;

                    #endregion

                    #region Composite

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7
                case "oneOf":
                    break;

                case "not":
                    break;

                case "anyOf":     // composition
                case "allOf":     // composition
                {
                    compositionType =
                        (CompositionType)Enum.Parse(typeof(CompositionType), kv.Key.GetString(), true);
                    foreach (var item in kv.Value.ArrayItems())
                    {
                        if (item.ContainsKey(s_ref))
                        {
                            var sub = ParseFromPath(fs.Get(item[s_ref].GetString()));
                            composition.Add(sub);
                        }
                        else
                        {
                            var sub = new JsonSchema();
                            sub.Parse(fs, item, compositionType.ToString());
                            composition.Add(sub);
                        }
                    }

                    Composite(compositionType, composition);
                }
                break;

                    #endregion

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.7
                case "format":
                    break;

                    #region Gltf

                case "gltf_detailedDescription":
                    break;

                case "gltf_webgl":
                    break;

                case "gltf_uriType":
                    break;

                    #endregion

                default:
                {
                    if (Validator != null)
                    {
                        if (Validator.FromJsonSchema(fs, kv.Key.GetString(), kv.Value))
                        {
                            continue;
                        }
                    }
                    throw new NotImplementedException(string.Format("unknown key: {0}", kv.Key));
                }
                }
            }
            m_context.Pop();

            if (Validator == null)
            {
                SkipComparison = true;
            }
        }
Ejemplo n.º 3
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;
            var 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);
        }