labpack/parsing/flask.py
def validate_request_content(request_content, request_model, request_component='body'):
    
    '''
        a method to validate the content fields of a flask request
        
    :param request_content: dictionary with content fields to validate 
    :param request_model: object with jsonmodel class properties
    :param request_component: string with name of component of request evaluated
    :return: dictionary with validation status details
    '''

# import dependencies
    from jsonmodel.validators import jsonModel
    from jsonmodel.exceptions import InputValidationError

    title = 'validate_request_content'

# validate inputs
    if not isinstance(request_content, dict):
        raise TypeError('%s(request_content={...}) must be a dictionary.' % title)
    elif not isinstance(request_model, jsonModel):
        raise TypeError('%s(request_model=<...>) must be a %s object.' % (title, jsonModel.__class__))

# construct default status details
    status_details = {
        'code': 200,
        'error': ''
    }

# validate request details
    try:
        request_model.validate(request_content)
    except InputValidationError as err:
        if err.error['input_criteria']['required_field'] and err.error['failed_test'] == 'required_field':
            required_field = err.error['input_path']
            if err.error['input_criteria']['value_datatype'] == 'map':
                if isinstance(err.error['error_value'], list):
                    required_field = err.error['error_value'][0]
            error_message = 'request %s is missing required field %s' % (request_component, required_field)
        elif err.error['input_criteria']['required_field'] and err.error['failed_test'] == 'extra_fields':
            path_insert = ''
            path_prefix = ''
            if len(err.error['input_path']) > 1:
                path_prefix = 'value for '
                path_insert = ' path %s' % err.error['input_path']
            from labpack.parsing.grammar import join_words
            path_plural = ''
            if len(err.error['error_value']) > 1:
                path_plural = 's'
            error_message = '%srequest %s%s may not contain field%s %s.' % (path_prefix, request_component, path_insert, path_plural, join_words(err.error['error_value'], operator='disjunction', quotes=True)) 
        else:
            failed_key = err.error['failed_test']
            error_value = ''
            if err.error['error_value']:
                error_value = '%s ' % err.error['error_value']
            error_message = '%svalue for request %s field %s is invalid. %s must be %s' % (error_value, request_component, err.error['input_path'], err.error['failed_test'], err.error['input_criteria'][failed_key])
        status_details['error'] = error_message
        status_details['code'] = 400
    except:
        status_details['error'] = 'request %s fields are invalid.' % request_component
        status_details['error'] = 400

    return status_details

        a method to validate the content fields of a flask request
        
    :param request_content: dictionary with content fields to validate 
    :param request_model: object with jsonmodel class properties
    :param request_component: string with name of component of request evaluated
    :return: dictionary with validation status details
    
a method to validate the content fields of a flask request
a method to validate the content fields of a flask request
def validate_request_content(request_content, request_model, request_component='body'):
    from jsonmodel.validators import jsonModel
    from jsonmodel.exceptions import InputValidationError
    title = 'validate_request_content'
    if not isinstance(request_content, dict):
        raise TypeError('%s(request_content={...}) must be a dictionary.' % title)
    elif not isinstance(request_model, jsonModel):
        raise TypeError('%s(request_model=<...>) must be a %s object.' % (title, jsonModel.__class__))
    status_details = {
        'code': 200,
        'error': ''
    }
    try:
        request_model.validate(request_content)
    except InputValidationError as err:
        if err.error['input_criteria']['required_field'] and err.error['failed_test'] == 'required_field':
            required_field = err.error['input_path']
            if err.error['input_criteria']['value_datatype'] == 'map':
                if isinstance(err.error['error_value'], list):
                    required_field = err.error['error_value'][0]
            error_message = 'request %s is missing required field %s' % (request_component, required_field)
        elif err.error['input_criteria']['required_field'] and err.error['failed_test'] == 'extra_fields':
            path_insert = ''
            path_prefix = ''
            if len(err.error['input_path']) > 1:
                path_prefix = 'value for '
                path_insert = ' path %s' % err.error['input_path']
            from labpack.parsing.grammar import join_words
            path_plural = ''
            if len(err.error['error_value']) > 1:
                path_plural = 's'
            error_message = '%srequest %s%s may not contain field%s %s.' % (path_prefix, request_component, path_insert, path_plural, join_words(err.error['error_value'], operator='disjunction', quotes=True)) 
        else:
            failed_key = err.error['failed_test']
            error_value = ''
            if err.error['error_value']:
                error_value = '%s ' % err.error['error_value']
            error_message = '%svalue for request %s field %s is invalid. %s must be %s' % (error_value, request_component, err.error['input_path'], err.error['failed_test'], err.error['input_criteria'][failed_key])
        status_details['error'] = error_message
        status_details['code'] = 400
    except:
        status_details['error'] = 'request %s fields are invalid.' % request_component
        status_details['error'] = 400
    return status_details
