Skip to main content

pattern-length-incompatible

Finds string schemas whose pattern cannot produce a value within the declared length range.

AttributeValue
CategorySchema
MaturityPlatinum
OpenAPISwagger 2.0; OpenAPI 3.0, 3.1, 3.2
StarterOff
LenientWarning
Recommended–CompleteError

Intent

A regex and length constraints should overlap. If they do not, every candidate value is invalid.

Flags

  • minLength is greater than the longest string a supported pattern can match.
  • maxLength is smaller than the shortest string a supported pattern can match.

Specmatic evaluates fixed text, simple character classes, and {n}, {n,m}, or {n,} quantifiers. It skips patterns containing groups, alternation, *, +, ?, or escapes to avoid unreliable findings.

Does not flag

A compatible range, a schema without minLength or maxLength, or a complex pattern outside the supported analysis above.

See it fail

countryCode:
type: string
pattern: '^[A-Z]{2}$'
minLength: 3

The pattern produces exactly two characters, but the schema requires at least three.

Fix it

Align the length with the pattern:

countryCode:
type: string
pattern: '^[A-Z]{2}$'
minLength: 2
maxLength: 2

Configure

profiles:
default:
rules:
extends: [recommended]
override:
pattern-length-incompatible: warn

Nearby: invalid-min-length, invalid-max-length.