|
| 1 | +--- |
| 2 | +title: Dataset validation |
| 3 | +description: Specify the dataset schema within the Actors so you can add monitoring and validation at the field level. |
| 4 | +slug: /actors/development/actor-definition/dataset-schema/validation |
| 5 | +--- |
| 6 | + |
| 7 | +**Specify the dataset schema within the Actors so you can add monitoring and validation at the field level.** |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +To define a schema for a default dataset of an Actor run, you need to set `fields` property in the dataset schema. |
| 12 | + |
| 13 | +:::info |
| 14 | + |
| 15 | +The schema defines a single item in the dataset. Be careful not to define the schema as an array, it always needs to be a schema of an object. |
| 16 | + |
| 17 | +Schema configuration is not available for named datasets or dataset views. |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +You can either do that directly through `actor.json`: |
| 22 | + |
| 23 | +```json title=".actor.json" |
| 24 | +{ |
| 25 | + "actorSpecification": 1, |
| 26 | + "storages": { |
| 27 | + "dataset": { |
| 28 | + "actorSpecification": 1, |
| 29 | + "fields": { |
| 30 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 31 | + "type": "object", |
| 32 | + "properties": { |
| 33 | + "name": { |
| 34 | + "type": "string" |
| 35 | + } |
| 36 | + }, |
| 37 | + "required": ["name"] |
| 38 | + }, |
| 39 | + "views": {} |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Or in a separate file linked from the `.actor.json`: |
| 46 | + |
| 47 | +```json title=".actor.json" |
| 48 | +{ |
| 49 | + "actorSpecification": 1, |
| 50 | + "storages": { |
| 51 | + "dataset": "./dataset_schema.json" |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +```json title="dataset_schema.json" |
| 57 | +{ |
| 58 | + "actorSpecification": 1, |
| 59 | + "fields": { |
| 60 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 61 | + "type": "object", |
| 62 | + "properties": { |
| 63 | + "name": { |
| 64 | + "type": "string" |
| 65 | + } |
| 66 | + }, |
| 67 | + "required": ["name"] |
| 68 | + }, |
| 69 | + "views": {} |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +:::important |
| 74 | + |
| 75 | +Dataset schema needs to be a valid JSON schema draft-07, so the `$schema` line is important and must be exactly this value or it must be omitted: |
| 76 | + |
| 77 | +`"$schema": "http://json-schema.org/draft-07/schema#"` |
| 78 | + |
| 79 | +::: |
| 80 | + |
| 81 | +## Dataset validation |
| 82 | + |
| 83 | +When you define a schema of your default dataset, the schema is then always used when you insert data into the dataset to perform validation (we use [AJV](https://ajv.js.org/)). |
| 84 | + |
| 85 | +If the validation succeeds, nothing changes from the current behavior, data is stored and an empty response with status code `201` is returned. |
| 86 | + |
| 87 | +If the data you attempt to store in the dataset is _invalid_ (meaning any of the items received by the API fails validation), _the entire request will be discarded_, The API will return a response with status code `400` and the following JSON response: |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "error": { |
| 92 | + "type": "schema-validation-error", |
| 93 | + "message": "Schema validation failed", |
| 94 | + "data": { |
| 95 | + "invalidItems": [{ |
| 96 | + "itemPosition": "<array index in the received array of items>", |
| 97 | + "validationErrors": "<Complete list of AJV validation error objects>" |
| 98 | + }] |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +The type of the AJV validation error object is [here](https://github.com/ajv-validator/ajv/blob/master/lib/types/index.ts#L86). |
| 105 | + |
| 106 | +If you use the Apify JS client or Apify SDK and call `pushData` function you can access the validation errors in a `try catch` block like this: |
| 107 | + |
| 108 | +```javascript |
| 109 | +try { |
| 110 | + const response = await Actor.pushData(items); |
| 111 | +} catch (error) { |
| 112 | + if (!error.data?.invalidItems) throw error; |
| 113 | + error.data.invalidItems.forEach((item) => { |
| 114 | + const { itemPosition, validationErrors } = item; |
| 115 | + }); |
| 116 | +} |
| 117 | +``` |
| 118 | +
|
| 119 | +## Examples of common types of validation |
| 120 | +
|
| 121 | +Optional field (price is optional in this case): |
| 122 | +
|
| 123 | +```json |
| 124 | +{ |
| 125 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 126 | + "type": "object", |
| 127 | + "properties": { |
| 128 | + "name": { |
| 129 | + "type": "string" |
| 130 | + }, |
| 131 | + "price": { |
| 132 | + "type": "number" |
| 133 | + } |
| 134 | + }, |
| 135 | + "required": ["name"] |
| 136 | +} |
| 137 | +``` |
| 138 | +
|
| 139 | +Field with multiple types: |
| 140 | +
|
| 141 | +```json |
| 142 | +{ |
| 143 | + "price": { |
| 144 | + "type": ["string", "number"] |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | +
|
| 149 | +Field with type `any`: |
| 150 | +
|
| 151 | +```json |
| 152 | +{ |
| 153 | + "price": { |
| 154 | + "type": ["string", "number", "object", "array", "boolean"] |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | +
|
| 159 | +Enabling fields to be `null` : |
| 160 | +
|
| 161 | +```json |
| 162 | +{ |
| 163 | + "name": { |
| 164 | + "type": "string", |
| 165 | + "nullable": true |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | +
|
| 170 | +Define type of objects in array: |
| 171 | +
|
| 172 | +```json |
| 173 | +{ |
| 174 | + "comments": { |
| 175 | + "type": "array", |
| 176 | + "items": { |
| 177 | + "type": "object", |
| 178 | + "properties": { |
| 179 | + "author_name": { |
| 180 | + "type": "string" |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | +``` |
| 187 | +
|
| 188 | +Define specific fields, but allow anything else to be added to the item: |
| 189 | +
|
| 190 | +```json |
| 191 | +{ |
| 192 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 193 | + "type": "object", |
| 194 | + "properties": { |
| 195 | + "name": { |
| 196 | + "type": "string" |
| 197 | + } |
| 198 | + }, |
| 199 | + "additionalProperties": true |
| 200 | +} |
| 201 | +``` |
| 202 | +
|
| 203 | +See [json schema reference](https://json-schema.org/understanding-json-schema/reference) for additional options. |
| 204 | +
|
| 205 | +You can also use [conversion tools](https://www.liquid-technologies.com/online-json-to-schema-converter) to convert an existing JSON document into it's JSON schema. |
| 206 | +
|
| 207 | +## Dataset field statistics |
| 208 | +
|
| 209 | +When you configure the dataset fields schema, we generate a field list and measure the following statistics: |
| 210 | +
|
| 211 | +- **Null count:** how many items in the dataset have the field set to null |
| 212 | +- **Empty count:** how many items in the dataset are `undefined` , meaning that for example empty string is not considered empty |
| 213 | +- **Minimum and maximum** |
| 214 | + - For numbers, this is calculated directly |
| 215 | + - For strings, this field tracks string length |
| 216 | + - For arrays, this field tracks the number of items in the array |
| 217 | + - For objects, this tracks the number of keys |
| 218 | + - For booleans, this tracks whether the boolean was set to true. Minimum is always 0, but maximum can be either 1 or 0 based on whether at least one item in the dataset has the boolean field set to true. |
| 219 | +
|
| 220 | +
|
| 221 | +You can use them in [monitoring](../../../../monitoring#alert-configuration). |
| 222 | +
|
0 commit comments