frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Show HN: Replacing NotNull and Preconditions with fluent Java assertions

1•symplice•1h ago
Some time ago, I was tasked with maintaining a Java app built using the Clean architecture. In theory, the domain was supposed to remain agnostic, but in reality, every entity was either:

Sprinkled with Jakarta annotations (framework leakage) Filled with Guava’s Preconditions.checkArgument() calls (intrusive dependencies) Cluttered with manual null checks like if (x == null) (inconsistent and untested)

The painful part? A constructor like this (anonymized):

`public Invoice(String customerEmail, BigDecimal amount, List<Item> items, LocalDate dueDate) { this.customerEmail = Preconditions.checkNotNull(customerEmail, "email required"); Preconditions.checkArgument(customerEmail.contains("@"), "bad email"); Preconditions.checkArgument(customerEmail.length() < 100, "email too long");

    this.amount = Preconditions.checkNotNull(amount);
    Preconditions.checkArgument(amount.compareTo(BigDecimal.ZERO) > 0, "amount positive");
    // ...
}`

What I tried first:

I removed Jakarta Bean Validation and Apache Commons Validate because they impose strong framework coupling and generate verbose dependencies or generic exceptions, violating domain independence. Bean Validation 3.0 with records improves the situation but is still annotation-based, making it hard to compose for complex business rules.

Constraints I set for myself:

If I were to introduce a dependency into our domain layer, it had to: Have no transitive dependencies (single JAR, only java.base) Throw typed exceptions (not IllegalArgumentException) Support fluent chaining without hurting readability Allow custom predicates without falling into lambda hell

What I built

    `public Invoice(String email, BigDecimal amount, List<Item> items, LocalDate dueDate) {
    this.email = Assert.field("email", email)
                       .notBlank()
                       .email()
                       .maxLength(100)
                       .value();
                       
    this.amount = Assert.field("amount", amount)
                        .positive()
                        .value();
    // ...
}`

Why this matters

The exception hierarchy: Instead of IllegalArgumentException: bad email, you get EmailFormatInvalidException with structured fields (fieldName, invalidValue, constraint). Our monitoring now distinguishes between "user sent garbage" (400) and "developer logic error" (500) automatically.

Composable rules:

    `Assert.field("iban", iban)
          .notBlank()
          .satisfies(this::isValidIBANChecksum, "Checksum failed");`
The trade-offs (being honest)

Not for Hibernate users: If you're all-in on Jakarta and love @NotNull on your JPA entities, this adds nothing. This is for the "ports and adapters" crowd. Verbosity vs. annotations: Yes, it's more lines than @Email @NotNull. But those lines are explicit, testable, and framework-agnostic.

No AOP magic: You can't just slap this on a method parameter and expect auto-validation. This is imperative, intentional, constructor-level defense.

The bigger picture

I've come to believe that validation libraries fall into two categories: Framework validation (Jakarta, Spring): Great for binding/HTTP layer, terrible for domain purity. Utility validation (Guava, Apache): Great for reuse, terrible for expressiveness and exception granularity. There's a missing middle: Domain-native validation that reads like business rules and fails like typed domain events.

Discussion

For those doing DDD or Clean Architecture in Java - how do you handle input validation without contaminating your entities? Do you accept Jakarta annotations in your domain layer? Write defensive code manually? Or have you found another approach?

Also curious: would typed exceptions (e.g., NumberValueTooLowException vs IllegalArgumentException) actually help your production debugging, or is it over-engineering?

https://github.com/Sympol/pure-assert

Comments

pestatije•40m ago
Another middle-way to go is define your own domain preconditions, then map those to whichever agent your using at compile- and run- times

The Future Was Federated

https://news.dyne.org/the-future-was-federated/
1•fiatjaf•35s ago•0 comments

The Wayback Machine's plug-in to fix the internet's broken links problem

https://techcrunch.com/2026/02/04/the-wayback-machine-debuts-a-new-plugin-designed-to-fix-the-int...
1•i7l•1m ago•0 comments

I let the internet control a GitHub repo for 4 weeks

https://old.reddit.com/r/webdev/comments/1qrz4xj/i_let_the_internet_control_a_github_repo_for_4/
1•tomcam•2m ago•0 comments

Bedrock, an A.I. Startup for Construction, Raises $270M

https://www.nytimes.com/2026/02/04/business/dealbook/bedrock-robotics-ai-fundraise.html
1•lxm•4m ago•0 comments

Show HN: A Data Interchange Format

https://github.com/HyaenaTechnologies/configuration/blob/main/documentation/data-interchange.md
1•JohnMatthias•5m ago•0 comments

