roskakori / cutplace

validate data stored in CSV, PRN, ODS or Excel files
http://cutplace.readthedocs.org/
GNU Lesser General Public License v3.0
18 stars 20 forks source link

Consider length for valid Integer range #80

Closed roskakori closed 9 years ago

roskakori commented 9 years ago

Goal: the range derived from a possible length becomes part of the rule for considering the range in IntegerFieldFormat:

length   rule      actual rule
                   -2147483648...2147483647
         1...99    1...99
4                  -999...9999
4        1...99    1..99
2        1...999   length_range = Range('-9..99')
                   rule_range = Range('1...999')
                   if length_range.upper < rule_range.upper --> error
                   if length_range.lower > rule_range.lower --> error   
roskakori commented 9 years ago

There is a code segment in cid.py to validate the field length for fixed format data, which probably can be simplified once Range supports an upper and lower property. In particular the variable field_length_is_broken seems bogus.

   # Validate field length for fixed format.
   if self._data_format.format == data.FORMAT_FIXED:
       self._location.set_cell(4)
       if field_format.length.items:
           field_length_is_broken = True
           if len(field_format.length.items) == 1:
               (lower, upper) = field_format.length.items[0]
               if lower == upper:
                   if lower < 1:
                       raise errors.InterfaceError(
                           "length of field %r for fixed data format must be at least 1 but is: %s"
                           % (field_name, field_format.length), self._location)
                   field_length_is_broken = False
           if field_length_is_broken:
               raise errors.InterfaceError(
                   "length of field %r for fixed data format must be a single value but is: %s"
                   % (field_name, field_format.length), self._location)
       else:
           raise errors.InterfaceError(
               "length of field %r must be specified with fixed data format" % field_name, self._location)
roskakori commented 9 years ago

@butterhirsch: I noticed that the initial implementation did not point out inconsistencies concerning the lower limit of a range, for example: length='3', rule='1234...'

During fixing this I changed the consistency check to simply traverse the lower and upper limits of the rule and check the character length of each of them. Therefore the derived length_range is not needed anymore. Also any errors detected by validating against the length range are already detected by validating against the length before, so currently IntergerFieldFormat has no use whatsoever for the derived length range. So I suppose we can remove it.

Nevertheless there still is a use for ranges.create_range_from_length() to derive a SQL constraint for integer fields with a length but without a rule.