Skip to content

Form submission structure

The Effect TS Schema determines what JSON is submitted to the GraphQL API.

Let’s take our basic form again:

import { NumberField, TextField } from "@pf/form-schema"
import { Schema } from "effect"
const formSchema = Schema.Struct({
name: TextField({
label: "Item name",
}),
quantity: NumberField({
label: "Quantity",
}),
})

This submits this JSON:

{
"name": "Office chair",
"quantity": 2
}

This becomes the initial process state if submitted as the first step of a process. If this form is a later step, and the process state already has JSON it would simply get merged.

Use a nested Schema.Struct to submit related fields together as an object. For example, add a deliveryAddress to the order form:

import { NumberField, TextField } from "@pf/form-schema"
import { Schema } from "effect"
const formSchema = Schema.Struct({
name: TextField({ label: "Item name" }),
quantity: NumberField({ label: "Quantity" }),
deliveryAddress: Schema.Struct({
street: TextField({ label: "Street address" }),
city: TextField({ label: "City" }),
postcode: TextField({ label: "Postcode" }),
}),
})

The address fields are submitted inside deliveryAddress:

{
"name": "Office chair",
"quantity": 2,
"deliveryAddress": {
"street": "10 Example Street",
"city": "Auckland",
"postcode": "1010"
}
}

Use ListField when someone can add or remove rows. This form collects one delivery address and a SKU and quantity for each order line:

import { ListField, NumberField, TextField } from "@pf/form-schema"
import { Schema } from "effect"
const formSchema = Schema.Struct({
deliveryAddress: Schema.Struct({
street: TextField({ label: "Street address" }),
city: TextField({ label: "City" }),
postcode: TextField({ label: "Postcode" }),
}),
orderLines: ListField(
{
sku: TextField({ label: "SKU" }),
quantity: NumberField({ label: "Quantity" }),
},
{ label: "Order lines", addButtonLabel: "Add order line" },
),
})

The address is submitted as a single object, and each row becomes an object in the orderLines array:

{
"deliveryAddress": {
"street": "10 Example Street",
"city": "Auckland",
"postcode": "1010"
},
"orderLines": [
{ "sku": "CHAIR-001", "quantity": 2 },
{ "sku": "DESK-002", "quantity": 1 }
]
}

So far any field we’ve added, changed the submission structure, and became a field itself. This is not always desirable, sometimes presentation structure is needed to display a form in a certain way that should not implement what is submitted. That is the subject of the next section.