vltr / middle

Flexible, extensible Python data structures for general usage
https://middle.readthedocs.io/en/latest/
MIT License
10 stars 1 forks source link

Setting fixed length to a string #41

Open ahopkins opened 5 years ago

ahopkins commented 5 years ago

Description

Set a length requirement for a str

What I Did

class Foo(middle.Model):
    bar: str = middle.field(min_length=4, max_length=4)
ValueError: The 'min_length' keyword value must not be equal or greater than the 'max_length' keyword value. 

This seems like it should be possible. Or, perhaps:

bar: str = middle.field(length=4)
ahopkins commented 5 years ago
bar: str = middle.field(length=4)

This is valid. I did not check before posting this. Perhaps an error message in this case would be helpful?

ValueError: 'min_length' is equal to 'max_length'. Instead, consider using 'length'.
vltr commented 5 years ago

@ahopkins well, length should not be a valid keyword (I think it's derived from attrs), only min_length and max_length, as described in the OpenAPI 3 specs. Perhaps the length=X could be a shorthand for min_length=X, max_length=X as well?

ahopkins commented 5 years ago

I think it should be. Seems like a very useful shorthand. Especially if you are going to drop attrs as a dependency.

ahopkins commented 5 years ago

But... that's just one man's opinion 😉