Query Strings & Forms ​
parseQuery and parseForm decode percent-encoded text into the same object model used for JSON, with coercion enabled for numbers, booleans and nulls. stringifyQuery writes the model back as a query string.
The schema ​
zig
const Search = struct {
query: []const u8,
page: u16,
active: bool,
tags: []const []const u8,
};
const SearchSchema = z.object(Search, .{
.query = z.string(.{ .min = 1 }),
.page = z.withDefault(z.integer(u16, .{ .min = 1 }), 1),
.active = z.boolean(.{}),
.tags = z.array(z.string(.{ .min = 1 }), .{ .max = 4 }),
}, .{ .naming = .snake_case });The program ​
zig
pub fn main() !void {
const allocator = std.heap.page_allocator;
var result = try z.parseQuery(SearchSchema, allocator, "query=zig+validation&active=true&tags=fast&tags=safe", .{});
defer result.deinit();
if (result.value) |search| {
std.debug.print("{s} page {d} active={any} tags={d}\n", .{
search.query, search.page, search.active, search.tags.len,
});
const text = try z.stringifyQuery(SearchSchema, allocator, search, .{});
defer allocator.free(text);
std.debug.print("{s}\n", .{text});
} else {
const text = try result.errors().render(allocator, .{});
defer allocator.free(text);
std.debug.print("{s}", .{text});
}
var form = try z.parseForm(SearchSchema, allocator, "query=login&active=false", .{});
defer form.deinit();
std.debug.print("form accepted: {any}\n", .{form.ok()});
}Output ​
text
zig validation page 1 active=true tags=2
query=zig+validation&page=1&active=true&tags=fast&tags=safe
form accepted: falseWhat to notice ​
+and%20both decode to a space. Percent escapes are decoded before validation, sotags=a%2Cbis the texta,b, not two tags.- Repeated keys become arrays. A single occurrence of an array field still produces a one-element array; a single value never silently becomes a scalar.
pageis absent and receives its schema default1.- Coercion is limited to text transport:
"true"becomestrue, numeric text becomes an integer, and an out-of-range number still fails the integer schema. stringifyQueryuses the object's naming policy, so asnake_caseschema writespagerather thanpage/Pagevariants. Values are re-encoded, which is why the space inzig validationreturns as+.- The form fails because
tagsis required by the schema and the body has notagsvalue. That is the sameE007a JSON document would produce, with the path$.tags.
