Gracefully handle missing node types when validating custom fields.

This commit is contained in:
Sybren A. Stüvel 2016-07-04 12:36:49 +02:00
parent b507216d23
commit 7e85b32fc7
2 changed files with 19 additions and 1 deletions

View File

@ -53,8 +53,22 @@ class ValidateCustomFields(Validator):
def _validate_valid_properties(self, valid_properties, field, value): def _validate_valid_properties(self, valid_properties, field, value):
projects_collection = app.data.driver.db['projects'] projects_collection = app.data.driver.db['projects']
lookup = {'_id': ObjectId(self.document['project'])} lookup = {'_id': ObjectId(self.document['project'])}
project = projects_collection.find_one(lookup) project = projects_collection.find_one(lookup)
node_type = project_get_node_type(project, self.document['node_type']) if project is None:
log.warning('Unknown project %s, declared by node %s',
project, self.document.get('_id'))
self._error(field, 'Unknown project')
return False
node_type_name = self.document['node_type']
node_type = project_get_node_type(project, node_type_name)
if node_type is None:
log.warning('Project %s has no node type %s, declared by node %s',
project, node_type_name, self.document.get('_id'))
self._error(field, 'Unknown node type')
return False
try: try:
value = self.convert_properties(value, node_type['dyn_schema']) value = self.convert_properties(value, node_type['dyn_schema'])
except Exception as e: except Exception as e:

View File

@ -74,5 +74,9 @@ def project_get_node_type(project_document, node_type_node_name):
"""Return a node_type subdocument for a project. If none is found, return """Return a node_type subdocument for a project. If none is found, return
None. None.
""" """
if project_document is None:
return None
return next((node_type for node_type in project_document['node_types'] return next((node_type for node_type in project_document['node_types']
if node_type['name'] == node_type_node_name), None) if node_type['name'] == node_type_node_name), None)