Sha256: 78a129f757d0ab6e665443077bbe1caaaa53f82cfda3ed209c520f10ba2f8e92

Contents?: true

Size: 979 Bytes

Versions: 1

Compression:

Stored size: 979 Bytes

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 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

      private

      def filter_internal(array_value, user_options)
        raise ValidationError.new('参数应该传递一个数组') unless array_value.respond_to?(:each_with_index)
        array_value.each_with_index.map do |item, index|
          begin
            @items.filter(item, user_options)
          rescue ValidationErrors => e
            raise e.prepend_root("[#{index}]")
          end
        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/schemas/array_schema.rb