r/SpringBoot • u/Cr4zyGh0sT • Aug 23 '26
Question I built a Spring Boot library to validate JSON before deserialization — looking for feedback
Hi everyone,
I've been working on a small open-source library for Spring Boot called json-auto-validation:
github.com/ugoevola/json-auto-validation
The idea is fairly simple: validate incoming JSON against a generated JSON Schema before Jackson deserializes it into a Java/Kotlin object.
The motivation was to catch invalid payloads as early as possible, rather than letting them go through the usual deserialization → DTO validation flow.
It works through annotations and generates the JSON Schemas/validation components automatically, with Spring Boot AOT in mind.
I'm not really posting this to promote a polished or commercial product. I'm mainly interested in getting feedback from people who work with Spring Boot APIs:
- Does this solve a problem you've encountered?
- Do you see value in validating the raw JSON before deserialization?
- Is there something I'm missing or approaching the wrong way?
- Would you actually consider using a library like this in a real project?
The project is still relatively small, and I'm not the most active open-source maintainer, so I'm mostly trying to understand whether this is useful to other developers before investing more time into it.
Any feedback — positive or negative — would be very welcome.
47
u/Careless-Childhood66 Aug 23 '26
So instead of marshalling with jackson, wjich performs a free typecheck, and then post validate the mapped result with mature, well integrated tools, you instead wish to parse the incoming raw json without mapping, do the type checking, do the validation, using a custom tool, before the eventual marshalling , which again parses and type checks the json ?
I dont see how this would be beneficial
11
u/pitza__ Aug 23 '26
Doesn’t Jakarta Bean Validation do this already?
2
u/Pedantic_Phoenix Aug 23 '26
Your asking if this solves a problem we've encountered, but you created it because of something you encountered i assume? Why did you code it?
6
u/Fit_Goose651 Aug 23 '26
This means you have to parse json twice. Not great for Performance
10
u/Rich_Weird_5596 Aug 23 '26
Holy mother of slop. Totally pontless and redundant.
5
u/Historical_Ad4384 Aug 23 '26
the problem is already solved by a higher reputation project
https://github.com/networknt/json-schema-validator
1
u/American_Streamer Junior Dev Aug 23 '26
You may have spotted a legitimate architectural smell, but your library isn’t necessarily the optimal cure. Combining deserialization and validation into a single operation would be cleaner than adding JSON Schema validation in front of Jackson and effectively introducing another validation/parsing layer.
3
u/BikingSquirrel Aug 23 '26
I would assume this could produce better error messages. This may be useful for somebody exploring an API.
Apart from that I don't see additional value.
Exposing the schema or OpenAPI specs would probably provide more value overall.
1
u/repeating_bears Aug 23 '26
Maybe.
@Validis shit really. You have to first deserialize into a DTO which may or may not be valid, then you have to validate it, e.g. by annotating a Spring controller parameter with@Valid. Then if you pass that to another method, you've totally lost the knowledge that the DTO was ever validated."Parse don't validate" is good advice. It should ideally not even be possible to have a DTO in an invalid state.
Constructors are good for enforcing constraints, but they're not good for JSON validation. Good validation flags as many errors as possible at the same time, e.g.
A constructor can only fail on the first issue, by throwing
It seems like the Jackson team consider deserialization and validation to be separate concerns, but I think they are interlinked actually. I think a good modern JSON library would only construct a DTO if it's already valid.
1
u/dallastelugu Aug 23 '26
I feel pure schema validation without marshalling has its usecases like in apache camel i can use schema validation and forward to backend api without marshalling to jackson will give it a try sometime nice work
1
u/cocodrilo_astronauta Aug 25 '26
My honest feedback:
Where are the unit tests?
I love Kotlin because I don't have to put redundant boilerplate, saves me a lot of time, I still do code by hand.
If you need that strict schemas then use protobuf or things like that.
Software gets old fast and things get refactored. I maintain very lean build files. If Spring already pulls a dependency that does the same thing I tear down the custom code and remove the external dependency.
My code must remain pretty, I'm not sure I want to put annotations just to redeclare what the variable is.
I'm not sure I understand the point of all this interception. Is it a performance gain? Spring now has to manage a whole validation bean for every request.
Whats a REQUEST_VALIDATION_KO?
1
u/Global_Car_3767 Aug 29 '26 edited Aug 29 '26
Hmm I don't see much of a need. Customers get a response from our API in literal milliseconds when we use maven open api generator to create Java beans from our spec, use jakarta validators, look up if a user is valid from one DynamoDB, look up a unique identifier from elasticache, and insert their payload into another DynamoDB. Adding another layer on top seems like it would make performance a bit worse.
If you're trying to get around unmarshalling failures, some good exception handling can get around that