Closed mindbound closed 4 months ago
You can use the min
precondition on string:
#let schema = z.string(min: 1)
For more involved checks, you can simply create your own assertions these simply have to be dictionaries containing a condition and message callback, here's the min
assertion:
#(
// return true if the assertion passed
condition: (self, it) => it.len() >= rhs,
// return a message to panic with
message: (self, it) => "Length must be at least " + str(rhs),
)
Oh, that's great (and obvious in hindsight), thanks! Feel free to close the issue.
There are a few equals
assertions, but not inversion of this. I think it's fair to say that we could add those inverted versions, so I'll leave the issue open.
For strings something like "not-empty" might be more useful.
Pull request on this implements neq for lengths too, so not empty could be a length not equal to 0, which I think is similar enough to your request that I want to consider it already completed
It would be useful to have an assertion that acts opposite to
eq
, e.g.,#let schema = z.string(assertions: (z.assert.not-eq(""),))
It's possible that it is already doable with the current functionality but I haven't found an obvious way for doing so.