Schema Validator
Overview
Schema validator checks if the provided doc
node complies with the json schema defined - i.e. if a property that has to be an array is not provided as a string, etc. If the object is not as expected it is possible to throw an error (that combined with catch step can stop the further flow execution) as well as the result of the validation can be stored in te doc
node.
This step is useful to validate the integrity of the received data.
For more details please refer to JSON Schema Documentation
You can generate JSON Schema for your input using JSON Schema Tool
Configuration
Data path
- path to thedoc
node on which to execute the schema validationJSON schema(overrides JSON inline schema)
- schema definition for thedoc
node (Requires schema to be set up first in ACE'sschemas
section. Afterwards the needed schema has to be selected under this selector.)JSON Inline schema
- schema definition for thedoc
nodethrow as error
- if checked, failed schema validation will throw an errortarget path
-doc
node on which to store the validation result (empty array if successful, error object if failed)
Providing either of JSON Schema or JSON Inline Schema properties is required. If both are provided, JSON schema property has more preference.
Examples
The example flow bellow showcase how a doc
node can be validated.
We assume that the flow input looks like this:
{
"input": {
"stringProp": "test prop"
}
}
In the below example, the Schema Validator
step is configured to verify that the 'stringProp' property on the doc
input
node is a required property that exists and its value is a string.
The validation results will be stored on the validations
node (we have opted out of throwing an error so the result will be appended to the validations node).
So if the input node does not contain 'stringProp' key or if its corresponding value is not string, then validations array will contain validation errors.
The below example uses JSON inline schema
property, similarly we can use the JSON schema
property defining the schema under schemas and selecting it.
tags:
- general
steps:
- stepType: ajv
config:
dataPath: input
targetPath: validations
schema:
title: testSchema
nodeKey: testSchema
properties:
stringProp:
type: string
type: object
required:
- stringProp
name: Schema Validator
description: ""
condition: ""
sampleData:
input:
stringPropUpdated: test
Result with valid input
Input
{
"input":
{
"stringProp":"test"
}
}
Result
{
"doc": {
"input": {
"stringProp": "test"
},
"validations": []
},
"errors": [],
"performance": {
"steps": [
{
"step": "ajv",
"executionTime": 6
}
],
"executionTimeOfFlow": 7,
"timeMetric": "ms"
}
}
Result with invalid input
Input
{
"input": {
"stringPropUpdated": "test"
}
}
Result
{
"doc": {
"input": {
"stringPropUpdated": "test"
},
"validations": [
{
"keyword": "required",
"dataPath": "",
"schemaPath": "#/required",
"params": {
"missingProperty": "stringProp"
},
"message": "should have required property 'stringProp'"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "ajv",
"executionTime": 7
}
],
"executionTimeOfFlow": 7,
"timeMetric": "ms"
}
}
Result with above invalid input, when Throw as error checked
{
"doc": {
"input": {
"stringPropUpdated": "test"
}
},
"errors": [
{
"error": "Error in flow 'schemaValidatorTest' : Schema validation error",
"validationErrors": [
{
"keyword": "required",
"dataPath": "",
"schemaPath": "#/required",
"params": {
"missingProperty": "stringProp"
},
"message": "should have required property 'stringProp'"
}
]
}
],
"performance": {
"steps": [
{
"step": "ajv",
"executionTime": 9
}
],
"executionTimeOfFlow": 10,
"timeMetric": "ms"
}
}
In the above example, the Schema Validator
step is next updated with testSchemaUpdated
option which verify that the stringPropUpdated
property on the doc
input
node is a required property that exists and its value is a string.
So if the input node does not contain 'stringPropUpdated' key or if its corresponding value is not string, it validations array will contain validation errors.
Here as both JSON Schema
and JSON inline schema
properties are provided, the JSON Schema property has more preference.
{
"title": "testSchemaUpdated",
"nodeKey": "testSchemaUpdated",
"properties": {
"stringPropUpdated": {
"type": "string"
}
},
"type": "object",
"required": ["stringPropUpdated"]
}
tags:
- general
steps:
- stepType: ajv
config:
dataPath: input
targetPath: validations
schema:
title: testSchema
nodeKey: testSchema
properties:
stringProp:
type: string
type: object
required:
- stringProp
schemaReference: testSchemaUpdated
name: Schema Validator
description: ""
condition: ""
sampleData:
input:
stringPropUpdated: test
Result with valid input
Input
{
"input": {
"stringPropUpdated": "test"
}
}
Result
{
"doc": {
"input": {
"stringPropUpdated": "test"
},
"validations": []
},
"errors": [],
"performance": {
"steps": [
{
"step": "ajv",
"executionTime": 26
}
],
"executionTimeOfFlow": 28,
"timeMetric": "ms"
}
}
Result with invalid input
Input
{
"input": {
"stringProp": "test"
}
}
Result
{
"doc": {
"input": {
"stringProp": "test"
},
"validations": [
{
"keyword": "required",
"dataPath": "",
"schemaPath": "#/required",
"params": {
"missingProperty": "stringPropUpdated"
},
"message": "should have required property 'stringPropUpdated'"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "ajv",
"executionTime": 18
}
],
"executionTimeOfFlow": 18,
"timeMetric": "ms"
}
}