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

What is the best way to disallow empty arrays/lists? #277

Open
tgross35 opened this issue Apr 5, 2022 · 1 comment
Open

What is the best way to disallow empty arrays/lists? #277

tgross35 opened this issue Apr 5, 2022 · 1 comment

Comments

@tgross35
Copy link

tgross35 commented Apr 5, 2022

In the doc, it's mentioned that validating an empty list [] will return true.

To do that, you need Schema(Or({int:int}, {})). This is unlike what happens with lists, where Schema([int]).is_valid([]) will return True.

What is the best way to disallow this? The best thing I can think of now is to import Use and call something that raises an error if nonempty. However, this isn't good for anything recursive.

@nuriel77
Copy link

nuriel77 commented Dec 1, 2022

Had the same requirement.
Managed to get this working:

Schema({
  "some_list": And(lambda n: len(n), [Regex(URL_REGEX)])
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants