Schema validation for Nim, in the style of Zod. Describe the shape your data should have, then run untrusted input — a JSON body, a form, a config file — through it. You get back the data cleaned up, or every reason it failed and where.
import bouncer
let user = obj({
"name": str().min(3).max(50),
"email": str().email(),
"age": integer().min(18)
})
let r = user.safeParse("""{"name":"Al","email":"nope","age":15}""")
if not r.ok:
for issue in r.issues:
echo issue
# name: must be at least 3 characters
# email: must be a valid email
# age: must be >= 18External data — off a network, from a user, out of a file — is untrusted until something
checks it. bouncer is that check: it turns unknown input into data you can rely on, or
tells you exactly what's wrong, field by field.
Nim's other validators annotate a static type with pragmas. bouncer takes Zod's route:
a schema is a value. You can build schemas at runtime, compose and reuse them, and
validate data you don't have a type for — the cases a type annotation can't cover.
nimble install bouncer
Nim 2.0+, no Nimble dependencies. Tested on Linux, macOS and Windows.
A valid parse doesn't just pass — it hands back a tidied value: strings trimmed, strings coerced to numbers, defaults filled in, unknown keys dropped. Handy for form and query data, where everything arrives as text:
let query = obj({
"page": integer().coerce().default(1), # "page=2" -> 2, missing -> 1
"term": str().trim().optional() # " nim " -> "nim"
})
query.parse("""{"page":"2","term":" nim ","junk":"x"}""")
# {"page": 2, "term": "nim"} — coerced, trimmed, "junk" dropped# scalars
str() .min(n) .max(n) .nonEmpty() .trim()
.email() .url() .uuid() .date() .datetime()
.pattern(r"…") .oneOf(["a", "b"])
integer() .min(n) .max(n) .positive() .nonNegative() .coerce()
number() .min(x) .max(x) .positive() .nonNegative() .coerce() # int or float
boolean() .coerce()
literal(v) # one exact value: a string, number or bool
# composites
obj({ "k": str(), … }) # add strict = true to reject unknown keys
arr(str()) .min(n) .max(n) .nonEmpty()
record(str()) # object with arbitrary keys, all values str()
union(a, b, …) # matches if any variant does
# on any schema
.optional() # value may be missing or null
.default(value) # fill in when missing or null
.refine(proc(n: JsonNode): bool … , "message")
.transform(proc(n: JsonNode): JsonNode … ) # reshape after it validates
# deriving object schemas (returns a new schema; the original is untouched)
.partial() # every field optional
.pick(["id", "name"]) .omit(["password"])
.extend({ "age": integer() }) merge(schemaA, schemaB)
# running it
schema.safeParse(node | text) # never raises; returns ParseResult
schema.parse(node | text) # returns the cleaned value, or raises BouncerError
# from a Nim type
schemaFor(MyType) # derive a schema from the type's shape
fromJson(MyType, node | text) # validate the shape, return a typed MyTypeDefine a schema once, then carve the variants you need from it — without touching the original:
let user = obj({
"id": integer(),
"name": str().min(1),
"password": str().min(8)
})
let public = user.omit(["password"]) # what you send back to clients
let update = user.partial() # a PATCH body: every field optional
let signup = user.pick(["name", "password"])ParseResult.ok says which path you got: read .value or .issues. Each Issue has a
path, a stable code (icTooSmall, icRequired, …) and a message — render it for a
user or ship it as an API response. Errors are collected, not fail-fast: one parse reports
everything that's wrong.
When a value can be more than one thing, list the options with union. It tries each
in turn and keeps the first that passes — so the branch that matched is also the one
that cleans the value:
let id = union(integer(), str().uuid()) # a numeric id or a UUID stringTag each object variant with a literal field and you have a discriminated union — the
shape depends on a kind, the way an event or a message does:
let event = union(
obj({ "kind": literal("click"), "x": integer(), "y": integer() }),
obj({ "kind": literal("key"), "code": str() })
)Already have a type? Derive the schema from it, or validate and deserialize in one step:
type
Address = object
city: string
zip: int
User = object
name: string
age: int
address: Address
nickname: Option[string] # optional
let u = fromJson(User, body) # a typed User, or BouncerError with .issues
let s = schemaFor(User) # just the schema — compose rules onto it
let adult = s.extend({ "age": integer().min(18) })schemaFor maps string→str(), int→integer(), Option[x]→optional, seq[x]→an
array, and nested objects recursively. fromJson checks the shape (bouncer errors, with
paths) and hands back a typed value.
Intersections, a real Date output type, and attaching rules through type pragmas —
maybe later.
MIT © DelfinVT