Skip to content

Files

118 lines (92 loc) · 3.66 KB

json-validator-component.adoc

File metadata and controls

118 lines (92 loc) · 3.66 KB

JSON Schema Validator

Since Camel 2.20

Only producer is supported

The JSON Schema Validator component performs bean validation of the message body against JSON Schemas v4, v6, v7, v2019-09 draft and v2020-12(partial) using the NetworkNT JSON Schema library (https://github.com/networknt/json-schema-validator).

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-json-validator</artifactId>
    <version>x.y.z</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

json-validator:resourceUri[?options]

Where resourceUri is some URL to a local resource on the classpath or a full URL to a remote resource or resource on the file system which contains the JSON Schema to validate against.

Example

Assuming we have the following JSON Schema:

myschema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {},
  "id": "my-schema",
  "properties": {
    "id": {
      "default": 1,
      "description": "An explanation about the purpose of this instance.",
      "id": "/properties/id",
      "title": "The id schema",
      "type": "integer"
    },
    "name": {
      "default": "A green door",
      "description": "An explanation about the purpose of this instance.",
      "id": "/properties/name",
      "title": "The name schema",
      "type": "string"
    },
    "price": {
      "default": 12.5,
      "description": "An explanation about the purpose of this instance.",
      "id": "/properties/price",
      "title": "The price schema",
      "type": "number"
    }
  },
  "required": [
    "name",
    "id",
    "price"
  ],
  "type": "object"
}

We can validate incoming JSON with the following Camel route, where myschema.json is loaded from the classpath.

from("direct:start")
  .to("json-validator:myschema.json")
  .to("mock:end")

If you use the default schema loader, it will try to determine the schema version from the $schema property and instruct the validator appropriately. If it can’t find (or doesn’t recognize) the $schema property, it will assume your schema is version 2019-09.

If your schema is local to your application (e.g. a classpath location as opposed to URL), your schema can also contain $ref links to a relative subschema in the classpath. Per the JSON schema spec, your schema must not have an $id identifier property for this to work properly. See the unit test and schema for an example.

spring-boot:partial$starter.adoc