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

Walkthrough

Space play step click any line
Three takeaways
  1. 1A compact canonical constructor is the single choke point for validating and normalizing every field of a record.
  2. 2Copying incoming collections with List.copyOf guarantees the record's immutability holds even against callers who keep a reference.
  3. 3A static factory keeps messy parsing logic out of the constructor while still funneling every instance through its invariants.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Validating config with a Java record — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code