Skip to content

Commit e0aaef3

Browse files
mforimarcel-rbro
andauthored
docs: Custom error messages in the input schema (#2121)
Document custom error messages in the input schema, introduced by apify/apify-shared-js#567 (based on the specification [here](apify/apify-shared-js#556)) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Documents custom validation error messages for input schema and links them from the spec and index. > > - **Docs**: > - Add new guide `input_schema/custom_error_messages.md` explaining `errorMessage` usage, supported validation keywords, nesting, and best practices. > - Update `input_schema/specification.md` to include `errorMessage` in input field properties table with link to the new page. > - Update `input_schema/index.md` to reference the spec, secrets, and new custom error messages guide. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 19d0412. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Marcel Rebro <[email protected]>
1 parent 7284c51 commit e0aaef3

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Custom error messages
3+
description: Learn how to define custom error messages for input validation in your Actor's input schema. Make validation feedback clearer and more user-friendly.
4+
slug: /actors/development/actor-definition/input-schema/custom-error-messages
5+
---
6+
7+
**Learn how to define custom error messages for input validation in your Actor's input schema. Make validation feedback clearer and more user-friendly.**
8+
9+
---
10+
11+
When an input fails validation against an Actor's input schema, the resulting errors are processed and displayed to the user. By default, these messages are generic and may not clearly explain what the validation rule actually means.
12+
13+
Custom error messages allow Actor developers to define tailored feedback messages for input validation errors, making it easier for users to understand what is required and improving overall usability.
14+
15+
## The problem with generic error messages
16+
17+
Some validation rules have a specific purpose that generic error messages don't explain well. For example, consider the following input field using the `pattern` validation keyword:
18+
19+
```json
20+
{
21+
"title": "Email address",
22+
"type": "string",
23+
"description": "Your email address",
24+
"editor": "textfield",
25+
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
26+
}
27+
```
28+
29+
Input that doesn't satisfy the pattern will produce an error message like:
30+
31+
```text
32+
Field "email" should match pattern "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$".
33+
```
34+
35+
However, a message such as the following would be much more understandable for the user:
36+
37+
```text
38+
Field "email" must be a valid email address.
39+
```
40+
41+
## Custom error messages for input fields
42+
43+
Each property in the [input schema](./index.md) can include an `errorMessage` field that defines a custom error message to be displayed when validation of that field fails.
44+
45+
The `errorMessage` must be an object that maps *validation keywords* (e.g., `type`, `pattern`, `minLength`) to their respective custom messages.
46+
47+
```json title="Email input with custom error messages"
48+
{
49+
"title": "Email address",
50+
"type": "string",
51+
"description": "Your email address",
52+
"editor": "textfield",
53+
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
54+
"errorMessage": {
55+
"type": "Email must be a string",
56+
"pattern": "Email must be a valid email address"
57+
}
58+
}
59+
```
60+
61+
If a validation error occurs for a keyword that is not listed in the `errorMessage` object, the system will fall back to the default error message.
62+
63+
:::note User-friendly messages
64+
65+
Custom error messages are especially useful for complex validation rules like regular expressions, where the default error message would show the entire pattern, which is not user-friendly. Refer to the [best practices](#best-practices) for more guidance.
66+
67+
:::
68+
69+
### Supported validation keywords
70+
71+
You can define custom error messages for any validation keyword supported by the [input schema](./index.md), including:
72+
73+
| Type | Supported validation keywords |
74+
|--------------------|-----------------------------------------------------------------------------|
75+
| `string` | `type`, `pattern`, `minLength`, `maxLength`, `enum` |
76+
| `number`/`integer` | `type`, `minimum`, `maximum` |
77+
| `boolean` | `type` |
78+
| `array` | `type`, `minItems`, `maxItems`, `uniqueItems`, `patternKey`, `patternValue` |
79+
| `object` | `type`, `minProperties`, `maxProperties`, `patternKey`, `patternValue` |
80+
81+
#### Nested properties
82+
83+
It's possible to define custom error messages in sub-properties as well. For objects with nested properties, you can define error messages at any level of nesting:
84+
85+
```json
86+
{
87+
"title": "User",
88+
"type": "object",
89+
"description": "Provide user details",
90+
"editor": "schemaBased",
91+
"properties": {
92+
"email": {
93+
"type": "string",
94+
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
95+
"errorMessage": {
96+
"pattern": "Please enter a valid email address"
97+
}
98+
}
99+
}
100+
}
101+
```
102+
103+
## Best practices
104+
105+
Custom error messages can be useful in specific cases, but they aren't always necessary. In most situations, the default validation messages are clear enough and ensure consistency across the platform. Use custom messages only when they meaningfully improve clarity—for example, when the default message would expose an unreadable regular expression or fail to explain a non-obvious requirement.

sources/platform/actors/development/actor_definition/input_schema/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ The actual input object passed from the autogenerated input UI to the Actor then
7878
}
7979
```
8080

81-
Next, let's take a look at [input schema specification](./specification.md), and the possibility of using input schema to enable users to pass [secrets](./secret_input.md).
81+
Next, let's take a look at [input schema specification](./specification.md), the possibility of using input schema to enable users to pass [secrets](./secret_input.md), and how to define [custom error messages](./custom_error_messages.md) for input validation.

sources/platform/actors/development/actor_definition/input_schema/specification.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Each field of your input is described under its key in the `inputSchema.properti
130130
| `default` | Must match `type` property. | No | Default value that will be <br/>used when no value is provided. |
131131
| `prefill` | Must match `type` property. | No | Value that will be prefilled <br/>in the Actor input interface. |
132132
| `example` | Must match `type` property. | No | Sample value of this field <br/>for the Actor to be displayed when <br/>Actor is published in Apify Store. |
133+
| `errorMessage` | Object | No | Custom error messages for validation keywords. <br/>See [custom error messages](./custom_error_messages.md) <br/>for more details. |
133134
| `sectionCaption` | String | No | If this property is set, <br/>then all fields following this field <br/>(this field included) will be separated <br/>into a collapsible section <br/>with the value set as its caption. <br/>The section ends at the last field <br/>or the next field which has the <br/> `sectionCaption` property set. |
134135
| `sectionDescription` | String | No | If the `sectionCaption` property is set, <br/>then you can use this property to <br/>provide additional description to the section. <br/>The description will be visible right under <br/>the caption when the section is open. |
135136

0 commit comments

Comments
 (0)