Module: Spree::PreferenceSchema::JsonSchema

Defined in:
app/models/concerns/spree/preference_schema/json_schema.rb

Overview

Compiles a class's preference declarations into the JSON Schema of its preferences object — the contract the OpenAPI components, the SDK types, the /…/types endpoints and write validation all read, so none of them is written by hand.

Constant Summary collapse

DECIMAL_PATTERN =
'^-?\d+(\.\d+)?$'.freeze
FORMATS =

Domain formats, with the pattern that makes each one checkable by any validator, not only those that know the format's name.

{
  currency: { format: 'currency', pattern: '^[A-Z]{3}$' },
  iso_country: { format: 'iso-country', pattern: '^[A-Z]{2}$' },
  timezone: { format: 'timezone' },
  locale: { format: 'locale' },
  url: { format: 'uri' },
  color: { format: 'color', pattern: '^#[0-9A-Fa-f]{6}$' }
}.freeze

Class Method Summary collapse

Class Method Details

.for(klass) ⇒ Hash

Returns a JSON Schema object (string keys).

Parameters:

Returns:

  • (Hash) —

    a JSON Schema object (string keys)



23
24
25
26
27
28
29
30
31
# File 'app/models/concerns/spree/preference_schema/json_schema.rb', line 23

def self.for(klass)
  fields = klass.preference_schema
  definitions = klass.preference_definitions
  properties = fields.to_h do |field|
    [field[:key].to_s, property_schema(definitions.fetch(field[:key]), default: field[:default])]
  end

  { 'type' => 'object', 'properties' => properties, 'additionalProperties' => false }
end

.property_schema(definition, default: declared_default(definition)) ⇒ Hash

The schema of one declared preference.

Parameters:

  • definition (Hash) —

    an entry of preference_definitions

  • default (Object) (defaults to: declared_default(definition)) —

    the declared default, already evaluated

Returns:

  • (Hash)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/concerns/spree/preference_schema/json_schema.rb', line 51

def self.property_schema(definition, default: declared_default(definition))
  schema = value_schema(definition)
  choices = definition[:choices]
  choices = choices.call if choices.respond_to?(:call)
  if choices.present?
    # `{ value => label }` names values that cannot label themselves — an
    # endpoint URL is no i18n key, let alone something to show a merchant.
    labels = choices.to_h { |value, label| [value.to_s, label.to_s] } if choices.is_a?(Hash)
    values = choices.is_a?(Hash) ? choices.keys : Array(choices)
    schema['enum'] = values.map { |choice| choice.is_a?(Symbol) ? choice.to_s : choice }
    schema['x-spree-choice-labels'] = labels if labels
  end
  default = definition[:dynamic_default] ? nil : wire_default(default, definition)
  unset_reads_null = default.nil? && !definition[:dynamic_default]

  # A value nothing has set reads back as null, so a preference without
  # a default is nullable on the wire whatever its declared type — and so
  # is a secret, whose default is never sent.
  if (definition[:nullable] || unset_reads_null || definition[:type] == :password) && schema['type']
    schema['type'] = [schema['type'], 'null']
    schema['enum'] += [nil] if schema['enum']
  end
  # A secret's default would leak alongside the masked value.
  schema['default'] = default unless default.nil? || definition[:type] == :password
  schema
end

.writable(klass) ⇒ Hash

The published schema plus the deprecated preferences, which a write still accepts until their removal although no read returns them.

Parameters:

  • klass (Class)

Returns:

  • (Hash)


38
39
40
41
42
43
44
# File 'app/models/concerns/spree/preference_schema/json_schema.rb', line 38

def self.writable(klass)
  deprecated = klass.preference_definitions.filter_map do |name, definition|
    [name.to_s, property_schema(definition)] if definition[:deprecated] && !definition[:internal]
  end
  schema = klass.preference_json_schema
  schema.merge('properties' => schema['properties'].merge(deprecated.to_h))
end