← Blog

Workflow Job Schema

Forms Bridge utilizes JSON Schemas to describe settings and perform data validations.

An example of this use is the Workflow Jobs registration. This is the schema of a workflow job setting data:

{
    "title": {
        "type": "string"
    },
    "description": {
        "type": "string"
    },
    "method": {
        "type": "string"
    },
    "input": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "required": {
                    "type": "boolean"
                },
                "schema": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "string",
                                "integer",
                                "number",
                                "array",
                                "object",
                                "boolean",
                                "null"
                            ]
                        },
                        "items": {
                            "type": [
                                "array",
                                "object"
                            ],
                            "additionalProperties": true,
                            "additionalItems": true
                        },
                        "properties": {
                            "type": "object",
                            "additionalProperties": true
                        },
                        "maxItems": {
                            "type": "integer"
                        },
                        "minItems": {
                            "type": "integer"
                        },
                        "additionalProperties": {
                            "type": "boolean"
                        },
                        "additionalItems": {
                            "type": "boolean"
                        },
                        "required": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            },
                            "additionalItems": true
                        }
                    },
                    "required": [
                        "type"
                    ],
                    "additionalProperties": false
                }
            },
            "required": [
                "name",
                "schema"
            ],
            "additionalProperties": false
        }
    },
    "output": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "touch": {
                    "type": "boolean"
                },
                "forward": {
                    "type": "boolean"
                },
                "schema": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "string",
                                "integer",
                                "number",
                                "array",
                                "object",
                                "boolean",
                                "null"
                            ]
                        },
                        "items": {
                            "type": [
                                "array",
                                "object"
                            ],
                            "additionalItems": true
                        },
                        "properties": {
                            "type": "object",
                            "additionalProperties": true
                        },
                        "maxItems": {
                            "type": "integer"
                        },
                        "minItems": {
                            "type": "integer"
                        },
                        "additionalProperties": {
                            "type": "boolean"
                        },
                        "additionalItems": {
                            "type": "boolean"
                        }
                    },
                    "required": [
                        "type"
                    ],
                    "additionalProperties": false
                }
            },
            "required": [
                "name",
                "schema"
            ],
            "additionalProperties": false
        }
    }
}

And this is an example of a valid workflow job setting data:

{
    "title": "Contact ID",
    "description": "Creates a new contact and sets its ID as the contactId field of the payload",
    "method": "forms_bridge_holded_contact_id",
    "input": [
        {
            "name": "name",
            "schema": {
                "type": "string"
            },
            "required": true
        },
        {
            "name": "tradeName",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "email",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "mobile",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "phone",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "type",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "code",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "vatnumber",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "iban",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "swift",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "billAddress",
            "schema": {
                "type": "object",
                "properties": {
                    "address": {
                        "type": "string"
                    },
                    "postalCode": {
                        "type": "string"
                    },
                    "city": {
                        "type": "string"
                    },
                    "countryCode": {
                        "type": "string"
                    }
                },
                "additionalProperties": true
            }
        },
        {
            "name": "defaults",
            "schema": {
                "type": "object",
                "properties": {
                    "language": {
                        "type": "string"
                    }
                },
                "additionalProperties": true
            }
        },
        {
            "name": "tags",
            "schema": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "additionalItems": true
            }
        },
        {
            "name": "note",
            "schema": {
                "type": "string"
            }
        },
        {
            "name": "isperson",
            "schema": {
                "type": "integer"
            }
        },
        {
            "name": "contactPersons",
            "schema": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "email": {
                            "type": "string"
                        },
                        "phone": {
                            "type": "string"
                        }
                    },
                    "additionalProperties": false
                }
            }
        }
    ],
    "output": [
        {
            "name": "contactId",
            "schema": {
                "type": "string"
            }
        }
    ]
}

And this is the forms_bridge_holded_contact_id method definition:

function forms_bridge_holded_contact_id( $payload, $bridge ) {	
    $contact = array(
		'name' => $payload['name'],
	);

	$contact_fields = array(
		'tradeName',
		'email',
		'mobile',
		'phone',
		'type',
		'code',
		'vatnumber',
		'iban',
		'swift',
		'billAddress',
		'defaults',
		'tags',
		'note',
		'isperson',
		'contactPersons',
		'shippingAddresses',
		'CustomId',
	);

	foreach ( $contact_fields as $field ) {
		if ( isset( $payload[ $field ] ) ) {
			$contact[ $field ] = $payload[ $field ];
		}
	}

	$method   = 'POST';
	$endpoint = '/api/invoicing/v1/contacts';

	if ( $update && isset( $payload['id'] ) ) {
		$method   = 'PUT';
		$endpoint = $endpoint . '/' . $payload['id'];
	}

	$response = $bridge
		->patch(
			array(
				'name'     => 'holded-create-contact',
				'endpoint' => $endpoint,
				'method'   => $method,
			)
		)
		->submit( $contact );

	if ( is_wp_error( $response ) ) {
		return $response;
	}

	$contact_id = $response['data']['id'];

	$response = $bridge
		->patch(
			array(
				'name'     => 'holded-get-contact-by-id',
				'endpoint' => '/api/invoicing/v1/contacts/' . $contact_id,
				'method'   => 'GET',
			)
		)
		->submit();

	if ( is_wp_error( $response ) ) {
		return $response;
	}

	$contact = $response['data'];

	if ( is_wp_error( $contact ) ) {
		return $contact;
	}

	$payload['contactId'] = $contact['id'];
	return $payload;
}

To validate this settings on registration time Forms Bridge utilizes the JSON Schema validation API included with the REST API package of WordPress.

In addition to the validation of the schema, Forms Bridge checks that the method value exists as a function on the global scope. If the workflow job does not pass the validation, it will be skipped from registration.