/* * Micro Service * Package: gitlab.hexacode.org/go-libs/microservice * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package validate import ( "fmt" "unicode" ) func (options *Options) ValidPassword() error { if !options.Required && options.ValueText == "" { return nil } number := 0 upper := 0 letters := 0 special := 0 for _, c := range options.ValueText { switch { case unicode.IsNumber(c): number++ case unicode.IsUpper(c): upper++ letters++ case unicode.IsPunct(c) || unicode.IsSymbol(c): special++ case unicode.IsLetter(c) || c == ' ': letters++ } } if number == 0 { return fmt.Errorf("invalid type password: Password must contains number") } if upper == 0 { return fmt.Errorf("invalid type password: Password must contains uppercase") } if letters == 0 { return fmt.Errorf("invalid type password: Password must contains letters") } if special == 0 { return fmt.Errorf("invalid type password: Password must contains special character") } if options.Min == 0 && options.Max == 0 { return nil } if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max { return fmt.Errorf("invalid type password: Password must be between %d and %d characters", options.Min, options.Max) } return nil }