WITH COMPONENTS constraint

class pyasn1.type.constraint.WithComponentsConstraint(*fields)

Create a WithComponentsConstraint object.

The WithComponentsConstraint satisfies any mapping object that has constrained fields present or absent, what is indicated by ComponentPresentConstraint and ComponentAbsentConstraint objects respectively.

The WithComponentsConstraint object is typically applied to Set or Sequence types.

Parameters

*fields (tuple) – Zero or more tuples of (field, constraint) indicating constrained fields.

Notes

On top of the primary use of WithComponentsConstraint (ensuring presence or absence of particular components of a Set or Sequence), it is also possible to pass any other constraint objects or their combinations. In case of scalar fields, these constraints will be verified in addition to the constraints belonging to scalar components themselves. However, formally, these additional constraints do not change the type of these ASN.1 objects.

Examples

class Item(Sequence):  #  Set is similar
    '''
    ASN.1 specification:

    Item ::= SEQUENCE {
        id    INTEGER OPTIONAL,
        name  OCTET STRING OPTIONAL
    } WITH COMPONENTS id PRESENT, name ABSENT | id ABSENT, name PRESENT
    '''
    componentType = NamedTypes(
        OptionalNamedType('id', Integer()),
        OptionalNamedType('name', OctetString())
    )
    withComponents = ConstraintsUnion(
        WithComponentsConstraint(
            ('id', ComponentPresentConstraint()),
            ('name', ComponentAbsentConstraint())
        ),
        WithComponentsConstraint(
            ('id', ComponentAbsentConstraint()),
            ('name', ComponentPresentConstraint())
        )
    )

item = Item()

# This will succeed
item['id'] = 1

# This will succeed
item.reset()
item['name'] = 'John'

# This will fail (on encoding)
item.reset()
descr['id'] = 1
descr['name'] = 'John'
class pyasn1.type.constraint.ComponentPresentConstraint

Create a ComponentPresentConstraint object.

The ComponentPresentConstraint is only satisfied when the value is not None.

The ComponentPresentConstraint object is typically used with WithComponentsConstraint.

Examples

present = ComponentPresentConstraint()

# this will succeed
present('whatever')

# this will raise ValueConstraintError
present(None)
class pyasn1.type.constraint.ComponentAbsentConstraint

Create a ComponentAbsentConstraint object.

The ComponentAbsentConstraint is only satisfied when the value is None.

The ComponentAbsentConstraint object is typically used with WithComponentsConstraint.

Examples

absent = ComponentAbsentConstraint()

# this will succeed
absent(None)

# this will raise ValueConstraintError
absent('whatever')