Commit 2f1685e4 authored by "Sylvain ext:(%22)'s avatar "Sylvain ext:(%22)
Browse files

enhance schema validation (focus on attributes only)

parent e131963cbc13
...@@ -2,6 +2,7 @@ ChangeLog for yams ...@@ -2,6 +2,7 @@ ChangeLog for yams
------------------ ------------------
-- --
* enhance schema validation (focus on attributes only)
* check password value is an encoded string * check password value is an encoded string
2006-06-08 -- 0.5.2 2006-06-08 -- 0.5.2
......
...@@ -330,54 +330,46 @@ class EntitySchema(ERSchema): ...@@ -330,54 +330,46 @@ class EntitySchema(ERSchema):
(rtype, self.type)) (rtype, self.type))
return constraint.vocabulary() return constraint.vocabulary()
def check(self, entity, creation=False):
def check(self, entity):
"""check the entity and raises an InvalidEntity exception if it """check the entity and raises an InvalidEntity exception if it
contains some invalid fields (ie some constraints failed) contains some invalid fields (ie some constraints failed)
""" """
assert not self.is_final() assert not self.is_final()
# FIXME: for now, the given entity may not be complete so we should only errors = {}
# check available attributes (except for newly created entity ?), and etype = self.type
# optionnaly fetch required data (need a connexion to the repo for that)
errors = []
for rschema in self.ordered_relations(): for rschema in self.ordered_relations():
if not rschema.is_final():
continue
rtype = rschema.type rtype = rschema.type
if rschema.is_final(): aschema = self.schema[self.destination_type(rtype)]
constraints = rschema.rproperty(self.type, # don't care about rhs cardinality, always '*' (if it make senses)
self.destination_type(rtype), card = rschema.rproperty(etype, aschema.type, 'cardinality')[0]
'constraints') assert card in '?1'
# check value according to their type required = card == '1'
try: # check value according to their type
value = entity[rtype] try:
except KeyError: value = entity[rtype]
# FIXME value = None would be more appropriate except KeyError:
# i think we are historicaly doing "continue" to speed if creation and required:
# check of a partial entity (ie don't search for non given # missing required attribute with no default on creation
# attributes in the sources # is not autorized
continue errors[rtype] = 'missing attribute'
# skip other constraint if value is None and None is allowed # on edition, missing attribute is considered as no changes
# (ie no subject cardinality is '?' and not '1' in the list) continue
eschema = rschema.objects(entity.e_type)[0] # skip other constraint if value is None and None is allowed
if value is None: if value is None and not required:
cardinality = rschema.rproperty(self.type, eschema.type, break
'cardinality') if not aschema.check_value(value):
if cardinality[0] == '?': errors[rtype] = 'incorrect value %r for type %s' % (value,
break aschema.type)
if not eschema.check_value(value): continue
msg = 'incorrect value %r for type %s' % (value,
eschema.type)
errors.append(msg)
continue
else:
value = None
constraints = [] # XXX
# check arbitrary constraints # check arbitrary constraints
for constraint in constraints: for constraint in rschema.rproperty(etype, aschema.type,
'constraints'):
if not constraint.check(entity, rtype, value): if not constraint.check(entity, rtype, value):
errors.append('%s constraint failed for %s' % (constraint, errors[rtype] = '%s constraint failed' % constraint
rtype))
if errors: if errors:
raise InvalidEntity(entity, '\n'.join(errors)) raise InvalidEntity(entity, errors)
def check_value(self, value): def check_value(self, value):
"""check the value of a final entity (ie a const value)""" """check the value of a final entity (ie a const value)"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment