TOP

Validation

Applies to: ASN.1/C# v5.3

To validate data, an ASN.1 schema must define valid values, such as a range for an INTEGER or a size for a String, to limit the original type. This is called ASN.1 subtyping or constraining:

LicensePlate  ::= IA5String (SIZE (4..8))
CarSpeed      ::= INTEGER (0 .. 200)
WarningColors ::= UTF8String ("Red" | "Yellow")

When you constrain a type, in some cases, you can minimize the size of encodings. For example, an INTEGER constrained to 0..255 can be encoded into 8 bits instead of 32 bits. These constraints are called encoding-visible.

Validation Options

Data can be validated either manually or automatically. Automatic validation is performed either before encoding or after decoding, or both, and is controlled by the Validate encoder/decoder option.

Example

codec.EncoderOptions.Validate = true;
codec.DecoderOptions.Validate = false;

Alternatively, the application can manually call the Validate() method of a PDU object on demand:

pdu.Validate();

Constraints

The following examples contain the constraints that are currently supported. Note that all encoding-visible constraints are supported.

Example

 -- Permitted Alphabet constraint. Applicable only to strings
HardToReadChars ::= IA5String (FROM("8BI10OD5S"))

-- Size constraint. Applicable to strings, sequences and sets 
LicensePlate ::= IA5String (SIZE (4..8))

-- Value Range constraint. Applicable only to scalar types
CarSpeed ::= INTEGER (0 .. 200)

-- Single Value constraint. Applicable to all types
WarningColors ::= UTF8String ("Red" | "Yellow")
InfoColors ::= UTF8String ("Blue" | "White")
CitySpeedLimit ::= INTEGER (25 | 30 | 40)
HighwaySpeedLimit ::= INTEGER (40 | 50 | 60 | 70)

-- Contained Subtype constraint. Applicable to all types
SignColors ::= UTF8String (InfoColors UNION WarningColors)
SpeedLimitSigns ::= INTEGER (CitySpeedLimit | HighwaySpeedLimit | 10 | 65) 
RuralSpeedLimit ::= INTEGER (CitySpeedLimit INTERSECTION HighwaySpeedLimit) 

-- a mix of the above constraints 
NiceLicensePlate ::= LicensePlate ( 
			(FROM(("A".."Z" | "0".. "9" | "-") EXCEPT (HardToReadChars))) 
			INTERSECTION 
			(PATTERN "\w#3-\d#3") ) 

-- Inner constraint. Applicable only to structured types and allows  
-- optional components presence via PRESENT/ABSENT.
CarFeatures ::= SEQUENCE 	{
				passengers	 	INTEGER OPTIONAL, 
				maxspeed 		INTEGER OPTIONAL }

-- Contents Constraint: specifies a <type>and/or encoding <rule> of the contents. 
-- Applicable only to OCTET STRING or BIT STRING
PerInsideBer ::= OCTET STRING ( CONTAINING SEQUENCE OF CarFeatures 
		ENCODED BY {joint-iso-itu-t asn1(1) packed-encoding(3) basic(0) unaligned(1)}) 

Component Relation Constraint

An ASN.1 component relation constraint defines a table (an information object set) that is used to resolve types during automatic decoding of open types (see the AutoDecode option). For more information about their representation, see Open Types.


This documentation applies to the OSS® ASN.1 Tools for C# release 5.3 and later.

Copyright © 2024 OSS Nokalva, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise, without the prior permission of OSS Nokalva, Inc.
Every distributed copy of the OSS® ASN.1 Tools for C# is associated with a specific license and related unique license number. That license determines, among other things, what functions of the OSS ASN.1 Tools for C# are available to you.