odoo/odoo · error · ValidationError

There is a syntax error in the barcode pattern %(pattern)s:

Error message

There is a syntax error in the barcode pattern %(pattern)s: braces can only contain N's followed by D's.

What it means

Raised by the @api.constrains('pattern') check on barcode.rule (addons/barcodes/models/barcode_rule.py:37). Barcode patterns may contain at most one brace pair, and inside the braces only the letters N (any digit) followed by D (decimal digits) are allowed, e.g. {NNNDD}. After stripping escaped braces (\{, \}, \\), the code regex-searches the remaining text for [{][N]*[D]*[}]; if exactly two brace characters exist but no {N*D*} match is found, this ValidationError is raised on record save/create/write.

Source

Thrown at addons/barcodes/models/barcode_rule.py:37

            ('ean8', 'EAN-8'),
            ('upca', 'UPC-A'),
        ], help='This rule will apply only if the barcode is encoded with the specified encoding')
    type = fields.Selection(
        string='Type', required=True, selection=[
            ('alias', 'Alias'),
            ('product', 'Unit Product'),
        ], default='product')
    pattern = fields.Char(string='Barcode Pattern', help="The barcode matching pattern", required=True, default='.*')
    alias = fields.Char(string='Alias', default='0', help='The matched pattern will alias to this barcode', required=True)

    @api.constrains('pattern')
    def _check_pattern(self):
        for rule in self:
            p = rule.pattern.replace('\\\\', 'X').replace('\\{', 'X').replace('\\}', 'X')
            findall = re.findall("[{]|[}]", p)  # p does not contain escaped { or }
            if len(findall) == 2:
                if not re.search("[{][N]*[D]*[}]", p):
                    raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: braces can only contain N's followed by D's.", pattern=rule.pattern))
                elif re.search("[{][}]", p):
                    raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: empty braces.", pattern=rule.pattern))
            elif len(findall) != 0:
                raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: a rule can only contain one pair of braces.", pattern=rule.pattern))
            elif p == '*':
                raise ValidationError(_(" '*' is not a valid Regex Barcode Pattern. Did you mean '.*'?"))
            try:
                re.compile(re.sub('{N+D*}', '', p))
            except re.error:
                raise ValidationError(_("The barcode pattern %(pattern)s does not lead to a valid regular expression.", pattern=rule.pattern))

View on GitHub (pinned to 1e661df964)

Solutions

  1. Fix the pattern so braces contain only N's followed by D's, e.g. '{NNDD}' or '{NNN}'.
  2. If a literal '{' or '}' is intended in the regex, escape it as '\{' / '\}' so it is ignored by this check.
  3. If the brace content is not a numeric placeholder, remove the braces entirely and use plain regex digit classes like '[0-9]{4}'.

Example fix

// before
rule.pattern = '^{NNX}'            // X inside braces -> ValidationError
// after
rule.pattern = '^{NND}'             // N's then D's only
Defensive patterns

Strategy: validation

Validate before calling

import re

def valid_brace_content(pattern: str) -> bool:
    p = pattern.replace('\\\\', 'X').replace('\\{', 'X').replace('\\}', 'X')
    braces = re.findall('[{]|[}]', p)
    return len(braces) != 2 or bool(re.search('[{][N]+[D]*[}]|[{][N]*[D]+[}]', p))

Try / catch

from odoo.exceptions import ValidationError
try:
    rule.write({'pattern': new_pattern})
except ValidationError as e:
    # surface to user; keep previous pattern
    raise UserError(str(e)) from e

Prevention

When it happens

Trigger: Creating or writing a barcode.rule record whose pattern field contains a brace pair whose content is not zero-or-more N's followed by zero-or-more D's — e.g. '12{NNX}', '{DNN}' (D before N), '{nn}' (lowercase), or '{N-D}'. Only fires when exactly 2 brace chars remain after replacing '\\', '\{', '\}' with 'X'.

Common situations: Admins hand-editing nomenclature rules in Settings > Technical > Barcode Nomenclatures; importing rule data via CSV/XML with malformed patterns; forgetting that the placeholder vocabulary is only N and D.

Related errors


AI-assisted analysis of odoo/odoo@1e661df964 (2026-08-15). Data as JSON: /api/errors/57f9557c135ccc3b. Report an issue: GitHub.