The accepted answer to this question says:
When the function is invoked, it can test the current mode and do something appropriate. For example, for some modes, or all modes except some modes, it can be a no-op or it can raise an error.
How can this be done?
derived-mode-pis the correct answer in most circumstances. You would only test
major-modedirectly in cases where you explicitly wished to exclude derived modes.