write schema based metadata to xml file only if metadata is valid as per the schema
@@ -332,17 +332,25 @@ class ModelInstanceFileMetaData(GenericFileMetaDataMixin):
etree.SubElement(model_specific_metadata_desc, executed_by, attrib=attrib)
if self.metadata_json:
+ try:
+ jsonschema.Draft4Validator(self.logical_file.metadata_schema_json).validate(
+ self.metadata_json)
+ valid_schema = True
+ except jsonschema.ValidationError:
+ valid_schema = False
+
+ if valid_schema:
metadata_dict = self.metadata_json
model_properties = etree.SubElement(model_specific_metadata_desc, 'modelProperties')
model_properties_desc = etree.SubElement(model_properties,
'{%s}Description' % CoreMetaData.NAMESPACES['rdf'])
- # since we don't allow nested objects in the schema, we are not expecting nested dict objects in metadata
+ # since we don't allow nested objects in the schema, we are not expecting nested dict
+ # objects in metadata
meta_schema_dict = self.logical_file.metadata_schema_json
meta_schema_dict_properties = meta_schema_dict.get("properties")
for k, v in metadata_dict.items():
element_path_root = "{}".format(k)
-
k_obj_element = etree.SubElement(model_properties_desc, k)
k_obj_element_desc = etree.SubElement(
k_obj_element, "{{{ns}}}Description".format(ns=CoreMetaData.NAMESPACES['rdf']))
@@ -367,7 +375,8 @@ class ModelInstanceFileMetaData(GenericFileMetaDataMixin):
for k_sub, v_sub in v.items():
field = etree.SubElement(k_obj_element_properties_desc, k_sub)
field_desc = etree.SubElement(field,
- "{{{ns}}}Description".format(ns=CoreMetaData.NAMESPACES['rdf']))
+ "{{{ns}}}Description".format(
+ ns=CoreMetaData.NAMESPACES['rdf']))
k_sub_desc_title = etree.SubElement(
field_desc, "{{{ns}}}title".format(ns=CoreMetaData.NAMESPACES['dc']))
k_sub_path_title = "{}.{}.{}".format(sub_element_path_root, k_sub, 'title')
