java
39 lines · 9 steps
Validating config with a Java record
A compact record uses a canonical constructor to validate and defensively copy its fields, then parses JSON into instances.
Explained by
highlit
1public record ServerConfig(String host, int port, boolean tls, List<String> allowedOrigins) {
2
3 public ServerConfig {
4 Objects.requireNonNull(host, "host is required");
5 if (port < 1 || port > 65535) {
6 throw new IllegalArgumentException("port must be between 1 and 65535, got: " + port);
7 }
8 allowedOrigins = List.copyOf(allowedOrigins);
9 }
10
11 public static ServerConfig from(JsonNode node) {
12 if (node == null || !node.isObject()) {
13 throw new IllegalArgumentException("config must be a JSON object");
14 }
15
16 String host = requiredText(node, "host");
17 int port = node.path("port").asInt(8080);
18 boolean tls = node.path("tls").asBoolean(false);
19
20 List<String> origins = new ArrayList<>();
21 JsonNode originsNode = node.get("allowedOrigins");
22 if (originsNode != null) {
23 if (!originsNode.isArray()) {
24 throw new IllegalArgumentException("allowedOrigins must be an array");
25 }
26 originsNode.forEach(o -> origins.add(o.asText()));
27 }
28
29 return new ServerConfig(host, port, tls, origins);
30 }
31
32 private static String requiredText(JsonNode node, String field) {
33 JsonNode value = node.get(field);
34 if (value == null || !value.isTextual() || value.asText().isBlank()) {
35 throw new IllegalArgumentException("missing or invalid field: " + field);
36 }
37 return value.asText();
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A compact canonical constructor is the single choke point for validating and normalizing every field of a record.
- 2Copying incoming collections with List.copyOf guarantees the record's immutability holds even against callers who keep a reference.
- 3A static factory keeps messy parsing logic out of the constructor while still funneling every instance through its invariants.
Related explainers
ruby
module ConfigMerge module_function def deep_merge(base, override)
Recursively merging nested config hashes in Ruby
recursion
hash-merge
immutability
Intermediate
8 steps
java
public final class RetryExecutor { private final int maxAttempts; private final Duration initialDelay;
Exponential backoff retry in Java
retry
exponential-backoff
jitter
Intermediate
8 steps
go
package handlers import ( "fmt"
Handling multipart profile uploads in Gin
form binding
file upload
validation
Intermediate
7 steps
ruby
module DeepFreeze module_function def call(obj)
Recursively freezing nested Ruby data
recursion
immutability
pattern matching
Intermediate
9 steps
java
import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.locks.ReentrantReadWriteLock;
A thread-safe LRU cache in Java
lru-cache
concurrency
linkedhashmap
Intermediate
7 steps
rust
use axum::{ extract::{Path, State}, http::StatusCode, response::{IntoResponse, Response},
Typed error handling in an Axum handler
error-handling
extractors
validation
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/validating-config-with-a-java-record-explained-java-4b49/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.