예제 #1
0
 def validation_error(self, value):
     """
     Return an error message if the value is not valid according to the schema.
     It relies on exception thrown by the 'cast1 method of Type method.
     :param value:
     :return: None if value is valid or an error message string
     """
     error = None
     # override the integer validation. The default message is a bit cryptic if there's an error casting a string
     # like '1.2' into an int.
     if isinstance(self.type, types.IntegerType):
         if not is_blank_value(value):
             not_integer = False
             try:
                 casted = self.cast(value)
                 # there's also the case where the case where a float 1.2 is successfully casted in 1
                 # (ex: int(1.2) = 1)
                 if str(casted) != str(value):
                     not_integer = True
             except Exception:
                 not_integer = True
             if not_integer:
                 return 'The field "{}" must be a whole number.'.format(self.name)
     try:
         self.cast(value)
     except Exception as e:
         error = e.message
     return error
예제 #2
0
 def validation_error(self, value):
     """
     Return an error message if the value is not valid according to the schema.
     It relies on exception thrown by the 'cast1 method of Type method.
     :param value:
     :return: None if value is valid or an error message string
     """
     error = None
     # override the integer validation. The default message is a bit cryptic if there's an error casting a string
     # like '1.2' into an int.
     if isinstance(self.type, types.IntegerType):
         if not is_blank_value(value):
             not_integer = False
             try:
                 casted = self.cast(value)
                 # there's also the case where the case where a float 1.2 is successfully casted in 1
                 # (ex: int(1.2) = 1)
                 if str(casted) != str(value):
                     not_integer = True
             except Exception:
                 not_integer = True
             if not_integer:
                 return 'The field "{}" must be a whole number.'.format(
                     self.name)
     try:
         self.cast(value)
     except Exception as e:
         error = e.message
     return error
예제 #3
0
 def validation_error(self, value):
     """
     Return an error message if the value is not valid according to the schema.
     It relies on exception thrown by the 'cast1 method of Type method.
     :param value:
     :return: None if value is valid or an error message string
     """
     error = None
     # override the integer validation. The default message is a bit cryptic if there's an error casting a string
     # like '1.2' into an int.
     if isinstance(self.type, types.IntegerType):
         if not is_blank_value(value):
             not_integer = False
             try:
                 casted = self.cast(value)
                 # there's also the case where the case where a float 1.2 is successfully casted in 1
                 # (ex: int(1.2) = 1)
                 if str(casted) != str(value):
                     not_integer = True
             except Exception:
                 not_integer = True
             if not_integer:
                 return 'The field "{}" must be a whole number.'.format(self.name)
     try:
         self.cast(value)
     except Exception as e:
         error = "{}".format(e)
         # Override the default enum exception message to include all possible values
         if error.find('enum array') and self.constraints.enum:
             values = [str(v) for v in self.constraints.enum]
             error = "The value must be one the following: {}".format(values)
     return error
예제 #4
0
 def cast(self, value):
     """
     Returns a native Python object of the expected format. Will throw an exception
     if the value doesn't complies with any constraints. See for details:
     https://github.com/frictionlessdata/jsontableschema-py#types
     This method is mainly a helper for the validation_error
     :param value:
     :return:
     """
     if is_blank_value(value):
         # must do that because an empty string is considered as valid even if required by the StringType
         value = None
     if isinstance(value, basestring):
         # the StringType accepts only unicode
         value = unicode(value)
     return self.type.cast(value)
예제 #5
0
 def cast(self, value):
     """
     Returns a native Python object of the expected format. Will throw an exception
     if the value doesn't complies with any constraints. See for details:
     https://github.com/frictionlessdata/jsontableschema-py#types
     This method is mainly a helper for the validation_error
     :param value:
     :return:
     """
     if is_blank_value(value):
         # must do that because an empty string is considered as valid even if required by the StringType
         value = None
     if isinstance(value, basestring):
         # the StringType accepts only unicode
         value = unicode(value)
     return self.type.cast(value)