66import re
77from enum import StrEnum , auto
88from functools import cached_property
9- from typing import TYPE_CHECKING , Annotated , Any , Literal , Optional , Self
9+ from typing import TYPE_CHECKING , Annotated , Any , Literal , Self
1010
1111from pydantic import (
12+ BeforeValidator ,
1213 Field ,
1314 FilePath ,
1415 NonNegativeInt ,
@@ -69,7 +70,19 @@ class Lookup_Type(StrEnum):
6970
7071# TODO (#220): Split Lookup into 2 classes
7172class Lookup (SecurityContentObject , abc .ABC ):
72- default_match : Optional [bool ] = None
73+ # We need to make sure that this is converted to a string because we widely
74+ # use the string "False" in our lookup content. However, PyYAML reads this
75+ # as a BOOL and this causes parsing to fail. As such, we will always
76+ # convert this to a string if it is passed as a bool
77+ default_match : Annotated [
78+ str , BeforeValidator (lambda dm : str (dm ).lower () if isinstance (dm , bool ) else dm )
79+ ] = Field (
80+ default = "" ,
81+ description = "This field is given a default value of ''"
82+ "because it is the default value specified in the transforms.conf "
83+ "docs. Giving it a type of str rather than str | None simplifies "
84+ "the typing for the field." ,
85+ )
7386 # Per the documentation for transforms.conf, EXACT should not be specified in this list,
7487 # so we include only WILDCARD and CIDR
7588 match_type : list [Annotated [str , Field (pattern = r"(^WILDCARD|CIDR)\(.+\)$" )]] = Field (
@@ -88,7 +101,7 @@ def serialize_model(self):
88101
89102 # All fields custom to this model
90103 model = {
91- "default_match" : "true" if self .default_match is True else "false" ,
104+ "default_match" : self .default_match ,
92105 "match_type" : self .match_type_to_conf_format ,
93106 "min_matches" : self .min_matches ,
94107 "max_matches" : self .max_matches ,
0 commit comments