# cvc-pattern-valid: Value is not facet-valid with respect to pattern

Source: https://xmlvalid.com/errors/cvc-pattern-valid

> A value does not match the regular expression its type requires.

The message, as Java reports it: `cvc-pattern-valid: Value 'ab-12' is not facet-valid with respect to pattern '[A-Z]{3}-[0-9]{4}' for type 'Code'.`

## Why it happens

- The wrong letter case, length or separator for a code.
- Leading or trailing spaces: XML Schema patterns match the whole value, spaces included, unless the type collapses whitespace.

## How to fix it

- Compare the value with the pattern in the message; XML Schema patterns are anchored, so the whole value has to match. Xerces follows this message with [cvc-type.3.1.3](/errors/cvc-type-3-1-3) naming the element.
- Java's schema validation (`javax.xml.validation.Validator`, and JAXB or Spring on top of it) throws at the first error unless you set an `ErrorHandler` whose `error()` records the `SAXParseException` and carries on; each one has `getLineNumber()` and `getColumnNumber()`. A well-formedness error ends parsing whatever the handler does.

## The same problem in xmlvalid

libxml2, which xmlvalid validates with, reports it as [[facet 'pattern'] The value is not accepted by the pattern](https://xmlvalid.com/errors/facet-pattern): `Element 'code': [facet 'pattern'] The value 'ab-12' is not accepted by the pattern '[A-Z]{3}-[0-9]{4}'.`

## Example that fails

```xml
<order currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
  <code>ab-12</code>
</order>
```

## Fixed

```xml
<order currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
  <code>ABC-0012</code>
</order>
```
