From 6d673ac6d531fa79e478e93b6550164363950c5c Mon Sep 17 00:00:00 2001 From: Nicholas Wold Date: Mon, 24 Jan 2022 10:34:32 -0800 Subject: [PATCH] Do not crash if the intended cell type is not valid --- nbformat/validator.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/nbformat/validator.py b/nbformat/validator.py index 46ab6716..135e0ded 100644 --- a/nbformat/validator.py +++ b/nbformat/validator.py @@ -336,13 +336,15 @@ def iter_validate(nbdict=None, ref=None, version=None, version_minor=None, if "oneOf" in error_tree["cells"][cell_idx].errors: intended_cell_type = nbdict["cells"][cell_idx]["cell_type"] schemas_by_index = [ref["$ref"] for ref in error_tree["cells"][cell_idx].errors["oneOf"].schema["oneOf"]] - schema_index = schemas_by_index.index(f"#/definitions/{intended_cell_type}_cell") - for error in error_tree["cells"][cell_idx].errors["oneOf"].context: - rel_path = error.relative_path - error_for_intended_schema = error.schema_path[0] == schema_index - is_top_level_metadata_key = len(rel_path) == 2 and rel_path[0] == "metadata" - if error_for_intended_schema and is_top_level_metadata_key: - nbdict["cells"][cell_idx]["metadata"].pop(rel_path[1], None) + cell_type_definition_name = f"#/definitions/{intended_cell_type}_cell" + if cell_type_definition_name in schemas_by_index: + schema_index = schemas_by_index.index(cell_type_definition_name) + for error in error_tree["cells"][cell_idx].errors["oneOf"].context: + rel_path = error.relative_path + error_for_intended_schema = error.schema_path[0] == schema_index + is_top_level_metadata_key = len(rel_path) == 2 and rel_path[0] == "metadata" + if error_for_intended_schema and is_top_level_metadata_key: + nbdict["cells"][cell_idx]["metadata"].pop(rel_path[1], None) # Validate one more time to ensure that us removing metadata # didn't cause another complex validation issue in the schema.