plone / guillotina

Python AsyncIO data API to manage billions of resources
https://guillotina.readthedocs.io/en/latest/
Other
187 stars 51 forks source link

Add regex field in guillotina schema #1096

Open rboixaderg opened 3 years ago

rboixaderg commented 3 years ago

Add regex field in guillotina schema

masipcat commented 3 years ago

You can use this in the meantime:

@implementer(ITextLine)
class RegexField(schema.TextLine):
    def __init__(self, *args, regex, **kwargs):
        super().__init__(*args, **kwargs)
        self.regex = re.compile(regex)

    def _validate(self, value):
        super()._validate(value)
        if not self.regex.match(value):
            raise InvalidValue(value, self.__name__)
rgba_color = RegexField(regex=r"^#[0-9A-Fa-f]{6,8}$")

I can open a PR as well if we want this in Guillotina.