• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _type.constraint:
3
4Constraints
5-----------
6
7ASN.1 standard has a built-in way of limiting the set of values
8a type can possibly have. Imposing value constraints on an ASN.1
9type, together with :ref:`tagging <type.tag>`, is a way of creating
10a more specialized subtype of an ASN.1 type.
11
12The pyasn1 implementation represents all flavors of constraints,
13as well as their combinations, as immutable Python objects. Ultimately,
14they get attached to ASN.1 type object at a *.subtypeSpec* attribute.
15
16.. code-block:: python
17
18   class Age(Integer):
19       """
20       ASN.1 specification:
21
22       Age ::= INTEGER (0..120)
23       """
24       subtypeSpec = ValueRangeConstraint(0, 120)
25
26
27.. toctree::
28   :maxdepth: 2
29
30   /pyasn1/type/constraint/singlevalue
31   /pyasn1/type/constraint/containedsubtype
32   /pyasn1/type/constraint/valuerange
33   /pyasn1/type/constraint/valuesize
34   /pyasn1/type/constraint/permittedalphabet
35   /pyasn1/type/constraint/withcomponents
36
37
38Logic operations on constraints
39+++++++++++++++++++++++++++++++
40
41Sometimes multiple constraints are applied on an ASN.1 type. To capture
42this situation, individual constraint objects can be glued together
43by the logic operator objects.
44
45The logic operators are Python objects that exhibit similar behaviour
46as the constraint objects do with the only difference that logic operators
47are instantiated on the constraint and logic operator objects, not on the
48bare values.
49
50.. code-block:: python
51
52   class PhoneNumber(NumericString):
53       """
54       ASN.1 specification:
55
56       PhoneNumber ::=
57           NumericString (FROM ("0".."9")) (SIZE (10))
58       """
59       subtypeSpec = ConstraintsIntersection(
60           ValueRangeConstraint('0', '9'), ValueSizeConstraint(10)
61       )
62
63.. toctree::
64   :maxdepth: 2
65
66   /pyasn1/type/constraint/constraintsintersection
67   /pyasn1/type/constraint/constraintsunion
68   /pyasn1/type/constraint/constraintsexclusion
69