[Cubicweb] [cubicweb] Constraint an Entity's attribute of type String to a regular expression?

Noé Gaumont noe.gaumont at logilab.fr
Thu Sep 10 14:48:24 CEST 2020


Hi,

It is possible to declare constraints on attributes.
The documentation is available 
https://cubicweb.readthedocs.io/en/3.28/book/devrepo/datamodel/definition/#constraints 
, but do not list RegExp.

For regexp constraints, see 
https://yams.readthedocs.io/en/latest/yams.html#yams.constraints.RegexpConstraint

For your use-case, it would be :

from yams.constraints import RegexpConstraint

class  Organisation(EntityType):name 
=String(required=True,unique=True,maxsize=255, 
constraints=[RegexpConstraint('^[^_]*$')])

Noé

Le 10/09/2020 à 14:31, christine.absil at engie.com a écrit :
>
> Hi all,
>
> A simple (and stupid?) question : in a cubicweb schema, is it 
> (“natively”) possible to constrain an Entity string attribute to a 
> regular expression?
>
> Would there be a dedicated param of the String function to declare that?
>
> For instance, if I wanted my Organisations names not to contain any 
> “_” character
>
> **
>
> *class**Organisation*(EntityType):
>
> name =String(required=True,unique=True,maxsize=255)
>
> Many thanks
>
> Regards
>
>
> Christine
>
> ENGIE Mail Disclaimer: http://www.engie.com/disclaimer/
>
> _______________________________________________
> Cubicweb mailing list
> Cubicweb at lists.cubicweb.org
> https://lists.cubicweb.org/mailman/listinfo/cubicweb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubicweb.org/pipermail/cubicweb/attachments/20200910/b0538ca7/attachment-0027.html>


More information about the Cubicweb mailing list