Class: Apiwork::Contract::Object
- 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.
Defined Under Namespace
Classes: Coercer, Deserializer, Transformer, Validator
Instance Attribute Summary collapse
- #action_name ⇒ Object readonly
- #contract_class ⇒ Object readonly
Attributes inherited from Object
Instance Method Summary collapse
-
#array(name, as: nil, default: UNSET, deprecated: false, description: nil, max: nil, min: nil, nullable: false, optional: false, required: false) {|element| ... } ⇒ void
Defines an array param with element type.
- #coerce(data) ⇒ Object
- #copy_type_definition_params(type_definition, target_param) ⇒ Object
- #deserialize(data) ⇒ Object
-
#initialize(contract_class, action_name: nil, visited_types: nil, wrapped: false) ⇒ Object
constructor
A new instance of Object.
-
#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
Defines a param with explicit type.
- #params ⇒ Object
-
#record(name, as: nil, default: UNSET, deprecated: false, description: nil, nullable: false, optional: false, required: false) {|element| ... } ⇒ void
Defines a record param with value type.
- #transform(data) ⇒ Object
- #validate(data, current_depth: 0, max_depth: 10, path: []) ⇒ Object
- #wrapped? ⇒ Boolean
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_name ⇒ Object (readonly)
25 26 27 |
# File 'lib/apiwork/contract/object.rb', line 25 def action_name @action_name end |
#contract_class ⇒ Object (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.
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, | nested_shape = [:shape] if [:type] == :array && nested_shape.is_a?(API::Object) target_param.param(param_name, **.except(:name)) elsif [:type] == :array target_param.param(param_name, **.except(:name, :shape, :discriminator)) elsif nested_shape.is_a?(API::Object) copy_nested_object_param(target_param, param_name, , nested_shape) elsif nested_shape.is_a?(API::Union) copy_nested_union_param(target_param, param_name, , nested_shape) else target_param.param(param_name, **.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.
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 ) = { 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 |
#params ⇒ Object
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? = @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, | [name] ||= end end 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.
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
313 314 315 |
# File 'lib/apiwork/contract/object.rb', line 313 def wrapped? @wrapped end |