Json schema validation in Spring REST APIs
Asked Answered
D

3

15

I’m building a REST API using Spring Boot and [jackson-module-jsonSchema] (https://github.com/FasterXML/jackson-module-jsonSchema) for JSON schema generation. I’m looking the best way to validate the request JSON payload arriving to my APIs endpoints (Spring controllers) against the defined JSON schema defined for the exposed resource, validation includes check required fields , format , min and max values, etc.. everything we can validate against the schema.

Seems jackson json schema module is useful for schema generation but not for validation, am I right? Any suggestion on how to achieve what I’m trying to do?

Demibastion answered 18/1, 2016 at 15:46 Comment(2)
AFAIK the only thing that can do a json schema validation is the project: github.com/fge/json-schema-validator which is now looking for a new maintainer. Take a look at it.Adopt
see also bolerio.github.io/mjsonShealy
A
15

If you take a look at JSON schema site, there are only two libraries for validation in Java.

  1. The one that Jorge Campos suggested is mature, but looking for new maintainer: https://github.com/fge/json-schema-validator
  2. Second one is relatively new: http://github.com/everit-org/json-schema

I was recently in situation where I had to choose one or the other and I picked first option. It is being used also by Rest Assured library under the hood.

Able answered 18/1, 2016 at 20:17 Comment(3)
thanks for your answer @Able I tried github.com/fge/json-schema-validator and is looking good, when testing it I realized that jackson is generating JSON schema v3 and validator is exepcting v4-draft, I'm considering to change Jackson for other library , do you have any suggestion?Demibastion
mJson also fully supports schema validation and it's much simpler to use than the above: bolerio.github.io/mjsonShealy
Please note that now is only one library is recommended: json-schema.org/implementations.html#validatorsTrodden
E
2

You can also look at Rest Assured Json Schema validator

https://www.baeldung.com/rest-assured-json-schema

Enthrone answered 9/7, 2019 at 1:2 Comment(2)
Please give some explanation to the content of a link answer.Forthcoming
@IsraeldelaCruz The idea here is to define json schema in your classpath and then validate the response against this schema using json validator. something like this assertThat().body(matchesJsonSchemaInClasspath("event_0.json")) You can go through the example on the link and let me know if you still have any questions.Enthrone
F
0

As of 2023, the following JSON schema validator library seems to be a good fit: https://github.com/networknt/json-schema-validator

This is a Java implementation of the JSON Schema Core Draft v4, v6, v7, v2019-09 and v2020-12(partial) specification for JSON schema validation. In addition, it also works for OpenAPI 3.0 request/response validation with some configuration flags.

And there is one more validator library which looks promising but YMMV: https://github.com/openapi-processor/openapi-parser

Forestall answered 13/7, 2023 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.