SchemaGuard
You give it data and a schema. It returns a clear Result β valid data, or a plain-English reason why not. Never a crash.
Every payload crossing your boundary β API body, config file, upload β either matches the shape you expect, or it doesnβt.
SchemaGuard answers that in one handshake:
Result.success β data is safe to use as-is.Result.failure β what failed, and where.No exceptions thrown for βbad shape.β No half-processed data leaking through.
What it replaces
- Annotate a class; hope the framework wires it
- Reflection scans objects at runtime
- Bad data throws deep in unrelated code
- Debug by chasing a stack trace
- Write the schema once as a plain
.axiomfile - No annotations, no reflection, no scanning
- Bad data never throws β you get a
Result - The failure message names the field and rule
How you actually use it (3.0.0)
Content is a MemorySegment (heap, native or memory-mapped) or an already-parsed map. Three steps. Thatβs the whole interaction.
final var content = MemorySegment.ofArray(contentBytes); // or FileChannel.map(..)
final var result = SchemaGuard
.checkContent(content)
.basedOnSchemaInPath("user-profile")
.withAxiomParser();
final var data = SchemaGuard
.checkContent(body) // body = MemorySegment.ofArray(request bytes)
.basedOnSchemaInPath("schemas/account_query_schema")
.withParser(seg -> JsonParser.take(seg)
.openBuffer()
.ensureRootIsObject()
.parseObject())
.getOrThrow();
final var result = SchemaGuard
.checkContent(myPersistentMap)
.basedOnSchemaInPath("user-profile")
.withAxiomParser();
Hand it a MemorySegment or a PersistentMap.
Name the schema file via basedOnSchemaInPath.
Get a Result β never a structural crash.
checkContent(byte[])is nowcheckContent(MemorySegment). Wrap existing arrays withMemorySegment.ofArray(bytes).checkContent(PersistentMap)is unchanged.withParseris nowFunction<MemorySegment, PersistentMap<String, Object>>.- Schemas are memory-mapped into a shared arena and cached. Use
SchemaGuard.reload(path)/reloadAll()for hot reload. - Full details: 2.0.0 β 3.0.0 changelog.
Writing a schema: the .axiom file
A schema is a plain file, not code. Drop it under schemas/, reference it by path β nothing to compile or annotate.
{
required: [status, limit]
properties: {
status: {
type: string
enum: [ACTIVE, INACTIVE, SUSPENDED, PENDING]
}
limit: {
type: integer
min: 1
max: 1000
description: Maximum records per fetch.
}
}
}
Part of axiom-language Β· depends on axiom-sovereign + axiom