Repository: elastic/ecs

Issue:
Set pattern field for `mac` fields
Implement a regex pattern to validate values using the `pattern` attribute from #1834 

Proposed regex: `^[A-F0-9]{2}(-[A-F0-9]{2}){5,}$`


Discussion:
The proposed pattern looks good to me. I ran some examples through it at https://regex101.com/r/JPolQ1/1.

Resolving patch:
diff --git a/scripts/schema/cleaner.py b/scripts/schema/cleaner.py
--- a/scripts/schema/cleaner.py
+++ b/scripts/schema/cleaner.py
@@ -268,10 +268,18 @@ def check_example_value(field, strict=True):
         strict_warning_handler(msg, strict)
 
     if pattern:
-        match = re.match(pattern, example_value)
-        if not match:
-            msg = f"Example value for field `{name}` does not match the regex defined in the pattern attribute: `{pattern}`."
-            strict_warning_handler(msg, strict)
+        # Examples with arrays must be handled
+        if 'array' in field['field_details']['normalize']:
+            # strips unnecessary chars in order to split each example value
+            example_values = example_value.translate(str.maketrans('', '', '"[] ')).split(',')
+        else:
+            example_values = [example_value]
+
+        for example_value in example_values:
+            match = re.match(pattern, example_value)
+            if not match:
+                msg = f"Example value for field `{name}` does not match the regex defined in the pattern attribute: `{pattern}`."
+                strict_warning_handler(msg, strict)
 
 
 def single_line_beta_description(schema_or_field, strict=True):


Test patch:
diff --git a/scripts/tests/unit/test_schema_cleaner.py b/scripts/tests/unit/test_schema_cleaner.py
--- a/scripts/tests/unit/test_schema_cleaner.py
+++ b/scripts/tests/unit/test_schema_cleaner.py
@@ -418,7 +418,8 @@ def test_example_value_mismatch_with_pattern(self):
             'field_details': {
                 'name': 'test',
                 'example': 'AA',
-                'pattern': 'A{3}'
+                'pattern': 'A{3}',
+                'normalize': [],
             }
         }
         with self.assertRaisesRegex(ValueError, 'does not match the regex defined in the pattern'):
@@ -429,7 +430,39 @@ def test_example_value_mismatch_with_pattern_strict_disabled(self):
             'field_details': {
                 'name': 'test',
                 'example': 'AA',
-                'pattern': 'A{3}'
+                'pattern': 'A{3}',
+                'normalize': [],
+            }
+        }
+        try:
+            with self.assertWarnsRegex(UserWarning, 'does not match the regex defined in the pattern'):
+                cleaner.check_example_value(field, strict=False)
+        except Exception:
+            self.fail("clean.check_example_value() raised Exception unexpectedly.")
+
+    def test_example_array_of_values_mismatch_with_pattern(self):
+        field = {
+            'field_details': {
+                'name': 'test',
+                'example': "['AAA', 'AA']",
+                'pattern': 'A{3}',
+                'normalize': [
+                    'array'
+                ]
+            }
+        }
+        with self.assertRaisesRegex(ValueError, 'does not match the regex defined in the pattern'):
+            cleaner.check_example_value(field)
+
+    def test_example_array_of_values_mismatch_with_patterns_strict_disabled(self):
+        field = {
+            'field_details': {
+                'name': 'test',
+                'example': "['AAA', 'AA']",
+                'pattern': 'A{3}',
+                'normalize': [
+                    'array'
+                ]
             }
         }
         try: