# SDK Reference - object_definitions

Input and output fields in Workato are defined as arrays of hashes and can be used interchangeably. Object definitions are a way to store these arrays which correspond to definitions of a specific resource in a target API.

Quick Overview

Object definitions are a way to store these arrays which correspond to definitions of a specific resource in a target API. Use them to keep your code dry.

# Structure

    object_definitions: {

      [Unique_object_definition_name]: {
        fields: lambda do |connection, config_fields, object_definitions|
          Array
        end
      },

      [Another_unique_object_definition_name]: {
        ...
      }
    },

Attribute Description
Key fields
Type lambda function
Required True
Description This lambda function is invoked whenever its parent object_definition's key is called in an action or trigger. It is able to make HTTP requests to dynamically build schema from metadata endpoints. The output of this lambda function should be an array of hashes that represents the input or output fields. This is called Workato Schema. Find out more here
Possible Arguments connection - Hash representing user given inputs defined in connection
config_fields - Hash representing the user given inputs from config_fields in the action or trigger that referenced this object definition.
object_definitions - Allows you to reference other object_definitions.
Expected Output Array

TIP

Multiple object_definitions can be called in the same input_fields or output_fields lambda function and merged into a larger superset of fields to be displayed.

Example - fields:

Object_definitions can be static and simply store an array. When this object definition is referenced, the fields lambda function returns this array.

    object_definitions: {
      lead: {
        fields: lambda do
          [
            { name: "name", type: :boolean },
            { name: "email" },
            { name: "number"}
          ]
        end
      }
    }

Object_definitions can also be dynamic and make HTTP requests to metadata endpoints. When this object definition is referenced, the fields lambda function makes this request, receives the response and should massage the response into the same array that can be returned to the input_fields or output_fields lambda function that referenced it. Find out more about defining these input_fields and output_fields (called Workato schema) here

    object_definitions: {
      form: {
        fields: lambda do |connection|
          get("https://api.unbounce.com/pages/#{connection['page_id']}/form_fields")["formFields"].
            map { |field| { name: field["id"] } }
        end
      }
    }
```

Example - Building schema from multiple object_definitions

To keep your code DRY, our recommendation is to logically break up your schema definitions into separate object_definitions. These object_definitions may be dynamically generated separately and pieced together.

    object_definitions: {
      create_object_output: {
        fields: lambda do |connection, config_fields, object_definitions|
          if config_fields['object'] == 'customer'
            [
              {
                name: 'customer',
                type: 'object',
                properties: object_definitions['customer_schema']
              },
              {
                name: 'card',
                type: 'object',
                properties: object_definitions['card_schema']
              }
            ]
          elsif config_fields['object'] == 'subscription'
            [
              {
                name: 'customer',
                type: 'object',
                properties: object_definitions['customer_schema']
              },
              {
                name: 'subscription',
                type: 'object',
                properties: object_definitions['subscription_schema']
              },
              {
                name: 'card',
                type: 'object',
                properties: object_definitions['card_schema']
              }
            ]
          end
        end
      }
    }