def validate_payload(payload, api_model, check_required=True):
 if check_required:
  for key in api_model:
   if (api_model[key].required and (key not in payload)):
    raise ValidationError(field=key, message="Required field '{}' missing".format(key))
 for key in payload:
  field = api_model[key]
  if isinstance(field, fields.List):
   field = field.container
   data = payload[key]
  elif isinstance(field, fields.Nested):
   if payload[key]:
    validate_payload(payload[key], field.model)
  else:
   data = [payload[key]]
  if (isinstance(field, CustomField) and hasattr(field, 'validate')):
   field.payload = payload
   for i in data:
    if (not field.validate(i)):
     raise ValidationError(field=key, message=(field.validation_error % ("'%s'" % key)))

def validate_payload ( payload , api_model , check_required = True ) : if check_required : for key in api_model : if ( api_model [ key ] . required and ( key not in payload ) ) : raise ValidationError ( field = key , message = "Required field '{}' missing" . format ( key ) ) for key in payload : field = api_model [ key ] if isinstance ( field , fields . List ) : field = field . container data = payload [ key ] elif isinstance ( field , fields . Nested ) : if payload [ key ] : validate_payload ( payload [ key ] , field . model ) else : data = [ payload [ key ] ] if ( isinstance ( field , CustomField ) and hasattr ( field , 'validate' ) ) : field . payload = payload for i in data : if ( not field . validate ( i ) ) : raise ValidationError ( field = key , message = ( field . validation_error % ( "'%s'" % key ) ) )
validate payload against an api_model .