R/constraints.checkPattern.R

Defines functions constraints.checkPattern

Documented in constraints.checkPattern

#' Pattern matching
#' @description Search for pattern matches (value) within a character vector (constraint).
#' A regular expression is used to test field values. 
#' If the regular expression matches then the value is valid. 
#' The values of this field \code{MUST} conform to the standard 
#' \href{http://www.w3.org/TR/xmlschema-2/#regexs}{XML Schema regular expression syntax}.
#' 
#' @param constraint character vector where matches are sought
#' @param value character string to be matched
#' @return TRUE if the pattern constraint is met 
#' @rdname constraints.checkPattern
#' @export
#' 
#' @seealso \href{https://specs.frictionlessdata.io//table-schema/#constraints}{Constraints specifications}
#' 
#' @examples 
#' 
#' constraints.checkPattern(constraint = '^test$', value = 'test')
#' 
#' constraints.checkPattern(constraint = '^test$', value = 'TEST')

constraints.checkPattern <- function(constraint, value) {
  
  if (is.null(value)) return(TRUE)
  
  if (isTRUE(grepl(constraint, value))) return(TRUE)
  
  return(FALSE)
  
}

Try the tableschema.r package in your browser

Any scripts or data that you put into this service are public.

tableschema.r documentation built on Sept. 30, 2022, 1:06 a.m.