11"""OpenAPI core schemas module"""
22import logging
33from collections import defaultdict
4+ import warnings
5+
46from distutils .util import strtobool
57from functools import lru_cache
68
911
1012from openapi_core .exceptions import (
1113 InvalidValueType , UndefinedSchemaProperty , MissingPropertyError ,
14+ InvalidValue ,
1215)
1316from openapi_core .models import ModelFactory
1417
@@ -26,7 +29,8 @@ class Schema(object):
2629
2730 def __init__ (
2831 self , schema_type , model = None , properties = None , items = None ,
29- spec_format = None , required = False , default = None , nullable = False ):
32+ spec_format = None , required = False , default = None , nullable = False ,
33+ enum = None ):
3034 self .type = schema_type
3135 self .model = model
3236 self .properties = properties and dict (properties ) or {}
@@ -35,6 +39,7 @@ def __init__(
3539 self .required = required
3640 self .default = default
3741 self .nullable = nullable
42+ self .enum = enum
3843
3944 def __getitem__ (self , name ):
4045 return self .properties [name ]
@@ -77,6 +82,11 @@ def unmarshal(self, value):
7782 if casted is None and not self .required :
7883 return None
7984
85+ if self .enum and casted not in self .enum :
86+ raise InvalidValue (
87+ "Value of %s not in enum choices: %s" , value , str (self .enum ),
88+ )
89+
8090 return casted
8191
8292 def _unmarshal_collection (self , value ):
@@ -138,6 +148,7 @@ def create(self, schema_spec):
138148 properties_spec = schema_deref .get ('properties' , None )
139149 items_spec = schema_deref .get ('items' , None )
140150 nullable = schema_deref .get ('nullable' , False )
151+ enum = schema_deref .get ('enum' , None )
141152
142153 properties = None
143154 if properties_spec :
@@ -149,7 +160,7 @@ def create(self, schema_spec):
149160
150161 return Schema (
151162 schema_type , model = model , properties = properties , items = items ,
152- required = required , nullable = nullable ,
163+ required = required , nullable = nullable , enum = enum ,
153164 )
154165
155166 @property
0 commit comments