You can also use the inbuilt fail language of pre-commit. See the docs here. The step in your pre-commit.yaml would look like this:
- id: disallow-spaces-in-files
name: Disallow spaces in files
entry: files must not contain spaces
language: fail
files: '.*\s.*$'