You can use maxLength in OCL to read the maximum permitted length of a string attribute and prevent values that are too long from being saved.
Syntax
<attribute>.maxLengthThe operator returns the maximum length allowed for the string attribute.
Example
Given a class Car with the string attribute Name, use:
self.Name.maxLengthThis expression returns the maximum allowed length for Car.Name.
Prevent a string attribute from overflowing
Use maxLength when code may assign text that exceeds the storage limit of an attribute. For example, the following expression truncates errorinfo before it is saved:
self.errorinfo.subString(1,self.errorinfo.maxLength-1)This pattern helps avoid database errors such as String or binary data would be truncated. For the full guidance on diagnosing the error, choosing an appropriate attribute length, and applying truncation, see Documentation:String attribute overflowing.
Related string operators
Do not use maxLength to determine how many characters a string currently contains. Use length or Size for the current character count. Use maxLength when you need the configured maximum for a string attribute.
