Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: not-eq assertion #40

Closed
mindbound opened this issue Jul 24, 2024 · 5 comments · Fixed by #42
Closed

Feature request: not-eq assertion #40

mindbound opened this issue Jul 24, 2024 · 5 comments · Fixed by #42
Labels
assertions Issue or PR relating to valkyrie assertions enhancement New feature or request
Milestone

Comments

@mindbound
Copy link

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 tingerrr added enhancement New feature or request assertions Issue or PR relating to valkyrie assertions labels Jul 24, 2024
@tingerrr
Copy link
Member

tingerrr commented Jul 24, 2024

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
Copy link
Author

mindbound commented Jul 24, 2024

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

@mindbound mindbound reopened this Jul 24, 2024
@tingerrr
Copy link
Member

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.

@jamesrswift jamesrswift added this to the 0.2.2 milestone Jul 24, 2024
@jamesrswift jamesrswift linked a pull request Jul 24, 2024 that will close this issue
@jneug
Copy link
Contributor

jneug commented Jul 24, 2024

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

@jamesrswift
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertions Issue or PR relating to valkyrie assertions enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants