Class: Apiwork::Contract::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/contract/object.rb,
lib/apiwork/contract/object/coercer.rb,
lib/apiwork/contract/object/validator.rb,
lib/apiwork/contract/object/transformer.rb,
lib/apiwork/contract/object/deserializer.rb,
lib/apiwork/contract/object/validator/result.rb

Overview

Block context for defining request/response structure.

Accessed via ‘body do` and `query do` inside contract actions, or `object :name do` at contract level to define reusable types.

Examples:

instance_eval style

body do
  string :title
  decimal :amount
end

yield style

body do |body|
  body.string :title
  body.decimal :amount
end

See Also:

Defined Under Namespace

Classes: Coercer, Deserializer, Transformer, Validator

Instance Attribute Summary collapse

Attributes inherited from Object

#merged

Instance Method Summary collapse

Methods inherited from Object

#array?, #binary, #binary?, #boolean, #boolean?, #date, #date?, #datetime, #datetime?, #decimal, #decimal?, #extends, #integer, #integer?, #literal, #merge, #number, #number?, #object, #object?, #record?, #reference, #reference?, #string, #string?, #time, #time?, #union, #union?, #unknown, #unknown?, #uuid, #uuid?

Constructor Details

#initialize(contract_class, action_name: nil, visited_types: nil, wrapped: false) ⇒ Object

Returns a new instance of Object.



43
44
45
46
47
48
49
# File 'lib/apiwork/contract/object.rb', line 43

def initialize(contract_class, action_name: nil, visited_types: nil, wrapped: false)
  super()
  @contract_class = contract_class
  @action_name = action_name
  @wrapped = wrapped
  @visited_types = visited_types
end

Instance Attribute Details

#action_nameObject (readonly)



25
26
27
# File 'lib/apiwork/contract/object.rb', line 25

def action_name
  @action_name
end

#contract_classObject (readonly)



25
26
27
# File 'lib/apiwork/contract/object.rb', line 25

def contract_class
  @contract_class
end

Instance Method Details

#array(name, as: nil, default: UNSET, deprecated: false, description: nil, max: nil, min: nil, nullable: false, optional: false, required: false) {|element| ... } ⇒ void

This method returns an undefined value.

Defines an array param with element type.

Examples:

instance_eval style

array :tags do
  string
end

yield style

array :tags do |element|
  element.string
end

Parameters:

  • name (Symbol)

    The param name.

  • as (Symbol, nil) (defaults to: nil)

    (nil) The target attribute name.

  • default (Object) (defaults to: UNSET)

    (UNSET) The default value. Omit to declare no default. Pass ‘nil` for an explicit null default.

  • deprecated (Boolean) (defaults to: false)

    (false) Whether deprecated. Metadata included in exports.

  • description (String, nil) (defaults to: nil)

    (nil) The description. Metadata included in exports.

  • max (Integer, nil) (defaults to: nil)

    (nil) The maximum number of elements.

  • min (Integer, nil) (defaults to: nil)

    (nil) The minimum number of elements.

  • nullable (Boolean) (defaults to: false)

    (false) Whether the value can be ‘null`.

  • optional (Boolean) (defaults to: false)

    (false) Whether the param is optional.

  • required (Boolean) (defaults to: false)

    (false) Whether the param is required.

Yields:

  • block for defining element type

Yield Parameters:

Raises:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/apiwork/contract/object.rb', line 215

def array(
  name,
  as: nil,
  default: UNSET,
  deprecated: false,
  description: nil,
  max: nil,
  min: nil,
  nullable: false,
  optional: false,
  required: false,
  &block
)
  raise ConfigurationError, 'array requires a block' unless block

  element = Element.new(@contract_class)
  block.arity.positive? ? yield(element) : element.instance_eval(&block)
  element.validate!

  param(
    name,
    as:,
    default:,
    deprecated:,
    description:,
    max:,
    min:,
    nullable:,
    optional:,
    required:,
    of: element,
    type: :array,
  )
end

#coerce(data) ⇒ Object



321
322
323
# File 'lib/apiwork/contract/object.rb', line 321

def coerce(data)
  Coercer.coerce(self, data)
end

#copy_type_definition_params(type_definition, target_param) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/apiwork/contract/object.rb', line 333

def copy_type_definition_params(type_definition, target_param)
  return unless type_definition.object?

  type_definition.params.each do |param_name, param_options|
    nested_shape = param_options[:shape]

    if param_options[:type] == :array && nested_shape.is_a?(API::Object)
      target_param.param(param_name, **param_options.except(:name))
    elsif param_options[:type] == :array
      target_param.param(param_name, **param_options.except(:name, :shape, :discriminator))
    elsif nested_shape.is_a?(API::Object)
      copy_nested_object_param(target_param, param_name, param_options, nested_shape)
    elsif nested_shape.is_a?(API::Union)
      copy_nested_union_param(target_param, param_name, param_options, nested_shape)
    else
      target_param.param(param_name, **param_options.except(:name, :shape))
    end
  end
end

#deserialize(data) ⇒ Object



325
326
327
# File 'lib/apiwork/contract/object.rb', line 325

def deserialize(data)
  Deserializer.deserialize(self, data)
end

#param(name, type: nil, as: nil, custom_type: nil, default: UNSET, deprecated: false, description: nil, discriminator: nil, enum: nil, example: nil, format: nil, max: nil, min: nil, nullable: false, of: nil, optional: false, required: false, shape: nil, value: nil) {|shape| ... } ⇒ void

This method returns an undefined value.

Defines a param with explicit type.

This is the verbose form. Prefer sugar methods (string, integer, etc.) for static definitions.

Examples:

Dynamic param generation

param_type = :string
param :title, type: param_type

Object with block

param :address, type: :object do
  string :street
  string :city
end

Parameters:

  • name (Symbol)

    The param name.

  • type (Symbol, nil) (defaults to: nil)

    (nil) [:array, :binary, :boolean, :date, :datetime, :decimal, :integer, :literal, :number, :object, :record, :string, :time, :union, :unknown, :uuid] The param type.

  • as (Symbol, nil) (defaults to: nil)

    (nil) The target attribute name.

  • default (Object) (defaults to: UNSET)

    (UNSET) The default value. Omit to declare no default. Pass ‘nil` for an explicit null default.

  • deprecated (Boolean) (defaults to: false)

    (false) Whether deprecated. Metadata included in exports.

  • description (String, nil) (defaults to: nil)

    (nil) The description. Metadata included in exports.

  • discriminator (Symbol, nil) (defaults to: nil)

    (nil) The discriminator param name. Unions only.

  • enum (Array, Symbol, nil) (defaults to: nil)

    (nil) The allowed values or enum reference.

  • example (Object, nil) (defaults to: nil)

    (nil) The example value. Metadata included in exports.

  • format (Symbol, nil) (defaults to: nil)

    (nil) [:date, :datetime, :double, :email, :float, :hostname, :int32, :int64, :ipv4, :ipv6, :password, :text, :url, :uuid] Format hint for exports. Does not change the type, but exports may add validation or documentation based on it. Valid formats by type: ‘:decimal`/`:number` (`:double`, `:float`), `:integer` (`:int32`, `:int64`), `:string` (`:date`, `:datetime`, `:email`, `:hostname`, `:ipv4`, `:ipv6`, `:password`, `:text`, `:url`, `:uuid`).

  • max (Integer, nil) (defaults to: nil)

    (nil) The maximum. For ‘:array`: size. For `:decimal`, `:integer`, `:number`: value. For `:string`: length.

  • min (Integer, nil) (defaults to: nil)

    (nil) The minimum. For ‘:array`: size. For `:decimal`, `:integer`, `:number`: value. For `:string`: length.

  • nullable (Boolean) (defaults to: false)

    (false) Whether the value can be ‘null`.

  • of (Symbol, Hash, nil) (defaults to: nil)

    (nil) The element or value type. Arrays and records only.

  • optional (Boolean) (defaults to: false)

    (false) Whether the param is optional.

  • required (Boolean) (defaults to: false)

    (false) Whether the param is required.

  • shape (Contract::Object, Contract::Union, nil) (defaults to: nil)

    (nil) The pre-built shape.

  • value (Object, nil) (defaults to: nil)

    (nil) The literal value.

