Description
A length limit alone lets a code with the wrong characters pass validation, even when it has the right number of characters.
Potential impact
If the server accepts the malformed code, a later lookup or processing step can fail.
Remediation
Use pattern for fields with a fixed format and validate the same format on the server. Skip the pattern for free-form text or when enum already lists every allowed value.
Examples
Here both code and message represent codes of exactly 15 lowercase ASCII letters or digits.
Before
json
{
"components": {
"schemas": {
"GeneralError": {
"properties": {
"code": {
"type": "string",
"maxLength": 15
},
"message": {
"type": "string",
"maxLength": 15
}
}
}
}
}
}
After
json
{
"components": {
"schemas": {
"GeneralError": {
"properties": {
"code": {
"type": "string",
"maxLength": 15,
"pattern": "^[0-9a-z]{15}$"
},
"message": {
"type": "string",
"maxLength": 15,
"pattern": "^[0-9a-z]{15}$"
}
}
}
}
}
}