Sha256: c0c044a430683e0ff17f09d6a30a85ab48da35bfe5636ea9eb6e6639b08c937d

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require_relative 'ref_schema_builder'
require_relative 'dynamic_schema_builder'
require_relative 'array_schema_builder'
require_relative 'object_schema_builder'
require_relative '../schemas/staging_schema'

module Meta
  module JsonSchema
    class SchemaBuilderTool
      class << self
        SCHEMA_BUILDER_OPTIONS = Utils::KeywordArgs::Builder.build do
          permit_extras true

          key :ref, alias_names: [:using], normalizer: ->(entity) {
            if Meta.config.default_locked_scope && entity.is_a?(Class) && entity < Meta::Entity
              entity.locked(scope: Meta.config.default_locked_scope)
            else
              entity
            end
          }
          key :dynamic_ref, alias_names: [:dynamic_using], normalizer: ->(value) { value.is_a?(Proc) ? { resolve: value } : value }
        end
        def build(options = {}, &block)
          options = SCHEMA_BUILDER_OPTIONS.check(options)

          if apply_array_schema?(options, block)
            ArraySchemaBuilder.new(options, &block).to_schema
          elsif apply_ref_schema?(options, block)
            RefSchemaBuilder.new(options).to_schema
          elsif apply_dynamic_schema?(options, block)
            DynamicSchemaBuilder.new(options).to_schema
          elsif apply_object_schema?(options, block)
            ObjectSchemaBuilder.new(options, &block).to_schema
          else
            StagingSchema.build_from_options(options)
          end
        end

        private

        def apply_array_schema?(options, block)
          options[:type] == 'array'
        end

        def apply_object_schema?(options, block)
          (options[:type] == 'object' || options[:type].nil?) && (options[:properties] || block)
        end

        def apply_ref_schema?(options, block)
          options[:ref] != nil
        end

        def apply_dynamic_schema?(options, block)
          options[:dynamic_ref] != nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
meta-api-0.0.9 lib//meta/json_schema/builders/schema_builder_tool.rb