Repository navigation
Expand file tree
/
Copy pathSchemaValidation.java
More file actions
43 lines (38 loc) · 2.27 KB
/
Copy pathSchemaValidation.java
File metadata and controls
43 lines (38 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// Validate a TSON data document against a TSON *schema* -- no build tool.
///
/// A single-file Java 25 program (compact source file + instance `main`), loading the `tson`
/// library via the module system:
///
/// ./gradlew :tson:modules
/// java --module-path tson/build/modules --add-modules io.ltr8.tson examples/SchemaValidation.java
///
/// `Tson.standard()` bootstraps the standard library (meta.tn, numeric.tn, temporal.tn, net.tn, core.tn and policy.tn);
//`resolve` / registers a schema under its own `!!id`. This data isn't self-describing, so the reader is told which /
//schema and type apply -- `withSchema(uri).readAs(source, type)`. Tree mode returns an immutable, / queryable TsonValue
//-- no Java class involved, the TSON schema is the source of truth -- that preserves / structure and types and is
//navigable with get/at.
import module io.ltr8.tson;
void main() {
Tson tson = Tson.standard();
String schema = """
!!id:"https://example.com/2026/40/app/server-1.tn"
!!import:"https://tson.io/2026/40/m/meta.tn"
!!import:"https://tson.io/2026/40/m/core.tn"
!schema {
server => { hostname: text port: int32 }
}""";
tson.resolve(schema); // registers it under its own !!id
var reader = tson.treeReader().withSchema("https://example.com/2026/40/app/server-1.tn");
TsonValue server = reader.readAs("{ hostname: \"web-01\" port: 8080 }", "server");
IO.println("hostname: " + server.get("hostname").asString().orElseThrow()); // web-01
IO.println("port: " + server.at("/port").asInt().orElseThrow()); // 8080
// A bad value surfaces as a diagnostic rather than a wrong result. Deriving a reader with a
// collecting receiver gathers every problem in one pass instead of stopping at the first:
var problems = DiagnosticsReceiver.collecting();
var bad = "{ hostname: \"web-01\" port: 99999999999999 }"; // port is out of int32 range
reader.withDiagnostics(problems).readAs(bad, "server");
for (Diagnostic d : problems.diagnostics()) {
// path is Optional because "" is RFC 6901's root, a real location, not an absence.
IO.println("problem: " + d.path().orElse("(no data location)") + " -- " + d.message());
}
}