-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add dataset schema validation #1304
Changes from all commits
9ecd6ef
b96183f
c4a48ec
d1b2db6
087f164
2372e0b
b5b2032
f1d989a
73e0f6c
4bdf857
4844192
4aca8a8
7356b35
d484ad0
1adaf5b
89a4166
dc6329e
4fb5eaf
9c6c99c
0f9872b
12142db
df06398
6d26ea0
976dd4f
fbb6dcd
64b51e0
b07d03d
310d614
c518b6b
8e0de86
330dfe2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
type: object | ||
properties: | ||
error: | ||
type: object | ||
properties: | ||
type: | ||
type: string | ||
description: The type of the error. | ||
example: "schema-validation-error" | ||
message: | ||
type: string | ||
description: A human-readable message describing the error. | ||
example: "Schema validation failed" | ||
data: | ||
type: object | ||
properties: | ||
invalidItems: | ||
type: array | ||
description: A list of invalid items in the received array of items. | ||
items: | ||
type: object | ||
properties: | ||
itemPosition: | ||
type: number | ||
description: The position of the invalid item in the array. | ||
example: 2 | ||
validationErrors: | ||
type: array | ||
description: A complete list of AJV validation error objects for the invalid item. | ||
items: | ||
type: object | ||
properties: | ||
instancePath: | ||
type: string | ||
description: The path to the instance being validated. | ||
schemaPath: | ||
type: string | ||
description: The path to the schema that failed the validation. | ||
keyword: | ||
type: string | ||
description: The validation keyword that caused the error. | ||
message: | ||
type: string | ||
description: A message describing the validation error. | ||
params: | ||
type: object | ||
description: Additional parameters specific to the validation error. | ||
required: | ||
- invalidItems | ||
required: | ||
- type | ||
- message | ||
- data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
title: PutItemResponseError | ||
required: | ||
- error | ||
type: object | ||
properties: | ||
error: | ||
allOf: | ||
- $ref: ./DatasetSchemaValidationError.yaml | ||
- {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
--- | ||
title: Dataset validation | ||
description: Specify the dataset schema within the Actors so you can add monitoring and validation at the field level. | ||
slug: /actors/development/actor-definition/dataset-schema/validation | ||
--- | ||
|
||
**Specify the dataset schema within the Actors so you can add monitoring and validation at the field level.** | ||
|
||
--- | ||
|
||
To define a schema for a default dataset of an Actor run, you need to set `fields` property in the dataset schema. | ||
|
||
:::info | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gippy, does user get an error when this happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not actually 100% sure. I think you could theoretically set the top level type of the schema to array. Will test it tomorrow. If it's possible then we will try to add some check to build so that the creator cannot do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Y, let's throw an error as early as possible so ideally in the build. Later it's too late. |
||
|
||
Schema configuration is not available for named datasets or dataset views. | ||
|
||
::: | ||
|
||
You can either do that directly through `actor.json`: | ||
|
||
```json title=".actor.json" | ||
{ | ||
"actorSpecification": 1, | ||
"storages": { | ||
"dataset": { | ||
"actorSpecification": 1, | ||
"fields": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["name"] | ||
}, | ||
"views": {} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Or in a separate file linked from the `.actor.json`: | ||
|
||
```json title=".actor.json" | ||
{ | ||
"actorSpecification": 1, | ||
"storages": { | ||
"dataset": "./dataset_schema.json" | ||
} | ||
} | ||
``` | ||
|
||
```json title="dataset_schema.json" | ||
{ | ||
"actorSpecification": 1, | ||
"fields": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["name"] | ||
}, | ||
"views": {} | ||
} | ||
``` | ||
|
||
:::important | ||
|
||
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: | ||
|
||
`"$schema": "http://json-schema.org/draft-07/schema#"` | ||
|
||
::: | ||
|
||
## Dataset validation | ||
|
||
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/)). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing to consider - do we want to mention we currently use AJV? Perhaps AJV contains some JSON schema extensions, and if we replace them, we could change the expected behavior. Or is this not the thing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to mention it, because the creators can then look up the validation output and what it means. I do not think AJV contains any additions to JSON schema. |
||
|
||
If the validation succeeds, nothing changes from the current behavior, data is stored and an empty response with status code `201` is returned. | ||
|
||
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: | ||
|
||
```json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the API docs with this and link them to this documentation. It's important to have it there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
"error": { | ||
"type": "schema-validation-error", | ||
"message": "Schema validation failed", | ||
"data": { | ||
"invalidItems": [{ | ||
"itemPosition": "<array index in the received array of items>", | ||
"validationErrors": "<Complete list of AJV validation error objects>" | ||
}] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The type of the AJV validation error object is [here](https://github.com/ajv-validator/ajv/blob/master/lib/types/index.ts#L86). | ||
|
||
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: | ||
|
||
```javascript | ||
try { | ||
const response = await Actor.pushData(items); | ||
} catch (error) { | ||
if (!error.data?.invalidItems) throw error; | ||
error.data.invalidItems.forEach((item) => { | ||
const { itemPosition, validationErrors } = item; | ||
}); | ||
} | ||
``` | ||
Comment on lines
+106
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about Python Client & SDK? If we provide code sample for one, it would make sense to provide it for the other as well, and utilize Tabs component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems it does not at the moment, I am in contact w @vdusek about adding it |
||
|
||
## Examples of common types of validation | ||
|
||
Optional field (price is optional in this case): | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"price": { | ||
"type": "number" | ||
} | ||
}, | ||
"required": ["name"] | ||
} | ||
``` | ||
|
||
Field with multiple types: | ||
|
||
```json | ||
{ | ||
"price": { | ||
"type": ["string", "number"] | ||
} | ||
} | ||
``` | ||
|
||
Field with type `any`: | ||
|
||
```json | ||
{ | ||
"price": { | ||
"type": ["string", "number", "object", "array", "boolean"] | ||
} | ||
} | ||
``` | ||
|
||
Enabling fields to be `null` : | ||
|
||
```json | ||
{ | ||
"name": { | ||
"type": "string", | ||
"nullable": true | ||
} | ||
} | ||
``` | ||
|
||
Define type of objects in array: | ||
|
||
```json | ||
{ | ||
"comments": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"author_name": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Define specific fields, but allow anything else to be added to the item: | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": true | ||
} | ||
``` | ||
|
||
See [json schema reference](https://json-schema.org/understanding-json-schema/reference) for additional options. | ||
|
||
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. | ||
|
||
## Dataset field statistics | ||
|
||
When you configure the dataset fields schema, we generate a field list and measure the following statistics: | ||
|
||
- **Null count:** how many items in the dataset have the field set to null | ||
- **Empty count:** how many items in the dataset are `undefined` , meaning that for example empty string is not considered empty | ||
- **Minimum and maximum** | ||
- For numbers, this is calculated directly | ||
- For strings, this field tracks string length | ||
- For arrays, this field tracks the number of items in the array | ||
- For objects, this tracks the number of keys | ||
- 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. | ||
|
||
katacek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
You can use them in [monitoring](../../../../monitoring#alert-configuration). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,12 +41,22 @@ Currently, the monitoring option offers the following features: | |
|
||
### Alert configuration | ||
|
||
When you set up an alert, you have two choices for how you want the metrics to be evaluated. And depending on your choices, the alerting system will behave differently: | ||
When you set up an alert, you have four choices for how you want the metrics to be evaluated. And depending on your choices, the alerting system will behave differently: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as a part of the validation, new monitoring possibilities arised, I have added them here |
||
|
||
1. **Alert, when the metric is lower than** - This type of alert is checked after the run finishes. If the metric is lower than the value you set, the alert will be triggered and you will receive a notification. | ||
|
||
2. **Alert, when the metric is higher than** - This type of alert is checked both during the run and after the run finishes. During the run, we do periodic checks (approximately every 5 minutes) so that we can notify you as soon as possible if the metric is higher than the value you set. After the run finishes, we do a final check to make sure that the metric does not go over the limit in the last few minutes of the run. | ||
|
||
3. **Alert, when run status is one of following** - This type of alert is checked only after the run finishes. It makes possible to track the status of your finished runs and send an alert if the run finishes in a state you do not expect. If your Actor runs very often and suddenly starts failing, you will receive a single alert after the first failed run in 1 minute, and then aggregated alert every 15 minutes. | ||
|
||
4. **Alert for dataset field statistics** - If you have a [dataset schema](../actors/development/actor_definition/dataset_schema/validation.md) set up, then you can use the field statistics to set up an alert. You can use field statistics for example to track if some field is filled in in all records, if some numeric value is too low/high (for example when tracking the price of a product over multiple sources), if the number of items in an array is too low/high (for example alert on Instagram Actor if post has a lot of comments) and many other tasks like these. | ||
|
||
:::important | ||
|
||
Available dataset fields are taken from the last successful build of the monitored Actor. If different versions have different fields, currently the solution will always display only those from the default version. | ||
|
||
::: | ||
|
||
![Metric condition configuration](./images/metric-options.png) | ||
|
||
You can get notified by email, Slack, or in Apify Console. If you use Slack, we suggest using Slack notifications instead of email because they are more reliable, and you can also get notified quicker. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the image is used also on some other page so I have left it in the original folder and just change the path, but it can be done either way (change the folder and path for the othe page)