Skip to content

Commit 8182e61

Browse files
committed
byte string format
1 parent 2311829 commit 8182e61

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

openapi_core/schema/schemas/models.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import attr
33
import functools
44
import logging
5+
from base64 import b64decode, b64encode
56
from collections import defaultdict
67
from datetime import date, datetime
8+
from uuid import UUID
79
import re
810
import warnings
911

1012
from six import iteritems, integer_types, binary_type, text_type
11-
from uuid import UUID
1213

1314
from openapi_core.extensions.models.factories import ModelFactory
1415
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
@@ -21,7 +22,7 @@
2122
forcebool, format_date, format_datetime,
2223
)
2324
from openapi_core.schema.schemas.validators import (
24-
TypeValidator, AttributeValidator,
25+
TypeValidator, AttributeValidator, DecodeEncodeValidator,
2526
)
2627

2728
log = logging.getLogger(__name__)
@@ -44,10 +45,13 @@ class Schema(object):
4445

4546
STRING_FORMAT_CALLABLE_GETTER = {
4647
SchemaFormat.NONE: Format(text_type, TypeValidator(text_type)),
47-
SchemaFormat.DATE: Format(format_date, TypeValidator(date, exclude=datetime)),
48+
SchemaFormat.DATE: Format(
49+
format_date, TypeValidator(date, exclude=datetime)),
4850
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
4951
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
5052
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
53+
SchemaFormat.BYTE: Format(
54+
b64decode, DecodeEncodeValidator(b64decode, b64encode))
5155
}
5256

5357
TYPE_VALIDATOR_CALLABLE_GETTER = {

openapi_core/schema/schemas/validators.py

+13
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,16 @@ def __call__(self, value):
2424
return False
2525

2626
return True
27+
28+
29+
class DecodeEncodeValidator(object):
30+
31+
def __init__(self, decode_callable, encode_callable):
32+
self.decode_callable = decode_callable
33+
self.encode_callable = encode_callable
34+
35+
def __call__(self, value):
36+
try:
37+
return self.encode_callable(self.decode_callable(value)) == value
38+
except:
39+
return False

tests/unit/schema/test_schemas.py

+19
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,25 @@ def test_string_format_binary(self, value):
478478

479479
assert result == value
480480

481+
@pytest.mark.parametrize('value', [
482+
u('tsssst'), b('tsssst'), u('dGVzdA=='),
483+
])
484+
def test_string_format_byte_invalid(self, value):
485+
schema = Schema('string', schema_format='byte')
486+
487+
with pytest.raises(OpenAPISchemaError):
488+
schema.validate(value)
489+
490+
@pytest.mark.parametrize('value', [
491+
b('dGVzdA=='),
492+
])
493+
def test_string_format_byte(self, value):
494+
schema = Schema('string', schema_format='byte')
495+
496+
result =schema.validate(value)
497+
498+
assert result == value
499+
481500
@pytest.mark.parametrize('value', [
482501
u('test'), b('stream'), datetime.date(1989, 1, 2),
483502
datetime.datetime(1989, 1, 2, 0, 0, 0),

0 commit comments

Comments
 (0)