...

Source file src/gitlab.hexacode.org/go-libs/microservice/validate/validate.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  	"strings"
    14  
    15  	"gitlab.hexacode.org/go-libs/hctypes"
    16  )
    17  
    18  type Options struct {
    19  	Name        string     `json:"name,omitempty"`
    20  	Label       string     `json:"label,omitempty"`
    21  	Placeholder string     `json:"placeholder,omitempty"`
    22  	Type        string     `json:"type,omitempty"`
    23  	Required    bool       `json:"required,omitempty"`
    24  	Regex       string     `json:"regex,omitempty"`
    25  	Min         int        `json:"min,omitempty"`
    26  	Max         int        `json:"max,omitempty"`
    27  	MinFloat    float32    `json:"min_float,omitempty"`
    28  	MaxFloat    float32    `json:"max_float,omitempty"`
    29  	Children    []*Options `json:"children,omitempty"`
    30  	ValueText   string     `json:"value_text,omitempty"`
    31  	ValueNumber int64      `json:"value_number,omitempty"`
    32  	ValueFloat  float32    `json:"value_float,omitempty"`
    33  	ValueDouble float64    `json:"value_double,omitempty"`
    34  }
    35  
    36  type ValidateError struct {
    37  	Name     string           `json:"name,omitempty"`
    38  	Message  string           `json:"message,omitempty"`
    39  	Value    interface{}      `json:"value,omitempty"`
    40  	Children []*ValidateError `json:"children,omitempty"`
    41  }
    42  
    43  func Validate(optionses []*Options, values hctypes.Dict) []*ValidateError {
    44  	errors := []*ValidateError{}
    45  	for _, param_opt := range optionses {
    46  		var value interface{}
    47  		children := false
    48  		switch param_opt.Type {
    49  		case "string", "text", "textnumber", "username", "name", "email", "phone", "password":
    50  			val, ok := values[param_opt.Name].(hctypes.String)
    51  			if ok {
    52  				value = string(val)
    53  			} else {
    54  				value = ""
    55  			}
    56  		case "number":
    57  			val, ok := values[param_opt.Name].(hctypes.Float64)
    58  			if ok {
    59  				value = int64(val)
    60  			} else {
    61  				value = 0
    62  			}
    63  		case "float":
    64  			val, ok := values[param_opt.Name].(hctypes.Float64)
    65  			if ok {
    66  				value = float32(val)
    67  			} else {
    68  				value = 0
    69  			}
    70  		case "double":
    71  			val, ok := values[param_opt.Name].(hctypes.Float64)
    72  			if ok {
    73  				value = float64(val)
    74  			} else {
    75  				value = 0
    76  			}
    77  		case "dict":
    78  			children = true
    79  			val, ok := values[param_opt.Name].(hctypes.Dict)
    80  			if !ok {
    81  				err := &ValidateError{
    82  					Name:    param_opt.Name,
    83  					Message: "required",
    84  				}
    85  
    86  				errors = append(errors, err)
    87  			}
    88  
    89  			if val == nil {
    90  				err := &ValidateError{
    91  					Name:    param_opt.Name,
    92  					Message: "required",
    93  				}
    94  
    95  				errors = append(errors, err)
    96  			}
    97  
    98  			errs := Validate(param_opt.Children, val)
    99  
   100  			if len(errs) > 0 {
   101  				errors = append(errors, &ValidateError{
   102  					Name:     param_opt.Name,
   103  					Message:  "required",
   104  					Children: errs,
   105  				})
   106  			}
   107  		}
   108  
   109  		if !children {
   110  			err := param_opt.Validate(value)
   111  			if err != nil {
   112  				errors = append(errors, err)
   113  			}
   114  		}
   115  	}
   116  
   117  	return errors
   118  }
   119  
   120  func (options *Options) check(value interface{}) error {
   121  	switch options.Type {
   122  	case "string":
   123  		options.ValueText = value.(string)
   124  		return options.ValidText()
   125  	case "text":
   126  		options.ValueText = value.(string)
   127  		return options.ValidText()
   128  	case "textnumber":
   129  		options.ValueText = value.(string)
   130  		return options.ValidTextNumber()
   131  	case "number":
   132  		options.ValueNumber = value.(int64)
   133  		return options.ValidNumber()
   134  	case "float":
   135  		options.ValueFloat = value.(float32)
   136  		return options.ValidFload()
   137  	case "double":
   138  		options.ValueDouble = value.(float64)
   139  		return options.ValidDouble()
   140  	case "name":
   141  		options.ValueText = value.(string)
   142  		return options.ValidName()
   143  	case "username":
   144  		options.ValueText = value.(string)
   145  		return options.ValidUsername()
   146  	case "phone":
   147  		options.ValueText = value.(string)
   148  		return options.ValidPhone()
   149  	case "email":
   150  		options.ValueText = value.(string)
   151  		return options.ValidEmail()
   152  	case "password":
   153  		options.ValueText = value.(string)
   154  		return options.ValidPassword()
   155  	}
   156  	return nil
   157  }
   158  
   159  func (options *Options) Validate(value interface{}) *ValidateError {
   160  	err := options.check(value)
   161  
   162  	options.ValueText = ""
   163  	options.ValueNumber = 0
   164  	options.ValueFloat = 0
   165  	options.ValueDouble = 0
   166  
   167  	if err != nil {
   168  		s_err := err.Error()
   169  		a_err := strings.Split(s_err, ": ")
   170  		if len(a_err) > 1 {
   171  			s_err = a_err[1]
   172  		}
   173  
   174  		return &ValidateError{
   175  			Name:    options.Name,
   176  			Message: s_err,
   177  			Value:   value,
   178  		}
   179  	}
   180  	return nil
   181  }
   182  

View as plain text