def validate(self, obj): if is_string(obj): if self.max_length is None: return True else: return self.max_length >= len(obj) else: return False
def add_item(self, key, schema): ''' Register a schema for the given key. Example ------- >>> from pyson.schema import * >>> schema = Object({'foo': Str('bar')}) >>> schema['foo'].parent is schema True >>> schema['foo'].name u'foo' >>> schema['foo'].label u'Foo' ''' if pyson_t.is_string(key): newvalue = self.as_child(schema) newvalue.name = key try: newvalue._ordering_index = schema._ordering_index except AttributeError: pass if key.endswith('?'): key = key[:-1] newvalue.is_optional = True if key.endswith('*'): key = key[:-1] newvalue.is_root = True if newvalue.is_root: if self.root_key is not None: raise TypeError('object has two root values: %s and %s' % (self.root_key, key)) self.root_key = key self._root_schema = newvalue else: self._schemas[key] = newvalue else: raise TypeError('invalid key of type %s' % type(key))