Refactor validity checking for constraints