Skip to content

Commit ce9ab03

Browse files
committed
default/prefill, validation
1 parent c89b7e2 commit ce9ab03

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed
27.3 KB
Loading

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

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Properties:
416416
#### Object fields validation
417417

418418
In the same way as in the root-level input schema, a schema can be defined for sub-properties of an object using the `properties` field.
419-
Each sub-property within this sub-schema can define the same fields as those available at the root level of the input schema, except for the fields that apply only at the root level: `sectionCaption`, `sectionDescription`, `prefill`, `example`, and `default`.
419+
Each sub-property within this sub-schema can define the same fields as those available at the root level of the input schema, except for the fields that apply only at the root level: `sectionCaption` and `sectionDescription`.
420420

421421
Validation is performed both in the UI and during actor execution via the API.
422422
Sub-schema validation works independently of the editor selected for the parent object. It also respects the `additionalProperties` and `required` fields, allowing precise control over whether properties not defined in `properties` are permitted and which properties are mandatory.
@@ -427,7 +427,6 @@ Object sub-properties can also define their own sub-schemas recursively, without
427427

428428
:::
429429

430-
431430
Example of an object property with sub-schema properties:
432431

433432
```json
@@ -472,6 +471,48 @@ In this example, the object has validation rules for its properties:
472471
- The `timeout` and `locale` properties are required
473472
- No additional properties beyond those defined are allowed
474473

474+
**Handling default and prefill values for object sub-properties**
475+
476+
When defining object with sub-properties, it's possible to set `default` and `prefill` values in two ways:
477+
1. **At the parent object level**: You can provide a complete object as the `default` or `prefill` value, which will set values for all sub-properties at once.
478+
2. **At the individual sub-property level**: You can specify `default` or `prefill` values for each sub-property separately within the `properties` definition.
479+
480+
When both methods are used, the values defined at the parent object level take precedence over those defined at the sub-property level.
481+
For example, in the input schema like this:
482+
483+
```json
484+
{
485+
"title": "Configuration",
486+
"type": "object",
487+
"description": "Advanced configuration options",
488+
"editor": "schemaBased",
489+
"default": {
490+
"timeout": 60
491+
},
492+
"properties": {
493+
"locale": {
494+
"title": "Locale",
495+
"type": "string",
496+
"description": "Locale identifier.",
497+
"pattern": "^[a-z]{2,3}-[A-Z]{2}$",
498+
"editor": "textfield",
499+
"default": "en-US"
500+
},
501+
"timeout": {
502+
"title": "Timeout",
503+
"type": "integer",
504+
"description": "Request timeout in seconds",
505+
"minimum": 1,
506+
"maximum": 300,
507+
"editor": "number",
508+
"default": 120
509+
}
510+
}
511+
}
512+
```
513+
514+
The `timeout` sub-property will have a default value of `60` (from the parent object), while the `locale` sub-property will have a default value of `"en-US"` (from its own definition).
515+
475516
#### `schemaBased` editor
476517

477518
Object with sub-schema defined can use the `schemaBased` editor, which provides a user-friendly interface for editing each property individually.
@@ -615,7 +656,9 @@ To correctly define options for multiselect, you need to define the `items` prop
615656
#### Array items validation
616657

617658
Arrays in the input schema can define an `items` field to specify the type and validation rules for each item.
618-
Each array item is validated according to its `type`. If the item is an `object`, it can define its own `properties`, `required`, and `additionalProperties` fields,
659+
Each array item is validated according to its `type` and inside the `items` field it's also possible to define additional validation rules such as `pattern`, `minimum`, `maximum`, etc., depending on the item type.
660+
661+
If the item type is an `object`, it can define its own `properties`, `required`, and `additionalProperties` fields,
619662
working in the same way as a single object field (see [Object fields validation](#object-fields-validation)).
620663

621664
Validation is performed both in the UI and during actor execution via the API.
@@ -664,6 +707,54 @@ In this example:
664707
- No additional properties beyond those defined are allowed.
665708
- The validation of each object item works the same as for a single object field (see [Object fields validation](#object-fields-validation)).
666709

710+
**Handling default and prefill values array with object sub-properties**
711+
712+
When defining an array of objects with sub-properties, it's possible to set `default` and `prefill` values in two ways:
713+
1. **At the parent array level**: You can provide an array of complete objects as the `default` or `prefill` value, which will be used only if there is no value specified for the field.
714+
2. **At the individual sub-property level**: You can specify `default` or `prefill` values for each sub-property within the `properties` definition of the object items. These values will be applied to each object in the array value.
715+
716+
For example, having an input schema like this:
717+
718+
```json
719+
{
720+
"title": "Requests",
721+
"type": "array",
722+
"description": "List of HTTP requests",
723+
"editor": "schemaBased",
724+
"default": [
725+
{ "url": "https://apify.com", "port": 80 }
726+
],
727+
"items": {
728+
"type": "object",
729+
"properties": {
730+
"url": {
731+
"title": "URL",
732+
"type": "string",
733+
"description": "Request URL",
734+
"editor": "textfield"
735+
},
736+
"port": {
737+
"title": "Port",
738+
"type": "integer",
739+
"description": "Request port",
740+
"editor": "number",
741+
"default": 8080
742+
}
743+
},
744+
"required": ["url", "port"],
745+
"additionalProperties": false
746+
}
747+
}
748+
```
749+
750+
If there is no value specified for the field, the array will default to containing one object:
751+
```json
752+
[
753+
{ "url": "https://apify.com", "port": 80 }
754+
]
755+
```
756+
However, if the user adds a new item to the array, the `port` sub-property of that new object will default to `8080`, as defined in the sub-property itself.
757+
667758
#### `schemaBased` editor
668759

669760
Arrays can use the `schemaBased` editor to provide a user-friendly interface for editing each item individually.
@@ -680,7 +771,8 @@ Example 1: Array of strings using the `schemaBased` editor:
680771
"description": "List of URLs for the scraper to visit",
681772
"editor": "schemaBased",
682773
"items": {
683-
"type": "string"
774+
"type": "string",
775+
"pattern": "^https?:\\/\\/(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(?:\\/\\S*)?$"
684776
},
685777
"minItems": 1,
686778
"maxItems": 50,

0 commit comments

Comments
 (0)