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
java
@Component public class RegionCacheWarmer implements SmartInitializingSingleton { private static final Logger log = LoggerFactory.getLogger(RegionCacheWarmer.class);
Warming a Spring cache at startup
caching
startup-hook
dependency-injection
Intermediate
7 steps
java
public final class EmailNormalizer { private static final Pattern EMAIL_PATTERN = Pattern.compile( "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"
Normalizing email addresses in Java
validation
regex
normalization
Intermediate
8 steps
python
from datetime import date, timedelta from typing import Annotated from fastapi import APIRouter, Depends, Query
Validating date ranges with FastAPI dependencies
dependency-injection
validation
pydantic
Intermediate
6 steps
java
@RestController @RequestMapping("/api/products") @RequiredArgsConstructor public class ProductBatchController {
Batch JSON Merge Patch in Spring
json-merge-patch
rest-api
partial-update
Intermediate
8 steps
java
public class TimedFetchService { private final ExecutorService executor = Executors.newFixedThreadPool(8); private final HttpClient httpClient = HttpClient.newHttpClient();
Enforcing HTTP timeouts with a Future
concurrency
timeouts
thread-pool
Intermediate
8 steps
php
<?php namespace App\Http\Requests\DataObjects;
Typed request DTOs in Laravel
data-transfer-object
validation
immutability
Intermediate
6 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.