It should be legal. I dont see the problem with having terms like this. There are plenty of other companies to choose from if you are a developer. From a company perspective it makes 100% sense to have terms that restricts when a developer is allowed to work on a project.
It should not be legal. You exchange your time to produce money for the company, and that's it. You should be able to do whatever you want out of the company hours, and I don't see how that can be a problem. It makes 100% sense from a worker perspective to not have your company have complete control of you. There are plenty of other workers to choose from if you are a company, in case you are dissatisfied with an employee.