Novo Nordisk Warns of First Sales Drop Since Start of Ozempic

https://www.nytimes.com/2026/02/04/business/novo-nordisk-wegovy-sales-earnings.html
1•lxm•6m ago•0 comments

Realtime Eval Guide

https://developers.openai.com/cookbook/examples/realtime_eval_guide
1•gmays•7m ago•0 comments

How VPN-free remote access works

1•brintha•12m ago•0 comments

Expansion Microscopy Has Transformed How We See the Cellular World

https://www.quantamagazine.org/expansion-microscopy-has-transformed-how-we-see-the-cellular-world...
1•pseudolus•17m ago•0 comments

Anthropic Is Close to Releasing Claude Sonnet 5, per Rumors

https://www.macobserver.com/news/anthropic-is-close-to-releasing-claude-sonnet-5-per-rumors/
1•itisit•19m ago•0 comments

How to Write Good (Short) Docs

https://tombedor.dev/how-to-write-good-short-docs/
1•jjfoooo4•36m ago•0 comments

Living Through the Cultural Revolution

https://substack.com/home/post/p-186384573
5•expectsomuch•38m ago•1 comments

Fact Check: Did JK Rowling invite Jeffrey Epstein to a play?

https://rainyseason.substack.com/p/fact-check-did-jk-rowling-invite
3•duck_house•39m ago•0 comments

Banks seek out new buyers for Oracle data centre loans

https://www.ft.com/content/90aa74a5-b39d-4131-a138-367726cb18fb
6•zerosizedweasle•40m ago•2 comments

243,000 words dictated in 39 days, speech-to-text changed how I work

https://modulovalue.com/blog/voxtral-transcribe-and-wispr-flow/
2•modulovalue•45m ago•0 comments

Thomistic Philosophical Anthropology: An Interpretive Grid for LLMs

https://michaelmangialardi.substack.com/p/a-thomistic-philosophical-anthropology-llm-pt1
1•mikemangialardi•47m ago•0 comments

Writing an LLM from scratch, part 32B – Interventions: gradient clipping

https://www.gilesthomas.com/2026/02/llm-from-scratch-32b-interventions-gradient-clipping
2•gpjt•48m ago•0 comments

Nearly 40% of Stanford undergraduates claim they're disabled. I'm one of them

https://www.thetimes.com/us/news-today/article/40-percent-stanford-undergraduates-claim-disabled-...
3•mudil•49m ago•1 comments

Same Image, Different Score?

https://halide.cx/blog/chroma-handling/
2•computerbuster•52m ago•0 comments

Show HN: Pygantry – Why ship a whole OS when you just need a Python environment?

https://github.com/erabytse/Pygantry
2•takouzlo•53m ago•0 comments

Trustless crypto escrow for agent to agent (or humans)

https://coinpayportal.com/
1•cranberryturkey•55m ago•0 comments

Recent trends in the work of the Django Security Team

https://www.djangoproject.com/weblog/2026/feb/04/recent-trends-security-team/
2•todsacerdoti•1h ago•0 comments

Relic – a missing secrets artifact for Node and Edge

https://www.npmjs.com/package/@nick-skriabin/relic
1•nicholasrq•1h ago•0 comments

Does AI have human-level intelligence? The evidence is clear

https://www.nature.com/articles/d41586-026-00285-6
2•mudil•1h ago•0 comments

Vector: A platform for founders to turn vision into real business

https://govector.ai/
1•znich•1h ago•0 comments

Billy Bass Nelson, Original Bassist for Funkadelic, Dies at 75

https://www.nytimes.com/2026/02/02/arts/music/billy-bass-nelson-dead.html
3•bookofjoe•1h ago•2 comments

Common Bacteria Discovered in the Eye Linked to Cognitive Decline

https://www.cedars-sinai.org/newsroom/common-bacteria-discovered-in-the-eye-linked-to-cognitive-d...
2•thunderbong•1h ago•0 comments

Sam Altman responds to Anthropic's "Ads are coming to AI. But not to Claude" ads

https://xcancel.com/sama/status/2019139174339928189
32•PieUser•1h ago•30 comments

FBI stymied by Apple's Lockdown Mode after seizing journalist's iPhone

https://arstechnica.com/tech-policy/2026/02/fbi-stymied-by-apples-lockdown-mode-after-seizing-jou...
5•voxadam•1h ago•0 comments

Using React and Claude Code to make slides awesome and easy

https://newsletter.aimuscle.com/p/using-ai-agents-to-make-better-slides
1•Sherveen•1h ago•0 comments