
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  validate.r                                                                             #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   isValidColumnName  ( DT, col, maxLength=NULL, minLength=1, exactLength=NULL                                              #
  #                        , allow_null=is.null(minLength) && is.null(maxLength) )                                             #
  #   getErrMsg          ( x )                                                                                                 #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# validate.r
# 
# functions used for simple validations. 
# Usually used in error-checking inputs of other functions


## TODO: 
# failIfAnotinB
# fail_if_A_not_in_B
#   if (!is.null(startCols) && any(startCols %ni% names(DT)))
#     stop("Some cols in startCols are not in DT: ", pasteQand(setdiff(names(DT), startCols)))



isValidColumnName <- function(DT, col, maxLength=NULL, minLength=1, exactLength=NULL, allow_null=is.null(minLength) && is.null(maxLength)) {
## Returns TRUE if error,  FALSE if no ERROR

  col.nm <- capture.output(substitute(col))
  L <- length(col)

  ## No Error - set to blank
  ErrMsg <- NULL

  if (!(  is.character(col) ||  (allow_null && is.null(col))  ))
    ErrMsg <- sprintf("'%s' should be a character vector", col.nm)
  else if (!is.null(exactLength) && L != exactLength)
    ErrMsg <- sprintf("'%s' should be of length exactly %i (it has length %i)", col.nm, exactLength, L)
  else if (!is.null(maxLength) && L > maxLength)
    ErrMsg <- sprintf("'%s' should have length at most %i (it has length %i)", col.nm, maxLength, L)
  else if (!is.null(minLength) && L < minLength)
    ErrMsg <- sprintf("'%s' should have length at least %i (it has length %i)", col.nm, minLength, L)
  else if (any(col %ni% names(DT)))
    ErrMsg <- sprintf("Some values in '%s' are not column names of the DT. They are:\n    %s", col.nm, pasteQand(setdiff(col, names(DT))))

  ## If there is an error, ErrMsg will not be ""
  ret <- is.null(ErrMsg)

  setattr(ret, "ErrMsg", ErrMsg)

  return(ret)
}

getErrMsg <- function(x) {
  attr(x, "ErrMsg")
}

