Skip to content

v0.4.0

Choose a tag to compare

@vkiryakov vkiryakov released this 13 Oct 08:42
· 43 commits to dev since this release
3c5e12d

Zefir 0.4.0 adds validator: validation rules declared on your structs.

validator

zefir-validator checks values against rules declared next to the struct.

  • pub const field_rules lists the rules of each field, validateSelf checks across fields, and v.custom adds a predicate of your own.
  • v.check returns the first violation without allocating; v.checkAll collects them all, at most 100. A violation has a code, a message and a path such as items[2].name.
  • A rule that does not fit its field is a compile error, and so are two rules in one tuple that no value can pass, such as v.min(10) with v.max(5).
  • 96 rules in eight groups: common (equals, oneOf, empty, …), numbers (min, max, range, positive, divisibleBy, finite, …), lists (minItems, maxItems, uniqueItems, uniqueBy, containsAll, containsNone, each), strings (minLength, maxLength, notBlank, alpha, contains, strongPassword, …), formats (email, httpUrl, fqdn, ip, port, decimal, …), encodings (json, jwt, base64, semVer, mimeType, colors, …), identifiers (uuid, iban, bic, creditCard, isbn, ean, btcAddress, ethereumAddress, …) and dates (date, rfc3339, iso8601, minDate, maxDate, militaryTime). Most rules have a public predicate, such as v.isEmail(s).
  • .withMessage sets a message, with placeholders.

See the validator guide. From this release on, the behaviour of a shipped rule is a contract: see Compatibility in the guide.

If you tried the early preview in the v0.3.0 tag: two rules in one tuple that no value can pass are now a compile error.

Other changes

  • log: the queue's detectors tell a lost wake-up from a slow thread (#32).
zig fetch --save git+https://github.com/vkiryakov/zefir#v0.4.0
const zefir = b.dependency("zefir", .{ .target = target, .optimize = optimize });
// in your module's .imports:
.{ .name = "validator", .module = zefir.module("zefir-validator") },

Until 1.0, breaking changes may happen in any minor release.