Validate enum symbols.
According to the Avro specification of the Enum field: the symbols attribute
is a JSON array, listing symbols, as JSON strings (required).
All symbols in an enum must be unique; duplicates are prohibited.
Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]*
(the same requirement as for names).
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -7,6 +7,7 @@ from os import path
from copy import deepcopy
import json
from libc.math cimport floor, log10
+import re
from ._schema_common import (
PRIMITIVES,
@@ -21,6 +22,8 @@ from ._schema_common import (
rabin_fingerprint,
)
+SYMBOL_REGEX = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
+
cpdef inline extract_record_type(schema):
if isinstance(schema, dict):
@@ -203,6 +206,8 @@ cdef _parse_schema(schema, namespace, expand, _write_hint, names, named_schemas)
raise SchemaParseException(f"redefined named type: {fullname}")
names.add(fullname)
+ _validate_enum_symbols(schema["symbols"])
+
named_schemas[fullname] = parsed_schema
parsed_schema["name"] = fullname
@@ -518,3 +523,13 @@ def fingerprint(parsing_canonical_form, algorithm):
h = hashlib.new(algorithm, parsing_canonical_form.encode())
return h.hexdigest()
+
+
+def _validate_enum_symbols(symbols):
+ for symbol in symbols:
+ if not isinstance(symbol, str) or not re.match(SYMBOL_REGEX, symbol):
+ raise SchemaParseException(
+ "Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]*"
+ )
+ if len(symbols) != len(set(symbols)):
+ raise SchemaParseException("All symbols in an enum must be unique")

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -5,6 +5,7 @@ import math
from os import path
from copy import deepcopy
import json
+import re
from ._schema_common import (
PRIMITIVES,
@@ -19,6 +20,8 @@ from ._schema_common import (
rabin_fingerprint,
)
+SYMBOL_REGEX = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
+
def extract_record_type(schema):
if isinstance(schema, dict):
@@ -344,6 +347,8 @@ def _parse_schema(schema, namespace, expand, _write_hint, names, named_schemas):
raise SchemaParseException(f"redefined named type: {fullname}")
names.add(fullname)
+ _validate_enum_symbols(schema["symbols"])
+
named_schemas[fullname] = parsed_schema
parsed_schema["name"] = fullname
@@ -772,3 +777,13 @@ def fingerprint(parsing_canonical_form, algorithm):
h = hashlib.new(algorithm, parsing_canonical_form.encode())
return h.hexdigest()
+
+
+def _validate_enum_symbols(symbols):
+ for symbol in symbols:
+ if not isinstance(symbol, str) or not re.match(SYMBOL_REGEX, symbol):
+ raise SchemaParseException(
+ "Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]*"
+ )
+ if len(symbols) != len(set(symbols)):
+ raise SchemaParseException("All symbols in an enum must be unique")

Validate enum symbols.

According to the Avro specification of the Enum field: the symbols attribute
is a JSON array, listing symbols, as JSON strings (required).
All symbols in an enum must be unique; duplicates are prohibited.
Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]*
(the same requirement as for names).