/* * 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" "regexp" ) func (options *Options) ValidNumber() error { if !options.Required && options.ValueNumber == 0 { return nil } if options.Min == 0 && options.Max == 0 { return nil } if options.ValueNumber < int64(options.Min) || options.ValueNumber > int64(options.Max) { return fmt.Errorf("invalid type number: Number must be between %d and %d", options.Min, options.Max) } return nil } func (options *Options) ValidTextNumber() error { if !options.Required && options.ValueText == "" { return nil } regexPattern := "^[0-9]+$" regex, err := regexp.Compile(regexPattern) if err != nil { return err } isValid := regex.MatchString(options.ValueText) if !isValid { return fmt.Errorf("invalid type text number: Text Number must be valid") } 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 text number: Text Number must be between %d and %d characters", options.Min, options.Max) } return nil } func (options *Options) ValidFload() error { if !options.Required && options.ValueFloat == 0 { return nil } if options.ValueFloat < float32(options.Min) || options.ValueFloat > float32(options.Max) { return fmt.Errorf("invalid type float: Number must be between %f and %f", options.MinFloat, options.MaxFloat) } return nil } func (options *Options) ValidDouble() error { if !options.Required && options.ValueDouble == 0 { return nil } if options.ValueDouble < float64(options.Min) || options.ValueDouble > float64(options.Max) { return fmt.Errorf("invalid type double: Number must be between %f and %f", options.MinFloat, options.MaxFloat) } return nil }