Yields:

  • block for nested structure

Yield Parameters:

Raises:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/apiwork/contract/object.rb', line 108

def param(
  name,
  type: nil,
  as: nil,
  custom_type: nil,
  default: UNSET,
  deprecated: false,
  description: nil,
  discriminator: nil,
  enum: nil,
  example: nil,
  format: nil,
  max: nil,
  min: nil,
  nullable: false,
  of: nil,
  optional: false,
  required: false,
  shape: nil,
  value: nil,
  &block
)
  options = {
    deprecated:,
    description:,
    example:,
    format:,
    max:,
    min:,
    nullable:,
    required:,
  }

  raise ConfigurationError, 'discriminator can only be used with type: :union' if discriminator && type != :union

  visited_types ||= @visited_types
  visited_types ||= Set.new

  resolved_enum = resolve_enum(enum)

  case type
  when :literal
    define_literal_param(name, as:, default:, deprecated:, description:, optional:, value:)
  when :union
    define_union_param(
      name,
      as:,
      default:,
      discriminator:,
      optional:,
      options:,
      resolved_enum:,
      &block
    )
  else
    define_regular_param(
      name,
      as:,
      default:,
      of:,
      optional:,
      options:,
      resolved_enum:,
      shape:,
      type:,
      visited_types:,
      &block
    )
  end
