sunlightlabs/validictory — pull request #17 by aconrad (2012-02-24 04:18:50+00:00)
Title: "ip-address" validator

I have implemented an IP address validator. Please pull.

Implementation notes:

The way the validator method name is resolved requires to have a .fieldtype.replace("-", "_") so "ip-address" becomes "ip_address", thus "self.validate_type_ip_address" can work.

I tried to move all validator methods as top-level module functions, and register them such as:

TYPE_CHECKERS = {'string': validate_type_string,
'integer': validate_type_integer,
'number': validate_type_number,
'boolean': validate_type_boolean,
'object': validate_type_object,
'array': validate_type_array,
'null': validate_type_null,
'ip-address': validate_type_ip_address,
'any': validate_type_any}

The problem is that it breaks custom validators as they extend the SchemaValidator class, such as:

DateValidator(validictory.validator.SchemaValidator): ...

So I thought about implementing a method on the SchemaValidator class, that would work like this:

def my_random_validator(val):
return random.choice([True, False])

validictory.register_validator("random", my_random_validator)

That would basically insert the key "random" in the TYPE_CHECKERS dictionary, along with the appropriate validator.

But that would still break the tests, so I did not implement it. Unless there's another way I am not aware of...

Thanks,
Alex