| = JSON Schema Validator Component | |
| :doctitle: JSON Schema Validator | |
| :shortname: json-validator | |
| :artifactid: camel-json-validator | |
| :description: Validate JSON payloads using NetworkNT JSON Schema. | |
| :since: 2.20 | |
| :supportlevel: Stable | |
| :tabs-sync-option: | |
| :component-header: Only producer is supported | |
| *Since Camel {since}* | |
| *{component-header}* | |
| 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: | |
| [source,xml] | |
| ---- | |
| <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. | |
| // component options: START | |
| include::partial$component-configure-options.adoc[] | |
| include::partial$component-endpoint-options.adoc[] | |
| include::partial$component-endpoint-headers.adoc[] | |
| // component options: END | |
| == Example | |
| Assuming we have the following JSON Schema: | |
| *myschema.json* | |
| [source,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. | |
| [tabs] | |
| ==== | |
| Java:: | |
| + | |
| [source,java] | |
| ---- | |
| from("direct:start") | |
| .to("json-validator:myschema.json") | |
| .to("mock:end"); | |
| ---- | |
| XML:: | |
| + | |
| [source,xml] | |
| ---- | |
| <route> | |
| <from uri="direct:start"/> | |
| <to uri="json-validator:myschema.json"/> | |
| <to uri="mock:end"/> | |
| </route> | |
| ---- | |
| YAML:: | |
| + | |
| [source,yaml] | |
| ---- | |
| - route: | |
| from: | |
| uri: direct:start | |
| steps: | |
| - to: | |
| uri: json-validator:myschema.json | |
| - to: | |
| uri: mock:end | |
| ---- | |
| ==== | |
| If you use the default schema loader, it will try to determine the schema version from the $schema property and instruct the https://github.com/networknt[validator] appropriately. If it can't find (or doesn't recognize) the $schema property, it will assume your schema is version https://json-schema.org/specification-links.html#draft-2019-09-formerly-known-as-draft-8[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 https://github.com/apache/camel/blob/main/components/camel-json-validator/src/test/java/org/apache/camel/component/jsonvalidator/LocalRefSchemaTest.java[unit test] and https://github.com/apache/camel/blob/main/components/camel-json-validator/src/test/resources/org/apache/camel/component/jsonvalidator/Order.json[schema] for an example. |