2424else :
2525 _binary_types = (bytes , buffer )
2626
27+
2728class ValidationError (Exception ):
2829 """Raised when a value doesn't pass validation by its validator."""
2930
@@ -63,6 +64,7 @@ def __repr__(self):
6364 # Not a perfect repr, but includes the error location information.
6465 return 'ValidationError(%r)' % str (self )
6566
67+
6668def generic_type_name (v ):
6769 """Return a descriptive type name that isn't Python specific. For example,
6870 an int value will return 'integer' rather than 'int'."""
@@ -80,6 +82,7 @@ def generic_type_name(v):
8082 else :
8183 return type (v ).__name__
8284
85+
8386class Validator (object ):
8487 """All primitive and composite data types should be a subclass of this."""
8588 __metaclass__ = ABCMeta
@@ -99,17 +102,20 @@ def has_default(self):
99102 def get_default (self ):
100103 raise AssertionError ('No default available.' )
101104
105+
102106class Primitive (Validator ):
103107 """A basic type that is defined by Babel."""
104108 pass
105109
110+
106111class Boolean (Primitive ):
107112
108113 def validate (self , val ):
109114 if not isinstance (val , bool ):
110115 raise ValidationError ('%r is not a valid boolean' % val )
111116 return val
112117
118+
113119class Integer (Primitive ):
114120 """
115121 Do not use this class directly. Extend it and specify a 'minimum' and
@@ -150,22 +156,27 @@ def validate(self, val):
150156 def __repr__ (self ):
151157 return '%s()' % self .__class__ .__name__
152158
159+
153160class Int32 (Integer ):
154161 minimum = - 2 ** 31
155162 maximum = 2 ** 31 - 1
156163
164+
157165class UInt32 (Integer ):
158166 minimum = 0
159167 maximum = 2 ** 32 - 1
160168
169+
161170class Int64 (Integer ):
162171 minimum = - 2 ** 63
163172 maximum = 2 ** 63 - 1
164173
174+
165175class UInt64 (Integer ):
166176 minimum = 0
167177 maximum = 2 ** 64 - 1
168178
179+
169180class Real (Primitive ):
170181 """
171182 Do not use this class directly. Extend it and optionally set a 'minimum'
@@ -231,14 +242,17 @@ def validate(self, val):
231242 def __repr__ (self ):
232243 return '%s()' % self .__class__ .__name__
233244
245+
234246class Float32 (Real ):
235247 # Maximum and minimums from the IEEE 754-1985 standard
236248 minimum = - 3.40282 * 10 ** 38
237249 maximum = 3.40282 * 10 ** 38
238250
251+
239252class Float64 (Real ):
240253 pass
241254
255+
242256class String (Primitive ):
243257 """Represents a unicode string."""
244258
@@ -296,7 +310,8 @@ def validate(self, val):
296310 % (val , self .pattern ))
297311 return val
298312
299- class Binary (Primitive ):
313+
314+ class Bytes (Primitive ):
300315
301316 def __init__ (self , min_length = None , max_length = None ):
302317 if min_length is not None :
@@ -315,7 +330,7 @@ def __init__(self, min_length=None, max_length=None):
315330
316331 def validate (self , val ):
317332 if not isinstance (val , _binary_types ):
318- raise ValidationError ("expected binary type, got %s"
333+ raise ValidationError ("expected bytes type, got %s"
319334 % generic_type_name (val ))
320335 elif self .max_length is not None and len (val ) > self .max_length :
321336 raise ValidationError ("'%s' must have at most %d bytes, got %d"
@@ -325,6 +340,7 @@ def validate(self, val):
325340 % (val , self .min_length , len (val )))
326341 return val
327342
343+
328344class Timestamp (Primitive ):
329345 """Note that while a format is specified, it isn't used in validation
330346 since a native Python datetime object is preferred. The format, however,
@@ -446,6 +462,7 @@ def get_default(self):
446462 assert not self .definition ._has_required_fields , 'No default available.'
447463 return self .definition ()
448464
465+
449466class StructTree (Struct ):
450467 """Validator for structs with enumerated subtypes.
451468
@@ -456,6 +473,7 @@ class StructTree(Struct):
456473 def __init__ (self , definition ):
457474 super (StructTree , self ).__init__ (definition )
458475
476+
459477class Union (Composite ):
460478
461479 def __init__ (self , definition ):
@@ -498,6 +516,7 @@ def validate_type_only(self, val):
498516 raise ValidationError ('expected type %s or subtype, got %s' %
499517 (self .definition .__name__ , generic_type_name (val )))
500518
519+
501520class Void (Primitive ):
502521
503522 def validate (self , val ):
@@ -511,6 +530,7 @@ def has_default(self):
511530 def get_default (self ):
512531 return None
513532
533+
514534class Nullable (Validator ):
515535
516536 def __init__ (self , validator ):
0 commit comments