end

#paramsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apiwork/contract/object.rb', line 28

def params
  return @params if @merged.empty?

  expanded = @params.dup
  @merged.each do |type_name|
    merged_type = @contract_class.api_class&.type_registry&.[](type_name)
    next unless merged_type&.params

    merged_type.params.each do |name, param_options|
      expanded[name] ||= param_options
    end
  end
  expanded
end

#record(name, as: nil, default: UNSET, deprecated: false, description: nil, nullable: false, optional: false, required: false) {|element| ... } ⇒ void

This method returns an undefined value.

Defines a record param with value type.

Examples:

instance_eval style

record :scores do
  integer
end

yield style

record :scores do |element|
  element.integer
end

Parameters:

  • name (Symbol)

    The param name.

  • as (Symbol, nil) (defaults to: nil)

    (nil) The target attribute name.

  • default (Object) (defaults to: UNSET)

    (UNSET) The default value. Omit to declare no default. Pass ‘nil` for an explicit null default.

  • deprecated (Boolean) (defaults to: false)

    (false) Whether deprecated. Metadata included in exports.

  • description (String, nil) (defaults to: nil)

    (nil) The description. Metadata included in exports.

  • nullable (Boolean) (defaults to: false)

    (false) Whether the value can be ‘null`.

  • optional (Boolean) (defaults to: false)

    (false) Whether the param is optional.

  • required (Boolean) (defaults to: false)

    (false) Whether the param is required.

Yields:

  • block for defining value type

Yield Parameters:

Raises:



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/apiwork/contract/object.rb', line 282

def record(
  name,
  as: nil,
  default: UNSET,
  deprecated: false,
  description: nil,
  nullable: false,
  optional: false,
  required: false,
  &block
)
  raise ConfigurationError, 'record requires a block' unless block

  element = Element.new(@contract_class)
  block.arity.positive? ? yield(element) : element.instance_eval(&block)
  element.validate!

  param(
    name,
    as:,
    default:,
    deprecated:,
    description:,
    nullable:,
    optional:,
    required:,
    of: element,
    type: :record,
  )
end

#transform(data) ⇒ Object



329
330
331
# File 'lib/apiwork/contract/object.rb', line 329

def transform(data)
  Transformer.transform(self, data)
end

#validate(data, current_depth: 0, max_depth: 10, path: []) ⇒ Object



317
318
319
# File 'lib/apiwork/contract/object.rb', line 317

def validate(data, current_depth: 0, max_depth: 10, path: [])
  Validator.validate(self, data, current_depth:, max_depth:, path:)
end

#wrapped?Boolean

Returns:

  • (Boolean)


313
314
315
# File 'lib/apiwork/contract/object.rb', line 313

def wrapped?
  @wrapped
end