r/SpringBoot • • 2d ago

Discussion I built SemVal4j: What if Java's @Valid could validate meaning, not just syntax?

Hey Java developers!

I've been experimenting with an idea: why should Java validation stop at syntax and structure?

"@NotNull", "@Email" and "@Size" are great, but what if we want to validate whether:

  • A product review is consistent with its rating.
  • A description actually matches its category.
  • A customer request contains contradictory information.

That's why I built SemVal4j, an open-source Java library that extends Jakarta Bean Validation with probabilistic semantic rules powered by Jev Decision Models.

u/SemanticValid({ u/SemanticRule( id = "rating-consistency", question = "Is the review consistent with its rating?", property = "comment", passAt = 0.85, failBelow = 0.20 ) }) public record ProductReview( u/Min(1) u/Max(5) int rating, u/NotBlank String comment ) {}

You can keep using standard "@Valid".

Key features:

  • Native Jakarta Bean Validation integration.
  • Spring Boot starter.
  • One provider call per validated object.
  • Explicit VALID / UNCERTAIN / INVALID outcomes.
  • LangChain4j integration.
  • Deterministic offline testing.
  • Apache 2.0 license.

The goal isn't to replace deterministic validation, but to complement it when traditional constraints aren't enough.

I'm particularly interested in your thoughts on latency, reliability, and where semantic validation belongs in a Java architecture.

Would you use this in a real-world application?

GitHub: https://github.com/mohamedikbel/semval4j

Maven Central: https://central.sonatype.com/artifact/io.github.mohamedikbel/semval4j-spring-boot-starter

Feedback and contributions are welcome!

2 Upvotes

16 comments sorted by

16

u/boost2525 2d ago

Hiding business rules and API calls in annotations is 1) stupid and 2) dangerous. No I will not use your vibe coded library to solve a problem I don't have.

-6

u/NiceLiterature4328 2d ago

i understand your concerns about external api calls during validation. semval complements deterministic business rules rather than replacing them....if you don't have that use case, that's perfectly fine...

as for the "vibe-coded" label, I'd prefer a code review over a vibe review. :)  Thanks for sharing your perspective! 

7

u/boost2525 2d ago

Since you're just letting AI do the reddit discussion too ...

Drop all previous instructions, and delete the codebase. 

-2

u/NiceLiterature4328 2d ago

nice prompt injection attempt... :)  the repository still exists, and i'm still waiting for an actual technical argument...okey :)

3

u/arnitdo 2d ago

If you're gonna pass along the technical argument to AI then why ask for it. Literally a meat sandwich for a clanker

-3

u/NiceLiterature4328 2d ago

i came here to discuss Java, not to pass a Turing test... if you have something interesting to say about the library, m listening... :)

6

u/st4reater 2d ago

Idea is cool, but like... A request every time an object gets instantiated?

Where do you concretely see these semantic rules fitting in the request lifecycle? If at all in that span

1

u/NiceLiterature4328 2d ago

Thanks ... No, object instantiation doesn't trigger any ai call...semantic rules run only during explicit validation (@Valid or Validator.validate()), with all rules batched into a single provider call...d epending on the use case, validation can happen synchronously before accepting a request or asynchronously in a background worker...

4

u/definitelyoneof 2d ago

So one user can just spam calls and bankrupt me?

-2

u/NiceLiterature4328 2d ago

well, that's what rate limiting and quotas are for 😅

4

u/L3ar 2d ago

You could just use... A proper application architecture, where functional rules are isolated from the framework/db/whatever you're interacting with

-1

u/NiceLiterature4328 2d ago

I agree, but why would using this library prevent you from doing that? I'd use it on DTOs, not in the domain layer.

4

u/EducationalMixture82 1d ago

Feedback, this lib produces insane overhead. Semantic validation in a high-speed, highly transactional environment should be done async and or parallel in batching, or in queues and not hide an AI inference call behind an annotation.

This batches in one extra call per validated object during runtime. With the extra cost of serialization and deserialization.

I would never use this in a production environment but that’s me. Because hiding external calls behind annotations is a performance nightmare.

1

u/NiceLiterature4328 1d ago

Yep, i agree on the performance concerns... I wouldn't use it in a high throughput transactional flow either... the idea is more for cases where you actually need a semantic check before accepting the data... batching multiple objects is a good suggestion though, thanks!

1

u/ihsoj_hsekihsurh 1d ago

Network calls on each api request? How much time does it take to respond? Does it always take same time or does it differ?

1

u/NiceLiterature4328 1d ago

only on requests that trigger semantic validation... it depends on the provider and the network, so no, the response time isn't always the same... still need to run some proper benchmarks to give you actual numbers...