Example #1
0
        private static IJsonSchema _FromJson(JsonValue json, SchemaFactory schemaFactory, Uri documentPath = null)
        {
            if (json == null)
            {
                return(null);
            }
            IJsonSchema schema = null;

            switch (json.Type)
            {
            case JsonValueType.Object:
                var schemaDeclaration = json.Object.TryGetString("$schema");
                var factory           = _schemaFactories.FirstOrDefault(f => f.Id == schemaDeclaration);
                if (factory != null)
                {
                    var id = json.Object.TryGetString(factory.IdKey);
                    if (id == factory.MetaSchema.Id)
                    {
                        return(factory.MetaSchema);
                    }
                    schema = factory.Factory();
                }
                schema = schema ?? schemaFactory.Factory();
                if (json.Object.ContainsKey("$ref"))
                {
                    schema = json.Object.Count > 1
                                                                 ? new JsonSchemaReference(schema.GetType())
                    {
                        Base = schema
                    }
                }
                : new JsonSchemaReference(schema.GetType());
                break;

            case JsonValueType.Array:
                schema = new JsonSchemaCollection();
                break;

            case JsonValueType.Boolean:
                // not determining draft here is fine
                // True is intended to pass all instances
                // False is intended to fail all instances
                schema = new JsonSchema06();
                break;

            default:
                throw new SchemaLoadException($"JSON Schema must be objects; actual type: {json.Type}");
            }
            schema.DocumentPath = documentPath;
            schema.FromJson(json, null);
            return(schema);
        }
Example #2
0
        internal static IJsonSchema FromJson(JsonValue json, Func <IJsonSchema> schemaFactory, Uri documentPath = null)
        {
            if (json == null)
            {
                return(null);
            }
            IJsonSchema schema = null;

            switch (json.Type)
            {
            case JsonValueType.Object:
                var schemaDeclaration = json.Object.TryGetString("$schema");
                if (schemaDeclaration == JsonSchema04.MetaSchema.Id)
                {
                    var id = json.Object.TryGetString("id");
                    if (id == JsonSchema04.MetaSchema.Id)
                    {
                        return(JsonSchema04.MetaSchema);
                    }
                    schema = new JsonSchema04();
                }
                else if (schemaDeclaration == JsonSchema06.MetaSchema.Id)
                {
                    var id = json.Object.TryGetString("$id");
                    if (id == JsonSchema06.MetaSchema.Id)
                    {
                        return(JsonSchema06.MetaSchema);
                    }
                    schema = new JsonSchema06();
                }
                else if (schemaDeclaration == JsonSchema07.MetaSchema.Id)
                {
                    var id = json.Object.TryGetString("$id");
                    if (id == JsonSchema07.MetaSchema.Id)
                    {
                        return(JsonSchema07.MetaSchema);
                    }
                    schema = new JsonSchema07();
                }
                schema = schema ?? schemaFactory();
                if (json.Object.ContainsKey("$ref"))
                {
                    schema = json.Object.Count > 1
                                                                 ? new JsonSchemaReference(schema.GetType())
                    {
                        Base = schema
                    }
                }
                : new JsonSchemaReference(schema.GetType());
                break;

            case JsonValueType.Array:
                schema = new JsonSchemaCollection();
                break;

            case JsonValueType.Boolean:
                schema = new JsonSchema06();
                break;

            default:
                throw new SchemaLoadException($"JSON Schema must be objects; actual type: {json.Type}");
            }
            schema.DocumentPath = documentPath;
            schema.FromJson(json, null);
            return(schema);
        }