6
6
import re
7
7
from enum import StrEnum , auto
8
8
from 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
10
10
11
11
from pydantic import (
12
+ BeforeValidator ,
12
13
Field ,
13
14
FilePath ,
14
15
NonNegativeInt ,
@@ -69,7 +70,19 @@ class Lookup_Type(StrEnum):
69
70
70
71
# TODO (#220): Split Lookup into 2 classes
71
72
class 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
+ )
73
86
# Per the documentation for transforms.conf, EXACT should not be specified in this list,
74
87
# so we include only WILDCARD and CIDR
75
88
match_type : list [Annotated [str , Field (pattern = r"(^WILDCARD|CIDR)\(.+\)$" )]] = Field (
@@ -88,7 +101,7 @@ def serialize_model(self):
88
101
89
102
# All fields custom to this model
90
103
model = {
91
- "default_match" : "true" if self .default_match is True else "false" ,
104
+ "default_match" : self .default_match ,
92
105
"match_type" : self .match_type_to_conf_format ,
93
106
"min_matches" : self .min_matches ,
94
107
"max_matches" : self .max_matches ,
0 commit comments