Value size constraint

class pyasn1.type.constraint.ValueSizeConstraint(minimum, maximum)

Create a ValueSizeConstraint object.

The ValueSizeConstraint satisfies any value for as long as its size falls within the range of permitted sizes.

The ValueSizeConstraint object can be applied to BitString, OctetString (including all character ASN.1 types), SequenceOf and SetOf types.

  • minimum (int) – Minimum permitted size of the value (inclusive)
  • maximum (int) – Maximum permitted size of the value (inclusive)


class BaseballTeamRoster(SetOf):
    ASN.1 specification:

    BaseballTeamRoster ::= SET SIZE (1..25) OF PlayerNames
    componentType = PlayerNames()
    subtypeSpec = ValueSizeConstraint(1, 25)

# this will succeed
team = BaseballTeamRoster()
team.extend(['Jan', 'Matej'])

# this will raise ValueConstraintError
team = BaseballTeamRoster()
team.extend(['Jan'] * 26)


Whenever ValueSizeConstraint is applied to mutable types (e.g. SequenceOf, SetOf), constraint validation only happens at the serialisation phase rather than schema instantiation phase (as it is with immutable types).