Creating a custom type with a custom validation function

1,505 views
Skip to first unread message

Paul Winkler

unread,
May 5, 2015, 2:20:42 AM5/5/15
to jsons...@googlegroups.com
Hi folks,

I have been trying to figure out how to declare a custom type so that I can write a validation function.
I've been scratching my head over `extends` and getting nowhere.

The goal is to be able to first define a custom type definition in my schema, and then register a validation function that's triggered on that type:

    'properties': {
        "foo_daterange": {
            "$ref": "#/definitions/dateRange"
        },
    },

    "definitions": {
        "dateRange": {
            "properties": {
                "from": {
                    "type": "string",
                    "format": "date",
                },
                "to": {
                    "type": "string",
                    "format": "date",
                },
            },
            "required": ["to", "from"],
        },
    },


So far so good, this part seems to work OK.
But then I want to write, and register on an extended Validator, a validation function which asserts eg. that a dateRange instance has "from" less than "to".
So that this document would raise a validation error because "from" is greater than "to":

example = {
    "foo_daterange": {
        "from": "2014-11-23",
        "to": "2014-11-20",
    },
}

I have been staring at the 'extends' documentation and I'm stumped. Has anyone got an example of this sort of thing?  Here's what I've tried:

def date_range_validator(validator, value, instance, schema):
    import pdb; pdb.set_trace()
    start = value['from_date']
    end = value['to_date']
    import datetime
    start = datetime.datetime.strptime(start, '%Y%m%d')
    end = datetime.strptime(end, '%Y%m%d')
    assert start <= end


new_validators = {
    'dateRange': date_range_validator,
}

MyValidator = validators.extend(
    Draft4Validator,
    validators=new_validators)

# If I were doing it right, this would hit pdb ... but it doesn't.
MyValidator(example).validate() 


Julian Berman

unread,
May 6, 2015, 9:55:59 AM5/6/15
to Paul Winkler, jsons...@googlegroups.com

Hey!

So you're defining a dateRange validator but you never use it :) which is why your function isn't called.

You have a $ref which is basically just a simple form of inlining, so you can just imagine the result which just inlines your definition, but all that it will have is a properties and a required validator inside.

Is your end goal to write some schemas that make use of a dateRange validator, or just to validate some from and to keys that you have in your schema?

Cheers,
Julian

--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages