예제 #1
0
def _check_enum(parameter_name, value, parameter_config):
    """Checks if an enum value is valid.

  This is called by the transform_parameter_value function and shouldn't be
  called directly.

  This verifies that the value of an enum parameter is valid.

  Args:
    parameter_name: A string containing the name of the parameter, which is
      either just a variable name or the name with the index appended. For
      example 'var' or 'var[2]'.
    value: A string containing the value passed in for the parameter.
    parameter_config: The dictionary containing information specific to the
      parameter in question. This is retrieved from request.parameters in
      the method config.

  Raises:
    EnumRejectionError: If the given value is not among the accepted
      enum values in the field parameter.
  """
    enum_values = [
        enum['backendValue']
        for enum in list(parameter_config['enum'].values())
        if 'backendValue' in enum
    ]
    if value not in enum_values:
        raise errors.EnumRejectionError(parameter_name, value, enum_values)
예제 #2
0
    def _check_enum(self, parameter_name, value, field_parameter):
        """Checks if the parameter value is valid if an enum.

    If the parameter is not an enum, does nothing. If it is, verifies that
    its value is valid.

    Args:
      parameter_name: A string containing the name of the parameter, which is
        either just a variable name or the name with the index appended. For
        example 'var' or 'var[2]'.
      value: A string or list of strings containing the value(s) to be used as
        enum(s) for the parameter.
      field_parameter: The dictionary containing information specific to the
        field in question. This is retrieved from request.parameters in the
        method config.

    Raises:
      EnumRejectionError: If the given value is not among the accepted
        enum values in the field parameter.
    """
        if 'enum' not in field_parameter:
            return

        enum_values = [
            enum['backendValue'] for enum in field_parameter['enum'].values()
            if 'backendValue' in enum
        ]
        if value not in enum_values:
            raise errors.EnumRejectionError(parameter_name, value, enum_values)