Refactored query criteria validation code