"Parse, don't validate"
Compatible with PydanticV2
Inspired by Phantom types
from light_types import LightType
class StartsWithString(str, LightType):
@classmethod
def validate(cls, value: str) -> bool:
return value.startswith("String")
from light_types import LightType
class StartsWithString(str, LightType):
@classmethod
def validate(cls, value: str) -> bool:
return value.startswith("String")
class MyModel(BaseModel):
value: StartsWithString
assert TypeAdapter(MyModel).validate_python({"value": "StringOk"})
from light_types import QLightType, NumericQ
class NumericBetween5And10(int, QLightType):
validator = (NumericQ() > 5).custom(lambda n: n < 10)
from light_types import QLightType, StringQ
class StartsWithString(str, QLightType):
validator = StringQ().startswith("String")
from light_types import QLightType, StringQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("String") & StringQ().custom(lambda s: s.count("o") >= 2)
from light_types import QLightType, StringQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("String") | ~StringQ().custom(lambda s: s.count("o") >= 2)
from light_types import QLightType, StringQ, LengthQ
class StringWith2O(str, QLightType):
validator = StringQ().startswith("foo") & (LengthQ[str]() > 5)
rye test | lint | fmt