Sha256: fbde0f3765aa88c08567c10af3d2d9d0a7c170896ae0e3ea1551b7de0c400e1b

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Meta
  module JsonSchema
    class ArraySchema < BaseSchema
      attr_reader :items

      def initialize(items, options = {})
        super(options)

        @items = items
      end

      def filter(array_value, options = {})
        array_value = super(array_value, options)
        return nil if array_value.nil?
        raise ValidationError.new('参数应该传递一个数组') unless array_value.respond_to?(:each_with_index)

        array_value.each_with_index.map do |item, index|
          begin
            @items.filter(item, **options)
          rescue ValidationErrors => e
            raise e.prepend_root("[#{index}]")
          end
        end
      end

      def to_schema_doc(**user_options)
        stage_options = options

        schema = {
          type: 'array',
          items: @items ? @items.to_schema_doc(**user_options) : {}
        }
        schema[:description] = stage_options[:description] if stage_options[:description]
        schema
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
meta-api-0.0.8 lib/meta/json_schema/schemas/array_schema.rb
meta-api-0.0.7 lib/meta/json_schema/schemas/array_schema.rb
meta-api-0.0.6 lib/meta/json_schema/schemas/array_schema.rb
meta-api-0.0.5 lib/meta/json_schema/schemas/array_schema.rb
meta-api-0.0.4 lib/meta/json_schema/schemas/array_schema.rb
meta-api-0.0.3 lib/meta/json_schema/schemas/array_schema.rb