diff --git a/src/pydantic2linkml/tools.py b/src/pydantic2linkml/tools.py index 0fcb22fb..e4f3743f 100644 --- a/src/pydantic2linkml/tools.py +++ b/src/pydantic2linkml/tools.py @@ -611,13 +611,18 @@ def apply_schema_overlay(schema_yml: str, overlay_file: FilePath) -> str: :param overlay_file: Path to an existing overlay YAML file :return: Canonical YAML string with the overlay applied, keys in SchemaDefinition order - :raises ValueError: If ``schema_yml`` does not deserialize to a dict + :raises ValueError: If ``schema_yml`` does not contain valid YAML or + does not deserialize to a dict :raises YAMLContentError: If the overlay file does not contain a YAML mapping :raises InvalidLinkMLSchemaError: If the result does not conform to the LinkML meta schema """ - schema_dict = yaml.safe_load(schema_yml) + try: + schema_dict = yaml.safe_load(schema_yml) + except yaml.YAMLError as e: + raise ValueError(f"schema_yml does not contain valid YAML: {e}") from e + if not isinstance(schema_dict, dict): raise ValueError( f"schema_yml did not deserialize to a dict: {type(schema_dict)}" diff --git a/tests/test_tools.py b/tests/test_tools.py index f5babbb4..755aad40 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -733,6 +733,14 @@ def test_schema_yml_not_dict_raises_value_error(self, tmp_path: Path): schema_yml="- item1\n- item2\n", overlay_file=overlay_file ) + def test_schema_yml_invalid_yaml_raises_value_error(self, tmp_path: Path): + overlay_file = tmp_path / "overlay.yaml" + overlay_file.write_text("name: new-name\n") + with pytest.raises(ValueError): + apply_schema_overlay( + schema_yml="key: [unclosed\n", overlay_file=overlay_file + ) + def test_unknown_field_raises_invalid_schema_error(self, tmp_path: Path): overlay_file = tmp_path / "overlay.yaml" overlay_file.write_text("not_a_field: some_value\n")