...

Source file src/gitlab.hexacode.org/go-libs/microservice/validate/validate_password.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  	"unicode"
    15  )
    16  
    17  func (options *Options) ValidPassword() error {
    18  	if !options.Required && options.ValueText == "" {
    19  		return nil
    20  	}
    21  
    22  	number := 0
    23  	upper := 0
    24  	letters := 0
    25  	special := 0
    26  
    27  	for _, c := range options.ValueText {
    28  		switch {
    29  		case unicode.IsNumber(c):
    30  			number++
    31  		case unicode.IsUpper(c):
    32  			upper++
    33  			letters++
    34  		case unicode.IsPunct(c) || unicode.IsSymbol(c):
    35  			special++
    36  		case unicode.IsLetter(c) || c == ' ':
    37  			letters++
    38  		}
    39  	}
    40  
    41  	if number == 0 {
    42  		return fmt.Errorf("invalid type password: Password must contains number")
    43  	}
    44  
    45  	if upper == 0 {
    46  		return fmt.Errorf("invalid type password: Password must contains uppercase")
    47  	}
    48  
    49  	if letters == 0 {
    50  		return fmt.Errorf("invalid type password: Password must contains letters")
    51  	}
    52  
    53  	if special == 0 {
    54  		return fmt.Errorf("invalid type password: Password must contains special character")
    55  	}
    56  
    57  	if options.Min == 0 && options.Max == 0 {
    58  		return nil
    59  	}
    60  
    61  	if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max {
    62  		return fmt.Errorf("invalid type password: Password must be between %d and %d characters", options.Min, options.Max)
    63  	}
    64  
    65  	return nil
    66  }
    67  

View as plain text