|
20 | 20 | )
|
21 | 21 | from openapi_core.schema.schemas.util import (
|
22 | 22 | forcebool, format_date, format_datetime, format_byte, format_uuid,
|
| 23 | + format_number, |
23 | 24 | )
|
24 | 25 | from openapi_core.schema.schemas.validators import (
|
25 | 26 | TypeValidator, AttributeValidator,
|
@@ -50,11 +51,19 @@ class Schema(object):
|
50 | 51 | SchemaFormat.BYTE: Format(format_byte, TypeValidator(text_type)),
|
51 | 52 | }
|
52 | 53 |
|
| 54 | + NUMBER_FORMAT_CALLABLE_GETTER = { |
| 55 | + SchemaFormat.NONE: Format(format_number, TypeValidator( |
| 56 | + integer_types + (float, ), exclude=bool)), |
| 57 | + SchemaFormat.FLOAT: Format(float, TypeValidator(float)), |
| 58 | + SchemaFormat.DOUBLE: Format(float, TypeValidator(float)), |
| 59 | + } |
| 60 | + |
53 | 61 | TYPE_VALIDATOR_CALLABLE_GETTER = {
|
54 | 62 | SchemaType.ANY: lambda x: True,
|
55 | 63 | SchemaType.BOOLEAN: TypeValidator(bool),
|
56 | 64 | SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool),
|
57 |
| - SchemaType.NUMBER: TypeValidator(integer_types + (float, ), exclude=bool), |
| 65 | + SchemaType.NUMBER: TypeValidator( |
| 66 | + integer_types + (float, ), exclude=bool), |
58 | 67 | SchemaType.STRING: TypeValidator(
|
59 | 68 | text_type, date, datetime, binary_type, UUID),
|
60 | 69 | SchemaType.ARRAY: TypeValidator(list, tuple),
|
@@ -237,7 +246,24 @@ def _unmarshal_number(self, value, custom_formatters=None, strict=True):
|
237 | 246 | if strict and not isinstance(value, (float, ) + integer_types):
|
238 | 247 | raise InvalidSchemaValue("Value {value} is not of type {type}", value, self.type)
|
239 | 248 |
|
240 |
| - return value |
| 249 | + try: |
| 250 | + schema_format = SchemaFormat(self.format) |
| 251 | + except ValueError: |
| 252 | + msg = "Unsupported format {type} unmarshalling for value {value}" |
| 253 | + if custom_formatters is not None: |
| 254 | + formatnumber = custom_formatters.get(self.format) |
| 255 | + if formatnumber is None: |
| 256 | + raise InvalidSchemaValue(msg, value, self.format) |
| 257 | + else: |
| 258 | + raise InvalidSchemaValue(msg, value, self.format) |
| 259 | + else: |
| 260 | + formatnumber = self.NUMBER_FORMAT_CALLABLE_GETTER[schema_format] |
| 261 | + |
| 262 | + try: |
| 263 | + return formatnumber.unmarshal(value) |
| 264 | + except ValueError as exc: |
| 265 | + raise InvalidCustomFormatSchemaValue( |
| 266 | + "Failed to format value {value} to format {type}: {exception}", value, self.format, exc) |
241 | 267 |
|
242 | 268 | def _unmarshal_boolean(self, value, custom_formatters=None, strict=True):
|
243 | 269 | if strict and not isinstance(value, (bool, )):
|
|
0 commit comments