gitlab.com/evatix-go/core@v1.3.55/corevalidator/TextValidator.go (about)

     1  package corevalidator
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"gitlab.com/evatix-go/core/constants"
     9  	"gitlab.com/evatix-go/core/coreutils/stringutil"
    10  	"gitlab.com/evatix-go/core/enums/stringcompareas"
    11  	"gitlab.com/evatix-go/core/errcore"
    12  	"gitlab.com/evatix-go/core/internal/msgformats"
    13  )
    14  
    15  type TextValidator struct {
    16  	Search   string `json:"Search,omitempty"`
    17  	SearchAs stringcompareas.Variant
    18  	ValidatorCoreCondition
    19  	searchTextFinalized *string
    20  }
    21  
    22  func (it *TextValidator) ToString(isSingleLine bool) string {
    23  	if isSingleLine {
    24  		return fmt.Sprintf(
    25  			msgformats.TextValidatorSingleLineFormat,
    26  			it.Search,
    27  			it.SearchAs.Name(),
    28  			it.IsTrimCompare,
    29  			it.IsSplitByWhitespace(),
    30  			it.IsUniqueWordOnly,
    31  			it.IsNonEmptyWhitespace,
    32  			it.IsSortStringsBySpace)
    33  	}
    34  
    35  	return fmt.Sprintf(
    36  		msgformats.TextValidatorMultiLineFormat,
    37  		it.Search,
    38  		it.SearchAs.Name(),
    39  		it.IsTrimCompare,
    40  		it.IsSplitByWhitespace(),
    41  		it.IsUniqueWordOnly,
    42  		it.IsNonEmptyWhitespace,
    43  		it.IsSortStringsBySpace)
    44  }
    45  
    46  func (it *TextValidator) String() string {
    47  	return it.ToString(true)
    48  }
    49  
    50  func (it *TextValidator) SearchTextFinalized() string {
    51  	return *it.SearchTextFinalizedPtr()
    52  }
    53  
    54  func (it *TextValidator) SearchTextFinalizedPtr() *string {
    55  	if it.searchTextFinalized != nil {
    56  		return it.searchTextFinalized
    57  	}
    58  
    59  	searchTerm := it.GetCompiledTermBasedOnConditions(
    60  		it.Search,
    61  		it.IsUniqueWordOnly) // for unique word, use lowercase
    62  
    63  	it.searchTextFinalized = &searchTerm
    64  
    65  	return it.searchTextFinalized
    66  }
    67  
    68  func (it *TextValidator) GetCompiledTermBasedOnConditions(
    69  	input string,
    70  	isCaseSensitive bool,
    71  ) string {
    72  	searchTerm := input
    73  
    74  	if it.IsTrimCompare {
    75  		searchTerm = strings.TrimSpace(searchTerm)
    76  	}
    77  
    78  	if it.IsSplitByWhitespace() {
    79  		compiledStringSplits := stringutil.SplitContentsByWhitespaceConditions(
    80  			searchTerm,
    81  			it.IsTrimCompare,
    82  			it.IsNonEmptyWhitespace,
    83  			it.IsSortStringsBySpace,
    84  			it.IsUniqueWordOnly,
    85  			!isCaseSensitive)
    86  
    87  		return strings.Join(
    88  			compiledStringSplits,
    89  			constants.Space)
    90  	}
    91  
    92  	return searchTerm
    93  }
    94  
    95  func (it *TextValidator) IsMatch(
    96  	content string,
    97  	isCaseSensitive bool,
    98  ) bool {
    99  	search := it.SearchTextFinalized()
   100  	processedContent := it.GetCompiledTermBasedOnConditions(
   101  		content,
   102  		isCaseSensitive)
   103  
   104  	isIgnoreCase := !isCaseSensitive
   105  
   106  	return it.SearchAs.IsCompareSuccess(
   107  		isIgnoreCase,
   108  		processedContent,
   109  		search,
   110  	)
   111  }
   112  
   113  func (it *TextValidator) IsMatchMany(
   114  	isSkipOnContentsEmpty,
   115  	isCaseSensitive bool,
   116  	contents ...string,
   117  ) bool {
   118  	if it == nil {
   119  		return true
   120  	}
   121  
   122  	if len(contents) == 0 && isSkipOnContentsEmpty {
   123  		return true
   124  	}
   125  
   126  	for _, content := range contents {
   127  		if !it.IsMatch(content, isCaseSensitive) {
   128  			return false
   129  		}
   130  	}
   131  
   132  	return true
   133  }
   134  
   135  func (it *TextValidator) VerifyDetailError(
   136  	params *ValidatorParamsBase,
   137  	content string,
   138  ) error {
   139  	if it == nil {
   140  		return nil
   141  	}
   142  
   143  	return it.verifyDetailErrorUsingLineProcessing(
   144  		constants.InvalidValue,
   145  		params,
   146  		content)
   147  }
   148  
   149  func (it *TextValidator) verifyDetailErrorUsingLineProcessing(
   150  	lineProcessingIndex int,
   151  	params *ValidatorParamsBase,
   152  	content string,
   153  ) error {
   154  	if it == nil {
   155  		return nil
   156  	}
   157  
   158  	processedSearch := it.SearchTextFinalized()
   159  	processedContent := it.GetCompiledTermBasedOnConditions(
   160  		content,
   161  		params.IsCaseSensitive)
   162  
   163  	isMatch := it.SearchAs.IsCompareSuccess(
   164  		params.IsIgnoreCase(),
   165  		processedContent,
   166  		processedSearch,
   167  	)
   168  
   169  	if isMatch {
   170  		return nil
   171  	}
   172  
   173  	expectationMethod := it.SearchAs.Name()
   174  
   175  	msg := errcore.GetSearchTermExpectationMessage(
   176  		params.CaseIndex,
   177  		expectationMethod,
   178  		lineProcessingIndex,
   179  		processedContent,
   180  		processedSearch,
   181  		it.String())
   182  
   183  	return errors.New(msg)
   184  }
   185  
   186  func (it *TextValidator) MethodName() string {
   187  	return it.SearchAs.Name()
   188  }
   189  
   190  func (it *TextValidator) VerifySimpleError(
   191  	processingIndex int,
   192  	params *ValidatorParamsBase,
   193  	content string,
   194  ) error {
   195  	if it == nil {
   196  		return nil
   197  	}
   198  
   199  	processedSearch := it.SearchTextFinalized()
   200  	processedContent := it.GetCompiledTermBasedOnConditions(
   201  		content,
   202  		params.IsCaseSensitive)
   203  
   204  	isMatch := it.SearchAs.IsCompareSuccess(
   205  		params.IsIgnoreCase(),
   206  		processedContent,
   207  		processedSearch,
   208  	)
   209  
   210  	if isMatch {
   211  		return nil
   212  	}
   213  
   214  	method := it.SearchAs.Name()
   215  
   216  	msg := errcore.GetSearchTermExpectationSimpleMessage(
   217  		params.CaseIndex,
   218  		method,
   219  		processingIndex,
   220  		processedContent,
   221  		processedSearch)
   222  
   223  	return errors.New(msg)
   224  }
   225  
   226  func (it *TextValidator) VerifyMany(
   227  	isContinueOnError bool,
   228  	params *ValidatorParamsBase,
   229  	contents ...string,
   230  ) error {
   231  	if isContinueOnError {
   232  		return it.AllVerifyError(
   233  			params,
   234  			contents...)
   235  	}
   236  
   237  	return it.VerifyFirstError(
   238  		params,
   239  		contents...)
   240  }
   241  
   242  func (it *TextValidator) VerifyFirstError(
   243  	params *ValidatorParamsBase,
   244  	contents ...string,
   245  ) error {
   246  	if it == nil {
   247  		return nil
   248  	}
   249  
   250  	length := len(contents)
   251  	if length == 0 && params.IsIgnoreCompareOnActualInputEmpty {
   252  		return nil
   253  	}
   254  
   255  	for i, content := range contents {
   256  		err := it.verifyDetailErrorUsingLineProcessing(
   257  			i,
   258  			params,
   259  			content,
   260  		)
   261  
   262  		if err != nil {
   263  			return err
   264  		}
   265  	}
   266  
   267  	return nil
   268  }
   269  
   270  func (it *TextValidator) AllVerifyError(
   271  	params *ValidatorParamsBase,
   272  	contents ...string,
   273  ) error {
   274  	if it == nil {
   275  		return nil
   276  	}
   277  
   278  	length := len(contents)
   279  	if length == 0 && params.IsIgnoreCompareOnActualInputEmpty {
   280  		return nil
   281  	}
   282  
   283  	var sliceErr []string
   284  
   285  	for i, content := range contents {
   286  		err := it.verifyDetailErrorUsingLineProcessing(
   287  			i,
   288  			params,
   289  			content,
   290  		)
   291  
   292  		if err != nil {
   293  			sliceErr = append(
   294  				sliceErr,
   295  				err.Error())
   296  		}
   297  	}
   298  
   299  	return errcore.SliceToError(
   300  		sliceErr)
   301  }