Cottonwood-Technology / ValidX

Fast, powerful, and flexible validator with sane syntax.
BSD 2-Clause "Simplified" License
20 stars 4 forks source link

Why doesnt Str offer a coerce option? #5

Closed darkdreamingdan closed 1 year ago

darkdreamingdan commented 3 years ago

Hi, fantastic work on this library! I was just wondering why the Str validator doesn't have a coerce option. This is useful if you want to, for example, check the length of a number is less than 3 digits:

validator = Str(maxlen=3)
validator(4000)

I was just trying to understand if there was any specific reason not to have a coerce for Strings, even if it isn't immediately obviously needed.

kr41 commented 3 years ago

Coercing to string doesn't validate anything at all, because any Python object can be converted to string. I think, it's just useless. As for your example, it's dirty and counter-intuitive. Why just don't use Int(min=999) instead, if you need to check number?

darkdreamingdan commented 3 years ago

Hey, perhaps I gave a bad example. Coercing to string by itself doesn't validate anything at all, however it's useful particularly for pattern checks and when the type was originally inferred incorrectly (because of a bug in source data). For example

We can always do this seperately, but sometimes it's useful to use the Str validation arguments on types that aren't initially strings (particularly float and integer)

kr41 commented 3 years ago

That makes sense.