How to validate inputs and outputs with schemas
This guide explains how to implement data validation in Lemline workflows using JSON Schema. You'll learn how to validate workflow inputs, task inputs and outputs, and exported data to ensure data quality and prevent errors.
Run the Examples
Each example below is a complete workflow. Complete the SQLite setup and keep its worker running. Run the commands from a second terminal in the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. A workflow-level schema is checked by instance start, so an invalid input is refused on the command line; a task-level schema is checked when the task runs, so the instance faults with a validation error. Some examples call the tutorial shop; start it from the root of the Lemline source checkout:
docker compose -f examples/tutorial-services/compose.yaml up -d
Understanding Schema Validation in Lemline
Schema validation allows you to define the expected structure, types, and constraints for your data. Lemline uses JSON Schema 2020-12 for validation at various points in the workflow:
Workflow Input: Validating data passed to the workflow
Task Input: Validating data before task execution
Task Output: Validating data produced by a task
Export Data: Validating data promoted to the global context
By defining schemas, you can catch data issues early, before they cause problems downstream, making your workflows more robust and reliable.
Basic Schema Structure
A JSON Schema defines the structure and constraints for JSON data:
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"format": "email"
},
"age": {
"type": "integer",
"minimum": 18
}
}
}
This schema validates an object with:
Required name and email properties
name must be a non-empty string
email must be a valid email address
Optional age must be an integer of at least 18 if present
To validate data passed to your workflow, define an input schema:
document:
dsl: '1.0.3'
namespace: examples
name: customer-onboarding
version: '1.0.0'
title: Customer Onboarding Workflow
input:
schema:
document:
type: object
required:
- name
- email
- address
properties:
name:
type: string
minLength: 1
email:
type: string
format: email
address:
type: object
required:
- street
- city
- zipCode
properties:
street:
type: string
city:
type: string
state:
type: string
zipCode:
type: string
pattern: ^\d{5}(-\d{4})?$
phoneNumber:
type: string
pattern: ^\+?[1-9]\d{1,14}$
do:
- acceptCustomer:
set:
accepted: ${ .email }
Save it as adding-an-input-schema.yaml:
bin/lemline definition post -f adding-an-input-schema.yaml
bin/lemline instance start examples customer-onboarding --input '{"name":"Ada Lovelace","email":"ada@example.com","address":{"street":"12 Main St","city":"Springfield","zipCode":"12345"}}'
bin/lemline instance start examples customer-onboarding --input '{"name":"Ada Lovelace","email":"ada-at-example","address":{"street":"12 Main St","city":"Springfield","zipCode":"12345"}}'
The first instance completes with {"accepted":"ada@example.com"}. The second is refused: ada-at-example is not a valid email. Refused inputs never become instances: the CLI prints Input validation failed against workflow schema and the rule that failed.
Providing Default Values
You can specify default values for optional fields:
document:
dsl: '1.0.3'
namespace: examples
name: default-workflow-input
version: '1.0.0'
input:
from: '${ { priority: "NORMAL", currency: "USD" } + . }'
do:
- recordOrder:
set:
orderId: ${ .orderId }
priority: ${ .priority }
currency: ${ .currency }
Save it as default-workflow-input.yaml:
bin/lemline definition post -f default-workflow-input.yaml
bin/lemline instance start examples default-workflow-input --input '{"orderId":"ORD-2"}'
bin/lemline instance start examples default-workflow-input --input '{"orderId":"ORD-3","currency":"EUR"}'
Expect {"orderId":"ORD-2","priority":"NORMAL","currency":"USD"}, then {"orderId":"ORD-3","priority":"NORMAL","currency":"EUR"}. The workflow-level input.from expression merges defaults with the input before the first task runs. Values supplied by the caller take precedence. $workflow.input still holds the input as the caller supplied it.
After validation, you can access the input data in your workflow:
document:
dsl: '1.0.3'
namespace: examples
name: accessing-validated-input
version: '1.0.0'
do:
- ProcessCustomer:
set:
customer:
name: ${ $workflow.input.name }
email: ${ $workflow.input.email }
address: ${ $workflow.input.address }
phone: ${ $workflow.input.phoneNumber // "Not provided" }
preferredContact: ${ $workflow.input.preferredContact }
Save it as accessing-validated-input.yaml:
bin/lemline definition post -f accessing-validated-input.yaml
bin/lemline instance start examples accessing-validated-input --input '{"name":"Ada Lovelace","email":"ada@example.com","address":{"city":"Springfield"},"preferredContact":"email"}'
Expect the customer with "phone":"Not provided", the fallback for the missing phone number.
To validate data before a task executes:
document:
dsl: '1.0.3'
namespace: examples
name: validating-task-input
version: '1.0.0'
do:
- requestPayment:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/audit
body:
reference: ${ .paymentMethod.token }
message: '${ "Payment of \(.amount) \(.currency) requested" }'
input:
schema:
document:
type: object
required:
- amount
- currency
- paymentMethod
properties:
amount:
type: number
exclusiveMinimum: 0
currency:
type: string
enum:
- USD
- EUR
- GBP
paymentMethod:
type: object
required:
- type
- token
properties:
type:
type: string
enum:
- CREDIT_CARD
- PAYPAL
- BANK_TRANSFER
token:
type: string
Save it as validating-task-input.yaml:
bin/lemline definition post -f validating-task-input.yaml
bin/lemline instance start examples validating-task-input --input '{"amount":25,"currency":"EUR","paymentMethod":{"type":"CREDIT_CARD","token":"tok-1"}}'
bin/lemline instance start examples validating-task-input --input '{"amount":0,"currency":"EUR","paymentMethod":{"type":"CREDIT_CARD","token":"tok-2"}}'
The first instance records Payment of 25 EUR requested in the shop's audit list. The second faults with a validation error before the call: the amount must be greater than 0, and the shop receives nothing for tok-2.
If the input doesn't match the schema, a validation error is raised before the task executes.
You can combine transformation with validation:
document:
dsl: '1.0.3'
namespace: examples
name: input-transformation-and-validation
version: '1.0.0'
do:
- FormatUserData:
set:
userProfile: ${ .formattedUser }
isAdult: ${ .formattedUser.age >= 18 }
input:
schema:
document:
type: object
required:
- userId
- firstName
- lastName
- email
properties:
userId:
type: string
firstName:
type: string
minLength: 1
lastName:
type: string
minLength: 1
email:
type: string
format: email
age:
type: integer
minimum: 13
from:
formattedUser:
id: ${ .userId }
fullName: ${ .firstName + " " + .lastName }
email: ${ .email | ascii_downcase }
age: ${ .age }
Save it as input-transformation-and-validation.yaml:
bin/lemline definition post -f input-transformation-and-validation.yaml
bin/lemline instance start examples input-transformation-and-validation --input '{"userId":"C-100","firstName":"Ada","lastName":"Lovelace","email":"Ada@Example.com","age":36}'
bin/lemline instance start examples input-transformation-and-validation --input '{"userId":"C-100","firstName":"","lastName":"Lovelace","email":"ada@example.com"}'
The schema checks the raw input; input.from then reshapes it. The first instance completes with {"userProfile":{"id":"C-100","fullName":"Ada Lovelace","email":"ada@example.com","age":36},"isAdult":true}. The second faults with a validation error: firstName is empty.
This example validates the input, then transforms it: input.schema is checked against the data the task receives, before input.from is applied.
Task Output Validation
Validating Task Output
To validate data produced by a task:
document:
dsl: '1.0.3'
namespace: examples
name: validating-task-output
version: '1.0.0'
do:
- GenerateOrderSummary:
set:
orderSummary:
id: ${ .orderId }
customer:
id: ${ .customerId }
name: ${ .customerName }
items: '${ .items | map({ id: .productId, name: .productName, quantity: .quantity, price: .price
}) }'
subtotal: ${ .items | map(.price * .quantity) | add }
tax: ${ .items | map(.price * .quantity) | add * 0.08 }
total: ${ .items | map(.price * .quantity) | add * 1.08 }
output:
schema:
document:
type: object
required:
- orderSummary
properties:
orderSummary:
type: object
required:
- id
- customer
- items
- subtotal
- tax
- total
properties:
id:
type: string
customer:
type: object
required:
- id
- name
items:
type: array
minItems: 1
items:
type: object
required:
- id
- name
- quantity
- price
subtotal:
type: number
minimum: 0
tax:
type: number
minimum: 0
total:
type: number
minimum: 0
Save it as validating-task-output.yaml:
bin/lemline definition post -f validating-task-output.yaml
bin/lemline instance start examples validating-task-output --input '{"orderId":"ORD-1","customerId":"C-100","customerName":"Ada Lovelace","items":[{"productId":"SKU-1","productName":"Garden hose","quantity":2,"price":25}]}'
bin/lemline instance start examples validating-task-output --input '{"orderId":"ORD-2","customerId":"C-100","customerName":"Ada Lovelace","items":[{"productId":"SKU-1","productName":"Garden hose","quantity":2,"price":-25}]}'
The first instance returns the summary with subtotal 50, tax 4 and total 54. In the second, the price is negative: the computed subtotal breaks minimum: 0 and the task faults with a validation error.
If the output doesn't match the schema, a validation error is raised after the task executes.
Combine transformation with validation:
document:
dsl: '1.0.3'
namespace: examples
name: output-transformation-and-validation
version: '1.0.0'
do:
- findProducts:
call: http
with:
method: get
endpoint: http://127.0.0.1:8787/products
query:
category: ${ .category }
output:
as: '${ if (.items | length) > 0 then { status: "SUCCESS", data: { items: [.items[].id] } } else { status: "ERROR", error: { message: "No products in this category" } } end }'
schema:
document:
type: object
required:
- status
properties:
status:
type: string
enum:
- SUCCESS
- ERROR
data:
type: object
if:
properties:
status:
const: SUCCESS
required:
- status
then:
required:
- data
error:
type: object
if:
properties:
status:
const: ERROR
required:
- status
then:
required:
- error
Save it as output-transformation-and-validation.yaml:
bin/lemline definition post -f output-transformation-and-validation.yaml
bin/lemline instance start examples output-transformation-and-validation --input '{"category":"garden"}'
bin/lemline instance start examples output-transformation-and-validation --input '{"category":"toys"}'
Expect {"status":"SUCCESS","data":{"items":["SKU-1","SKU-2","SKU-3"]}}, then {"status":"ERROR","error":{"message":"No products in this category"}}. Both outputs satisfy the schema.
This transforms the API response into a standardized format and validates the structure.
Validating Exported Data
Export Schema Validation
To validate data being exported to the global context:
document:
dsl: '1.0.3'
namespace: examples
name: export-schema-validation
version: '1.0.0'
do:
- fetchCustomerData:
call: http
with:
method: get
endpoint: ${ "http://127.0.0.1:8787/customers/" + .customerId }
export:
as:
customer: ${ . }
schema:
document:
type: object
required:
- customer
properties:
customer:
type: object
required:
- id
- name
- email
- tier
properties:
id:
type: string
name:
type: string
email:
type: string
format: email
tier:
type: string
enum:
- gold
- standard
- greetCustomer:
set:
greeting: ${ "Hello " + $context.customer.name }
Save it as export-schema-validation.yaml:
bin/lemline definition post -f export-schema-validation.yaml
bin/lemline instance start examples export-schema-validation --input '{"customerId":"C-100"}'
bin/lemline instance start examples export-schema-validation --input '{"customerId":"C-300"}'
C-100 passes and the next task greets Ada Lovelace. The shop stores C-300 with the email not-an-email: the export breaks format: email and the task faults with a validation error.
This ensures that only valid customer data is exported to the global context.
Advanced Schema Features
Complex Property Dependencies
You can define dependencies between properties:
document:
dsl: '1.0.3'
namespace: examples
name: schema-property-dependencies
version: '1.0.0'
input:
schema:
document:
type: object
properties:
shippingMethod:
type: string
enum:
- STANDARD
- EXPRESS
- SAME_DAY
deliveryDate:
type: string
format: date
dependentSchemas:
shippingMethod:
oneOf:
- properties:
shippingMethod:
enum:
- SAME_DAY
deliveryDate:
type: string
required:
- deliveryDate
- properties:
shippingMethod:
enum:
- EXPRESS
- STANDARD
do:
- example:
set:
ready: true
Save it as schema-property-dependencies.yaml:
bin/lemline definition post -f schema-property-dependencies.yaml
bin/lemline instance start examples schema-property-dependencies --input '{"shippingMethod":"SAME_DAY","deliveryDate":"2026-12-01"}'
bin/lemline instance start examples schema-property-dependencies --input '{"shippingMethod":"EXPRESS"}'
bin/lemline instance start examples schema-property-dependencies --input '{"shippingMethod":"SAME_DAY"}'
The first two start. The third is refused: a same-day delivery needs a deliveryDate. Refused inputs never become instances: the CLI prints Input validation failed against workflow schema and the rule that failed.
This schema requires deliveryDate only when shippingMethod is "SAME_DAY".
Conditional Schemas with if/then/else
You can apply different validations based on conditions:
document:
dsl: '1.0.3'
namespace: examples
name: schema-conditional-validation
version: '1.0.0'
input:
schema:
document:
type: object
properties:
paymentMethod:
type: string
enum:
- CREDIT_CARD
- BANK_TRANSFER
- PAYPAL
cardNumber:
type: string
expiryDate:
type: string
accountNumber:
type: string
routingNumber:
type: string
paypalEmail:
type: string
required:
- paymentMethod
if:
properties:
paymentMethod:
enum:
- CREDIT_CARD
required:
- paymentMethod
then:
required:
- cardNumber
- expiryDate
else:
if:
properties:
paymentMethod:
enum:
- BANK_TRANSFER
required:
- paymentMethod
then:
required:
- accountNumber
- routingNumber
else:
required:
- paypalEmail
do:
- example:
set:
ready: true
Save it as schema-conditional-validation.yaml:
bin/lemline definition post -f schema-conditional-validation.yaml
bin/lemline instance start examples schema-conditional-validation --input '{"paymentMethod":"CREDIT_CARD","cardNumber":"4111111111111111","expiryDate":"12/30"}'
bin/lemline instance start examples schema-conditional-validation --input '{"paymentMethod":"CREDIT_CARD"}'
bin/lemline instance start examples schema-conditional-validation --input '{"paymentMethod":"PAYPAL"}'
The first starts. The second is refused for the missing cardNumber, the third for the missing paypalEmail.
This schema requires different fields based on the payment method selected.
Pattern Properties
You can validate properties that match a pattern:
document:
dsl: '1.0.3'
namespace: examples
name: schema-pattern-properties
version: '1.0.0'
input:
schema:
document:
type: object
properties:
name:
type: string
patternProperties:
^custom_:
type: string
additionalProperties: false
do:
- example:
set:
ready: true
Save it as schema-pattern-properties.yaml:
bin/lemline definition post -f schema-pattern-properties.yaml
bin/lemline instance start examples schema-pattern-properties --input '{"name":"Garden hose","custom_color":"green"}'
bin/lemline instance start examples schema-pattern-properties --input '{"name":"Garden hose","color":"green"}'
The first starts. The second is refused: color does not start with custom_ and additional properties are not allowed.
This schema allows any property that starts with "custom_" (and requires it to be a string), plus the "name" property, but no others.
JSON Schema supports several built-in formats for string validation:
document:
dsl: '1.0.3'
namespace: examples
name: schema-string-formats
version: '1.0.0'
input:
schema:
document:
type: object
properties:
email:
type: string
format: email
website:
type: string
format: uri
date:
type: string
format: date
time:
type: string
format: time
dateTime:
type: string
format: date-time
ipAddress:
type: string
format: ipv4
uuid:
type: string
format: uuid
do:
- example:
set:
ready: true
Save it as schema-string-formats.yaml:
bin/lemline definition post -f schema-string-formats.yaml
bin/lemline instance start examples schema-string-formats --input '{"email":"ada@example.com","website":"https://shop.example","date":"2026-12-01","dateTime":"2026-12-01T10:00:00Z","ipAddress":"127.0.0.1"}'
bin/lemline instance start examples schema-string-formats --input '{"date":"2026-13-45"}'
bin/lemline instance start examples schema-string-formats --input '{"email":"not-an-email"}'
Lemline checks formats: the first starts, the second is refused as not a valid RFC 3339 full-date, the third as not a valid RFC 5321 mailbox. uri, time, ipv4 and uuid are checked the same way.
Array Validation
For validating arrays and their contents:
document:
dsl: '1.0.3'
namespace: examples
name: schema-array-validation
version: '1.0.0'
input:
schema:
document:
type: object
properties:
tags:
type: array
minItems: 1
maxItems: 5
uniqueItems: true
items:
type: string
minLength: 2
matrix:
type: array
items:
type: array
items:
type: number
coordinates:
type: array
items:
type: number
minItems: 2
maxItems: 3
do:
- example:
set:
ready: true
Save it as schema-array-validation.yaml:
bin/lemline definition post -f schema-array-validation.yaml
bin/lemline instance start examples schema-array-validation --input '{"tags":["garden","sale"],"coordinates":[45.7,4.8]}'
bin/lemline instance start examples schema-array-validation --input '{"tags":["sale","sale"]}'
bin/lemline instance start examples schema-array-validation --input '{"coordinates":[45.7]}'
The first starts. The second is refused because the tags are not unique, the third because a coordinate needs at least two numbers.
Content Media Type
You can specify the expected media type for string content:
document:
dsl: '1.0.3'
namespace: examples
name: schema-media-type
version: '1.0.0'
input:
schema:
document:
type: object
properties:
htmlContent:
type: string
contentMediaType: text/html
csvData:
type: string
contentMediaType: text/csv
jsonData:
type: string
contentMediaType: application/json
do:
- example:
set:
ready: true
Save it as schema-media-type.yaml:
bin/lemline definition post -f schema-media-type.yaml
bin/lemline instance start examples schema-media-type --input '{"jsonData":"{\"sku\":\"SKU-1\"}"}'
bin/lemline instance start examples schema-media-type --input '{"jsonData":"not json"}'
Both start. contentMediaType is an annotation in JSON Schema 2020-12: Lemline does not check that the string holds JSON, HTML or CSV. Parse it in an expression, for example with fromjson, if the workflow needs it.
Example: Order Processing
The input schema requires an order ID and a nonnegative total. The workflow validates the input before choosing a processing path.
document:
dsl: '1.0.3'
namespace: examples
name: validate-and-route-order
version: '1.0.0'
input:
schema:
format: json
document:
type: object
required: [orderId, total]
properties:
orderId:
type: string
total:
type: number
minimum: 0
do:
- routeOrder:
switch:
- largeOrder:
when: ${ .total >= 1000 }
then: requestReview
- automatic:
then: acceptOrder
- requestReview:
set:
status: needs-review
then: exit
- acceptOrder:
set:
status: accepted
output:
as: '${ { orderId: $workflow.input.orderId, status: .status } }'
Save it as validate-and-route-order.yaml:
bin/lemline definition post -f validate-and-route-order.yaml
bin/lemline instance start examples validate-and-route-order --input '{"orderId":"ORD-301","total":250}'
bin/lemline instance start examples validate-and-route-order --input '{"orderId":"ORD-303","total":-5}'
The first completes with {"orderId":"ORD-301","status":"accepted"}. The second is refused: the total must be at least 0.
Schema Reuse and References
For complex schemas that are used in multiple places, you can use references:
document:
dsl: '1.0.3'
namespace: examples
name: schema-with-address-definition
version: '1.0.0'
input:
schema:
document:
type: object
required: [billing, shipping]
properties:
billing:
$ref: '#/$defs/address'
shipping:
$ref: '#/$defs/address'
$defs:
address:
type: object
required: [street, postalCode]
properties:
street:
type: string
postalCode:
type: string
do:
- acceptOrder:
set:
valid: true
Save it as schema-with-address-definition.yaml:
bin/lemline definition post -f schema-with-address-definition.yaml
bin/lemline instance start examples schema-with-address-definition --input '{"billing":{"street":"1 Main St","postalCode":"69001"},"shipping":{"street":"2 Side St","postalCode":"69002"}}'
bin/lemline instance start examples schema-with-address-definition --input '{"billing":{"street":"1 Main St"},"shipping":{"street":"2 Side St","postalCode":"69002"}}'
The first completes with {"valid":true}. The second is refused: the billing address has no postalCode.
Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
Best Practices for Schema Validation
Validate Early: Add schema validation as early as possible in the workflow
Be Specific: Create detailed, specific schemas that precisely define your data requirements
Default Values: Provide defaults for optional fields when appropriate
Schema Reuse: Use schema references to avoid duplication
Document Schemas: Add comments to explain complex validation requirements
Incremental Validation: Validate at multiple points to catch issues early
Performance Consideration: Balance validation thoroughness with performance needs
Version Schemas: Include version information for evolving schemas
Error Messages: Configure clear, helpful validation error messages
Test Edge Cases: Test your schemas with both valid and invalid data
Troubleshooting Schema Validation
Common Validation Errors
The error names the rule that failed. These messages come from the examples above:
Problem | Message |
|---|
Missing required property | required property 'postalCode' not found
|
Wrong type | integer found, string expected
|
Too few array items | must have at least 1 items but found 0
|
Duplicate array items | must have only unique items in the array
|
Invalid format | does not match the date pattern must be a valid RFC 3339 full-date
|
Property not allowed | property 'color' is not defined in the schema and the schema does not allow additional properties
|
No matching oneOf branch | must be valid to one and only one schema, but 0 are valid
|
For a workflow-level schema, instance start prints the message after Input validation failed against workflow schema. For a task-level schema, the instance faults with a validation error that carries it.
Debugging Validation Issues
When troubleshooting validation problems:
Add Diagnostic Tasks: Insert temporary tasks to inspect data before validation
Simplify Schemas: Temporarily simplify complex schemas to isolate issues
Check Data Types: Verify that data types match schema requirements
Inspect Transformations: Ensure transformations produce data matching the schema
Validate Externally: Use online JSON Schema validators to test schemas independently
25 November 2026