def validate_request_content ( request_content , request_model , request_component = 'body' ) : from jsonmodel . validators import jsonModel from jsonmodel . exceptions import InputValidationError title = 'validate_request_content' if not isinstance ( request_content , dict ) : raise TypeError ( '%s(request_content={...}) must be a dictionary.' % title ) elif not isinstance ( request_model , jsonModel ) : raise TypeError ( '%s(request_model=<...>) must be a %s object.' % ( title , jsonModel . __class__ ) ) status_details = { 'code' : 200 , 'error' : '' } try : request_model . validate ( request_content ) except InputValidationError as err : if err . error [ 'input_criteria' ] [ 'required_field' ] and err . error [ 'failed_test' ] == 'required_field' : required_field = err . error [ 'input_path' ] if err . error [ 'input_criteria' ] [ 'value_datatype' ] == 'map' : if isinstance ( err . error [ 'error_value' ] , list ) : required_field = err . error [ 'error_value' ] [ 0 ] error_message = 'request %s is missing required field %s' % ( request_component , required_field ) elif err . error [ 'input_criteria' ] [ 'required_field' ] and err . error [ 'failed_test' ] == 'extra_fields' : path_insert = '' path_prefix = '' if len ( err . error [ 'input_path' ] ) > 1 : path_prefix = 'value for ' path_insert = ' path %s' % err . error [ 'input_path' ] from labpack . parsing . grammar import join_words path_plural = '' if len ( err . error [ 'error_value' ] ) > 1 : path_plural = 's' error_message = '%srequest %s%s may not contain field%s %s.' % ( path_prefix , request_component , path_insert , path_plural , join_words ( err . error [ 'error_value' ] , operator = 'disjunction' , quotes = True ) ) else : failed_key = err . error [ 'failed_test' ] error_value = '' if err . error [ 'error_value' ] : error_value = '%s ' % err . error [ 'error_value' ] error_message = '%svalue for request %s field %s is invalid. %s must be %s' % ( error_value , request_component , err . error [ 'input_path' ] , err . error [ 'failed_test' ] , err . error [ 'input_criteria' ] [ failed_key ] ) status_details [ 'error' ] = error_message status_details [ 'code' ] = 400 except : status_details [ 'error' ] = 'request %s fields are invalid.' % request_component status_details [ 'error' ] = 400 return status_details
a method to validate the content fields of a flask request
a method to validate the content fields of a flask request
'''
        a method to validate the content fields of a flask request
        
    :param request_content: dictionary with content fields to validate 
    :param request_model: object with jsonmodel class properties
    :param request_component: string with name of component of request evaluated
    :return: dictionary with validation status details
    ''' # import dependencies
 # validate inputs
 # construct default status details
 # validate request details

dictionary with validation status details
dictionary with validation status details
request_content
dictionary with content fields to validate
dictionary with content fields to validate
request_model
object with jsonmodel class properties
object with jsonmodel class properties
request_component
string with name of component of request evaluated
string with name of component of request evaluated