jpmckinney/validictory

new version not allow a blank True string with format

Closed this issue · 4 comments

hi jamesturk,

I have a problem after update my validictoty lib from 0.8.3 to 1.1.1, while a string is blank, new version will validate format ,like this:

#test_blank.py
import validictory

interface_schema = {
'type': 'object',
'properties': {
'ip': {'type': 'string', 'blank': True,'format':'ip-address'},
'mask': {'type': 'string', 'blank': True,'format':'ip-address'},
},
'additionalProperties': False
}

test_data = {"ip":"","mask":""}
validictory.validate(test_data,interface_schema)

it work well in old version,but new version alert
Value '' for field '' is not a ip-address

I think the code "if format_validator and value is not None:" can write as 'if format_validator and value is not None and value != "":'

compare:
new version:
def validate_format(self, x, fieldname, schema, path, format_option=None):
'''
Validates the format of primitive data types
'''
value = x.get(fieldname, None)

    format_validator = self._format_validators.get(format_option, None)

    if format_validator and value is not None:
        try:
            format_validator(self, fieldname, value, format_option)
        except FieldValidationError as fve:
            if self.fail_fast:
                raise
            else:
                self._errors.append(fve)

old version:

def validate_format(self, x, fieldname, schema, format_option=None):
'''
Validates the format of primitive data types
'''
value = x.get(fieldname)

    format_validator = self._format_validators.get(format_option, None)

    if format_validator and value:
        format_validator(self, fieldname, value, format_option)

    # TODO: warn about unsupported format ?

would you check this issue?
wait for your reply.
thank you,best regard.

I have tried jsonschema-2.6.0, but it can't validate blank false and format of ip-address.

before we update validictory, we use the version 0.8.3, it can support ip value is "" while schema is 'blank':' True''format':'ip-adress', and does not alert 'Value '' for field '' is not a ip-address',
this is conform our expectation, because we some time don't input ip, and its value is "",we hope it's valid, and some time we input ip, we also hope validate its format.

or if you know some other validators, would you like to tell me, let me see how they validate, if all not support my requirement, I may have to rewrite my code.

thanks .

and I think if one need always validate ip-address, he can set blank false.

depending on your needs I see a few options:

  • stick w/ 0.8.3 if it is working for you
  • if you want to support this both jsonschema and validictory support custom validation overrides so you could make a formatter called 'blank-ip' or something similar, or even override the IP one
  • potentially you could alter your schema to not use the IP format but use a regex type validator for IP addresses

given the pending deprecation of validictory & fact this would be a backwards incompatible change and require a major version # bump I'm going to mark this as closed, but I hope one of the above is acceptable for your needs