Validator has the function of analyzing an input JSON and comparing it to the provided schema, so a validation is created inside its pipeline and interrupts the flow if the structure isn’t aligned with what’s declared.

JSON Schema is a vocabulary widely known in the market, which allows the validation of JSON documentation. To know more about his vocabulary, click here.

Take a look at the configuration parameters of the component:

  • JSON Schema: JSON Schema definition that validates JSON

Messages flow

Input

The component accepts JSON-type input messages to make the validation.

{
"data": [
{
"product": "Samsung 4k Q60T 55",
"price": 3278.99
},
{
"product": "Samsung galaxy S20 128GB",
"price": 3698.99
}
]
}

Output

The component doesn’t change any information of the input message when JSON Validator is aligned with JSON Schema. Therefore, the message is returned to the following component or is used as final return if this component is the last step of the pipeline.

{
"data": [
{
"product": "Samsung 4k Q60T 55",
"price": 3278.99
},
{
"product": "Samsung galaxy S20 128GB",
"price": 3698.99
}
]
}

Validator in Action

See below how the component behaves in specific situations and what its respective configuration is.

  • Informing a schema and sending a JSON that doesn’t match the requirements

Configure the component with the following schema:

{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"examples": [
{
"product": "Product name",
"price": 1.11
}
],
"required": [
"product",
"price"
],
"properties": {
"product": {
"$id": "#/properties/product",
"type": "string",
"default": ""
},
"price": {
"$id": "#/properties/price",
"type": "number",
"default": 0.0
}
},
"additionalProperties": true
}

Then send the following JSON to the component:

Input

{
"name": "Samsung galaxy S20 128GB",
"price": 3698.99
}

Output

{
"timestamp": 1614186691087,
"error": "Failed to validate message. #Validation: error: object has missing required properties ([\"product\"])\n level: \"error\"\n schema: {\"loadingURI\":\"#\",\"pointer\":\"\"}\n instance: {\"pointer\":\"\"}\n domain: \"validation\"\n keyword: \"required\"\n required: [\"price\",\"product\"]\n missing: [\"product\"]\n",
"code": 400
}

  • Informing a schema and sending a JSON that matches the requirements

Configure the component with the following schema:

{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"default": {},
"examples": [
{
"product": "Product name",
"price": 1.11
}
],
"required": [
"product",
"price"
],
"properties": {
"product": {
"$id": "#/properties/product",
"type": "string",
"default": ""
},
"price": {
"$id": "#/properties/price",
"type": "number",
"default": 0.0
}
},
"additionalProperties": true
}

Then send the following JSON to the component:

Input

{
"product": "Samsung galaxy S20 128GB",
"price": 3698.99
}

Output

{
"product": "Samsung galaxy S20 128GB",
"price": 3698.99
}
Did this answer your question?