#NEWLINE# def validate_document( self , document ) : #NEWLINE# #INDENT# self . schema = document . schema #NEWLINE# ( collection_name , errors ) = ( type ( document ) . __name__ , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# self . validate ( document . document ) #NEWLINE# for ( key , _errors ) in self . flattened_errors . items ( ) : #NEWLINE# #INDENT# field = key #NEWLINE# field_schema = self . get_field_schema ( field ) #NEWLINE# _errors = ( [ _errors ] if ( not isinstance ( _errors , list ) ) else _errors ) #NEWLINE# for _error in _errors : #NEWLINE# #INDENT# error = None #NEWLINE# if ( _error == 'unknown field' ) : #NEWLINE# #INDENT# error = UnknownFieldError ( collection_name , field ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( _error == 'required field' ) : #NEWLINE# #INDENT# error = RequiredFieldError ( collection_name , field ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( _error == 'field is read-only' ) : #NEWLINE# #INDENT# error = ReadOnlyFieldError ( collection_name , field ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( _error . startswith ( 'must be ' ) and _error . endswith ( 'type' ) ) : #NEWLINE# #INDENT# type_or_types = field_schema [ 'type' ] #NEWLINE# if isinstance ( type_or_types , list ) : #NEWLINE# #INDENT# type_or_types = ' or ' . join ( type_or_types ) #NEWLINE# #UNINDENT# error = FieldTypeError ( collection_name , field , type_or_types ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'does not match regex' in _error ) : #NEWLINE# #INDENT# error = RegexMatchError ( collection_name , field , _error . split ( "match regex '" ) [ 1 ] [ : ( - 1 ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'min length is' ) : #NEWLINE# #INDENT# error = MinLengthError ( collection_name , field , field_schema [ 'minlength' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'max length is' ) : #NEWLINE# #INDENT# error = MaxLengthError ( collection_name , field , field_schema [ 'maxlength' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'length of ' ) : #NEWLINE# #INDENT# error = LengthError ( collection_name , field , len ( field_schema [ 'items' ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'unallowed value ' ) : #NEWLINE# #INDENT# error = ValueNotAllowedError ( collection_name , field , _error . split ( 'unallowed value ' ) [ 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'unallowed values ' ) : #NEWLINE# #INDENT# error = ValuesNotAllowedError ( collection_name , field , _error . split ( 'unallowed values ' ) [ 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'min value is ' ) : #NEWLINE# #INDENT# error = MinValueError ( collection_name , field , field_schema [ 'min' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _error . startswith ( 'max value is ' ) : #NEWLINE# #INDENT# error = MaxValueError ( collection_name , field , field_schema [ 'max' ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( error and isinstance ( error , SchemaValidationError ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# errors [ 'errors' ] . append ( error ) #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# errors = DocumentValidationError ( collection_name , self . schema , document . document , [ error ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if errors : #NEWLINE# #INDENT# if ( 'errors' in errors ) : #NEWLINE# #INDENT# errors [ 'errors' ] = sorted ( errors [ 'errors' ] , key = ( lambda e : e [ 'field' ] ) ) #NEWLINE# #UNINDENT# document . _errors = errors #NEWLINE# return False #NEWLINE# #UNINDENT# return True