|
5 | 5 | from ..enumeration import DataType, IntegerFormat, NumberFormat, StringFormat |
6 | 6 | from ..errors import ParserError |
7 | 7 | from ..specification import Array, Boolean, Discriminator, Integer, Number, Object, OneOf, Property, Schema, String |
| 8 | +from ..loose_types import ( |
| 9 | + LooseIntegerFormat, |
| 10 | + LooseNumberFormat, |
| 11 | + LooseStringFormat, |
| 12 | +) |
8 | 13 |
|
9 | 14 | SchemaBuilderMethod = Callable[[dict], Schema] |
10 | 15 |
|
@@ -79,8 +84,10 @@ def merge_all_of_schemas(original_data: dict) -> dict: |
79 | 84 |
|
80 | 85 | class SchemaFactory: |
81 | 86 | _builders: Dict[DataType, SchemaBuilderMethod] |
| 87 | + strict_enum: bool |
82 | 88 |
|
83 | | - def __init__(self) -> None: |
| 89 | + def __init__(self, strict_enum: bool = True) -> None: |
| 90 | + self.strict_enum = strict_enum |
84 | 91 | self._builders = { |
85 | 92 | DataType.INTEGER: self._integer, |
86 | 93 | DataType.NUMBER: self._number, |
@@ -116,39 +123,39 @@ def create(self, data: dict) -> Schema: |
116 | 123 |
|
117 | 124 | return builder_func(data) |
118 | 125 |
|
119 | | - @staticmethod |
120 | | - def _integer(data: dict) -> Integer: |
| 126 | + def _integer(self, data: dict) -> Integer: |
| 127 | + format_cast = IntegerFormat if self.strict_enum else LooseIntegerFormat |
121 | 128 | attrs_map = { |
122 | 129 | "multiple_of": PropertyMeta(name="multipleOf", cast=int), |
123 | 130 | "maximum": PropertyMeta(name="maximum", cast=int), |
124 | 131 | "exclusive_maximum": PropertyMeta(name="exclusiveMaximum", cast=int), |
125 | 132 | "minimum": PropertyMeta(name="minimum", cast=int), |
126 | 133 | "exclusive_minimum": PropertyMeta(name="exclusiveMinimum", cast=int), |
127 | | - "format": PropertyMeta(name="format", cast=IntegerFormat), |
| 134 | + "format": PropertyMeta(name="format", cast=format_cast), |
128 | 135 | } |
129 | 136 |
|
130 | 137 | return Integer(**extract_attrs(data, attrs_map)) |
131 | 138 |
|
132 | | - @staticmethod |
133 | | - def _number(data: dict) -> Number: |
| 139 | + def _number(self, data: dict) -> Number: |
| 140 | + format_cast = NumberFormat if self.strict_enum else LooseNumberFormat |
134 | 141 | attrs_map = { |
135 | 142 | "multiple_of": PropertyMeta(name="multipleOf", cast=float), |
136 | 143 | "maximum": PropertyMeta(name="maximum", cast=float), |
137 | 144 | "exclusive_maximum": PropertyMeta(name="exclusiveMaximum", cast=float), |
138 | 145 | "minimum": PropertyMeta(name="minimum", cast=float), |
139 | 146 | "exclusive_minimum": PropertyMeta(name="exclusiveMinimum", cast=float), |
140 | | - "format": PropertyMeta(name="format", cast=NumberFormat), |
| 147 | + "format": PropertyMeta(name="format", cast=format_cast), |
141 | 148 | } |
142 | 149 |
|
143 | 150 | return Number(**extract_attrs(data, attrs_map)) |
144 | 151 |
|
145 | | - @staticmethod |
146 | | - def _string(data: dict) -> String: |
| 152 | + def _string(self, data: dict) -> String: |
| 153 | + format_cast = StringFormat if self.strict_enum else LooseStringFormat |
147 | 154 | attrs_map = { |
148 | 155 | "max_length": PropertyMeta(name="maxLength", cast=int), |
149 | 156 | "min_length": PropertyMeta(name="minLength", cast=int), |
150 | 157 | "pattern": PropertyMeta(name="pattern", cast=None), |
151 | | - "format": PropertyMeta(name="format", cast=StringFormat), |
| 158 | + "format": PropertyMeta(name="format", cast=format_cast), |
152 | 159 | } |
153 | 160 |
|
154 | 161 | return String(**extract_attrs(data, attrs_map)) |
|
0 commit comments