diff --git a/src/pyhf/workspace.py b/src/pyhf/workspace.py index 6339f71cc4..070f3966db 100644 --- a/src/pyhf/workspace.py +++ b/src/pyhf/workspace.py @@ -300,14 +300,15 @@ def __init__(self, spec, validate: bool = True, **config_kwargs): """ spec = copy.deepcopy(spec) - super().__init__(spec, channels=spec['channels']) self.schema = config_kwargs.pop('schema', 'workspace.json') self.version = config_kwargs.pop('version', spec.get('version', None)) # run jsonschema validation of input specification against the (provided) schema if validate: log.info(f"Validating spec against schema: {self.schema}") - schema.validate(self, self.schema, version=self.version) + schema.validate(spec, self.schema, version=self.version) + + super().__init__(spec, channels=spec['channels']) self.measurement_names = [] for measurement in self.get('measurements', []): diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 4ef6eed51a..6f1f380307 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -910,3 +910,11 @@ def test_workspace_without_validation(mocker, simplemodels_model_data): pyhf.Workspace(dict(ws), validate=False) assert pyhf.schema.validate.called is False + + +def test_workspace_invalid_specification(): + spec = {"channels": [{"name": "SR", "samples_wrong_name": []}]} + # Ensure that an invalid specifications gets caught as such + # before a KeyError: 'samples' could be reached. + with pytest.raises(pyhf.exceptions.InvalidSpecification): + pyhf.Workspace(spec)