keleshev / schema

Schema validation just got Pythonic
MIT License
2.86k stars 214 forks source link

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

Open tgross35 opened 2 years ago

tgross35 commented 2 years ago

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 commented 1 year ago

Had the same requirement. Managed to get this working:

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