Module: Tamara::JsonSchemas::Item

Extended by:
CheckoutOptionalKeys
Defined in:
lib/tamara/json_schemas/item.rb

Constant Summary collapse

REQUIRED_KEYS =
%w[name quantity reference_id type sku total_amount].freeze

Class Method Summary collapse

Methods included from CheckoutOptionalKeys

checkout_optional_keys

Class Method Details

.filtered_keysObject

rubocop:enable Metrics/AbcSize



32
33
34
35
36
37
38
# File 'lib/tamara/json_schemas/item.rb', line 32

def self.filtered_keys
  @filtered_keys ||= begin
    optional_keys = (checkout_optional_keys[:items] || []).map(&:to_s)

    REQUIRED_KEYS - optional_keys
  end
end

.schema(params = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tamara/json_schemas/item.rb', line 9

def self.schema(params = nil)
  @params = params
  {
    "$schema": "http://json-schema.org/draft-06/schema",
    type: "object",
    properties: {
      name: Types::String.schema(allows_null: filtered_keys.exclude?("name")),
      quantity: Types::Integer.schema(min: 1, allows_null: filtered_keys.exclude?("quantity")),
      reference_id: Types::String.schema(allows_null: filtered_keys.exclude?("reference_id")),
      type: Types::String.schema(allows_null: filtered_keys.exclude?("type")),
      sku: Types::String.schema(max_length: 128, allows_null: filtered_keys.exclude?("sku")),
      item_url: Types::Url.schema(allows_null: filtered_keys.exclude?("item_url")),
      image_url: Types::Url.schema(allows_null: filtered_keys.exclude?("image_url")),
      unit_price: Amount.schema(allows_null: filtered_keys.exclude?("unit_price")),
      tax_amount: Amount.schema(allows_null: filtered_keys.exclude?("tax_amount")),
      discount_amount: Amount.schema(allows_null: filtered_keys.exclude?("discount_amount")),
      total_amount: Amount.schema(allows_null: filtered_keys.exclude?("total_amount"))
    },
    required: filtered_keys
  }
end