typst-community / valkyrie

Type safe type safety for Typst
Other
33 stars 4 forks source link

Feature request: `not-eq` assertion #40

Closed mindbound closed 4 months ago

mindbound commented 4 months ago

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.

tingerrr commented 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),
)
mindbound commented 4 months ago

Oh, that's great (and obvious in hindsight), thanks! Feel free to close the issue.

tingerrr commented 4 months ago

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.

jneug commented 4 months ago

For strings something like "not-empty" might be more useful.

jamesrswift commented 4 months ago

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