...

Source file src/gitlab.hexacode.org/go-libs/microservice/validate/validate_name.go

Documentation: gitlab.hexacode.org/go-libs/microservice/validate

     1  /*
     2   * Micro Service
     3   * Package: gitlab.hexacode.org/go-libs/microservice
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  package validate
    11  
    12  import (
    13  	"fmt"
    14  	"regexp"
    15  )
    16  
    17  func (options *Options) ValidName() error {
    18  	if !options.Required && options.ValueText == "" {
    19  		return nil
    20  	}
    21  
    22  	regexPattern := "^[a-zA-Z .'-]+$"
    23  
    24  	regex, err := regexp.Compile(regexPattern)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	isValid := regex.MatchString(options.ValueText)
    30  	if !isValid {
    31  		return fmt.Errorf("invalid type name: Name must be naming style")
    32  	}
    33  
    34  	if options.Min == 0 && options.Max == 0 {
    35  		return nil
    36  	}
    37  
    38  	if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max {
    39  		return fmt.Errorf("invalid type name: Name must be between %d and %d characters", options.Min, options.Max)
    40  	}
    41  
    42  	return nil
    43  }
    44  

View as plain text