Skip to content

Instantly share code, notes, and snippets.

@theorm
Created November 26, 2012 01:44
Show Gist options
  • Save theorm/4146165 to your computer and use it in GitHub Desktop.
Save theorm/4146165 to your computer and use it in GitHub Desktop.
class Location(Document):
structure = {
"ll" : validate_coordinates,
"street" : ...
...
}
class Venue(Document):
structure = {
"location" : Location.validate,
"name" : unicode,
...
}
@property
def location(self):
# should be cached
return Location(self['location'])
@benneic
Copy link

benneic commented Nov 26, 2012

Or maybe use a custom type as the some embedded element for custom validation and the rest is just an dict structure...

class LongitudeLatitude(CustomType):
    mongo_type = list
    python_type = list
    init_type = [0,0]

    def to_bson(self, value):
        return value

    def to_python(self, value):
        return value

    def validate(self, value, path):
        if len(value) != 2 or not value[0] or not value[1]:
            raise SchemaTypeError('Bad ll')
        pass

class Venue(Document):
    structure = {
        "location" : {
            'street' : basestring,
            'll' : LongitudeLatitude(),
            ...
        },
        "name" : unicode,
        ...
    }

@benneic
Copy link

benneic commented Nov 26, 2012

In fact that isn't even really necessary... it's just a list so we could override the validate() method on the Venue and implement the check there...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment