Factorize input validation code for k-fold CV