jpmckinney / validictory

🎓 deprecated general purpose python data validator
Other
240 stars 57 forks source link

How to validate non-required data #105

Closed sldhana-zz closed 7 years ago

sldhana-zz commented 8 years ago

I have a schema like this. The secondary_email should only be validated if it contains anything other than an blank value. But with this schema, the pattern is always being used to validate a blank value and it fails validation.

data = {"testing": "", 'email': 'yoo@y.com', 'secondary_email': ''}
schema = {
    "properties": {
        "email": {
            "required": True,
            'pattern': "^[a-zA-Z0-9'\-_\+]+(?:[\.][a-zA-Z0-9'\-_\+]+)*@([a-zA-Z0-9]([-]?[a-zA-Z0-9])*\.)*[a-zA-Z0-9]([-]?[a-zA-Z0-9]){0,61}[a-zA-Z0-9]{0,1}\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$"
        },
        "secondary_email": {
            'type': ['string', 'null'],
            'blank': True,
            'required': False,
            'pattern': "^[a-zA-Z0-9'\-_\+]+(?:[\.][a-zA-Z0-9'\-_\+]+)*@([a-zA-Z0-9]([-]?[a-zA-Z0-9])*\.)*[a-zA-Z0-9]([-]?[a-zA-Z0-9]){0,61}[a-zA-Z0-9]{0,1}\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$"
        },
        "testing": {
            "required": False,
            "blank": True # fails
        }
    }
}

Next approach was to do this:

data = {"testing": "", 'email': 'yoo@y.com', 'secondary_email': ''}
schema = {
    "properties": {
        "email": {
            "required": True,
            'pattern': "^[a-zA-Z0-9'\-_\+]+(?:[\.][a-zA-Z0-9'\-_\+]+)*@([a-zA-Z0-9]([-]?[a-zA-Z0-9])*\.)*[a-zA-Z0-9]([-]?[a-zA-Z0-9]){0,61}[a-zA-Z0-9]{0,1}\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$"
        },
        "secondary_email": {
            'type': ['string', 'null'],
            'blank': True,
            'required': False,
            'pattern': "^[a-zA-Z0-9'\-_\+]+(?:[\.][a-zA-Z0-9'\-_\+]+)*@([a-zA-Z0-9]([-]?[a-zA-Z0-9])*\.)*[a-zA-Z0-9]([-]?[a-zA-Z0-9]){0,61}[a-zA-Z0-9]{0,1}\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$"
        },
        "testing": {
            "required": False,
            "blank": True # fails
        }
    }
}

I then have a custom email function that checks the format_option and determine whether it contains the word optional or not. If optional, I then only validate if value is not empty. Although this works, this solution doesn't seem very elegant. Am I missing something that I could do much simpler?

jamesturk commented 8 years ago

Hmm, if pattern is present it is validated... I see the case for making blank override this but I'm not sure if that'd break compatibility w/ JSON schema. Is it an option to make your secondary regex accept blank strings as well?

sldhana-zz commented 8 years ago

@jamesturk - seems like then I have to specify the regex differently for each optional field. I am trying to do this with dynamic form generation. Could you explain the compatibility with JSON schema? Is it because each key should be independent and not rely on each other?

jamesturk commented 8 years ago

That's fair. I'll try and figure out which is expected behavior from JSON schema's point of view (it's basically a question of precedence, IIRC this might be vague in the draft spec)

On Tue, Sep 6, 2016 at 8:02 PM, sldhana notifications@github.com wrote:

@jamesturk https://github.com/jamesturk - seems like then I have to specify the regex differently for each optional field. I am trying to do this with dynamic form generation. Could you explain the compatibility with JSON schema? Is it because each key should be independent and not rely on each other?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jamesturk/validictory/issues/105#issuecomment-245133700, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAfYiXBnT8kavRY80yXSw7oCr4gfQf1ks5qnf79gaJpZM4J0qmW .

jamesturk commented 7 years ago

closed in light of #114