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

     1  package corevalidator
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/coreinterface"
     6  	"gitlab.com/evatix-go/core/defaultcapacity"
     7  	"gitlab.com/evatix-go/core/enums/stringcompareas"
     8  	"gitlab.com/evatix-go/core/errcore"
     9  	"gitlab.com/evatix-go/core/internal/strutilinternal"
    10  )
    11  
    12  type TextValidators struct {
    13  	Items []TextValidator
    14  }
    15  
    16  func NewTextValidators(capacity int) *TextValidators {
    17  	slice := make([]TextValidator, 0, capacity)
    18  
    19  	return &TextValidators{
    20  		Items: slice,
    21  	}
    22  }
    23  
    24  func (it *TextValidators) AsBasicSliceContractsBinder() coreinterface.BasicSlicerContractsBinder {
    25  	return it
    26  }
    27  
    28  func (it *TextValidators) Length() int {
    29  	if it == nil {
    30  		return constants.Zero
    31  	}
    32  
    33  	return len(it.Items)
    34  }
    35  
    36  func (it *TextValidators) Count() int {
    37  	return it.LastIndex()
    38  }
    39  
    40  func (it *TextValidators) IsEmpty() bool {
    41  	return it.Length() == 0
    42  }
    43  
    44  func (it *TextValidators) Add(
    45  	validator TextValidator,
    46  ) *TextValidators {
    47  	it.Items = append(
    48  		it.Items,
    49  		validator)
    50  
    51  	return it
    52  }
    53  
    54  func (it *TextValidators) Adds(
    55  	validators ...TextValidator,
    56  ) *TextValidators {
    57  	if len(validators) == 0 {
    58  		return it
    59  	}
    60  
    61  	it.Items = append(
    62  		it.Items,
    63  		validators...)
    64  
    65  	return it
    66  }
    67  
    68  func (it *TextValidators) AddSimple(
    69  	searchTerm string,
    70  	compareAs stringcompareas.Variant,
    71  ) *TextValidators {
    72  	return it.Add(TextValidator{
    73  		Search:   searchTerm,
    74  		SearchAs: compareAs,
    75  	})
    76  }
    77  
    78  func (it *TextValidators) AddSimpleAllTrue(
    79  	searchTerm string,
    80  	compareAs stringcompareas.Variant,
    81  ) *TextValidators {
    82  	coreCondition := ValidatorCoreCondition{
    83  		IsTrimCompare:        true,
    84  		IsNonEmptyWhitespace: true,
    85  		IsSortStringsBySpace: true,
    86  		IsUniqueWordOnly:     true,
    87  	}
    88  
    89  	return it.Add(
    90  		TextValidator{
    91  			Search:                 searchTerm,
    92  			SearchAs:               compareAs,
    93  			ValidatorCoreCondition: coreCondition,
    94  		})
    95  }
    96  
    97  func (it *TextValidators) HasAnyItem() bool {
    98  	return it.Length() > 0
    99  }
   100  
   101  func (it *TextValidators) LastIndex() int {
   102  	return it.Length() - 1
   103  }
   104  
   105  func (it *TextValidators) HasIndex(index int) bool {
   106  	return it.LastIndex() >= index
   107  }
   108  
   109  func (it *TextValidators) String() string {
   110  	return strutilinternal.AnyToFieldNameString(
   111  		it.Items)
   112  }
   113  
   114  func (it *TextValidators) IsMatch(
   115  	content string,
   116  	isCaseSensitive bool,
   117  ) bool {
   118  	if it.IsEmpty() {
   119  		return true
   120  	}
   121  
   122  	for _, validator := range it.Items {
   123  		if !validator.IsMatch(
   124  			content,
   125  			isCaseSensitive) {
   126  			return false
   127  		}
   128  	}
   129  
   130  	return true
   131  }
   132  
   133  func (it *TextValidators) IsMatchMany(
   134  	isSkipOnContentsEmpty,
   135  	isCaseSensitive bool,
   136  	contents ...string,
   137  ) bool {
   138  	if it.IsEmpty() {
   139  		return true
   140  	}
   141  
   142  	for _, validator := range it.Items {
   143  		isNotMatched := !validator.IsMatchMany(
   144  			isSkipOnContentsEmpty,
   145  			isCaseSensitive,
   146  			contents...)
   147  
   148  		if isNotMatched {
   149  			return isNotMatched
   150  		}
   151  	}
   152  
   153  	return true
   154  }
   155  
   156  func (it *TextValidators) VerifyFirstError(
   157  	caseIndex int,
   158  	content string,
   159  	isCaseSensitive bool,
   160  ) error {
   161  	if it.IsEmpty() {
   162  		return nil
   163  	}
   164  
   165  	params := ValidatorParamsBase{
   166  		CaseIndex:                         caseIndex,
   167  		IsIgnoreCompareOnActualInputEmpty: false,
   168  		IsAttachUserInputs:                false,
   169  		IsCaseSensitive:                   isCaseSensitive,
   170  	}
   171  
   172  	for _, validator := range it.Items {
   173  		err := validator.VerifyDetailError(
   174  			&params,
   175  			content,
   176  		)
   177  
   178  		if err != nil {
   179  			return err
   180  		}
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func (it *TextValidators) VerifyErrorMany(
   187  	isContinueOnError bool,
   188  	params *ValidatorParamsBase,
   189  	contents ...string,
   190  ) error {
   191  	if it == nil {
   192  		return nil
   193  	}
   194  
   195  	if isContinueOnError {
   196  		return it.AllVerifyErrorMany(
   197  			params,
   198  			contents...)
   199  	}
   200  
   201  	return it.VerifyFirstErrorMany(
   202  		params,
   203  		contents...)
   204  }
   205  
   206  func (it *TextValidators) VerifyFirstErrorMany(
   207  	params *ValidatorParamsBase,
   208  	contents ...string,
   209  ) error {
   210  	if it.IsEmpty() {
   211  		return nil
   212  	}
   213  
   214  	for _, item := range it.Items {
   215  		err := item.AllVerifyError(
   216  			params,
   217  			contents...,
   218  		)
   219  
   220  		if err != nil {
   221  			return err
   222  		}
   223  	}
   224  
   225  	return nil
   226  }
   227  
   228  func (it *TextValidators) AllVerifyErrorMany(
   229  	params *ValidatorParamsBase,
   230  	contents ...string,
   231  ) error {
   232  	if it.IsEmpty() {
   233  		return nil
   234  	}
   235  
   236  	capacity := defaultcapacity.OfSearch(it.Length())
   237  	errorSlice := make(
   238  		[]string,
   239  		0,
   240  		capacity)
   241  
   242  	for _, item := range it.Items {
   243  		err := item.AllVerifyError(
   244  			params,
   245  			contents...,
   246  		)
   247  
   248  		if err != nil {
   249  			errorSlice = append(
   250  				errorSlice,
   251  				err.Error())
   252  		}
   253  	}
   254  
   255  	return errcore.SliceToError(
   256  		errorSlice)
   257  }
   258  
   259  func (it *TextValidators) AllVerifyError(
   260  	caseIndex int,
   261  	content string,
   262  	isCaseSensitive bool,
   263  ) error {
   264  	if it.IsEmpty() {
   265  		return nil
   266  	}
   267  
   268  	capacity := defaultcapacity.OfSearch(it.Length())
   269  	errorSlice := make(
   270  		[]string,
   271  		0,
   272  		capacity)
   273  
   274  	params := ValidatorParamsBase{
   275  		CaseIndex:                         caseIndex,
   276  		IsIgnoreCompareOnActualInputEmpty: false,
   277  		IsAttachUserInputs:                false,
   278  		IsCaseSensitive:                   isCaseSensitive,
   279  	}
   280  
   281  	for _, item := range it.Items {
   282  		err := item.VerifyDetailError(
   283  			&params,
   284  			content)
   285  
   286  		if err != nil {
   287  			errorSlice = append(errorSlice, err.Error())
   288  		}
   289  	}
   290  
   291  	return errcore.SliceToError(errorSlice)
   292  }