Fixed query validation function