Brijesh's Git Server — identity @ 8c36e39b8c6eda064ffa45901a976c3ebf028d97

authentication service

core/utils/necessary_fields.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
package utils

import (
	"errors"
	"fmt"
	"reflect"
)

func CheckNeceassaryFieldsExist(v interface{}, requiredFields []string) error {
	val := reflect.ValueOf(v)

	// Ensure we're dealing with a struct
	if val.Kind() != reflect.Struct {
		return errors.New("expected a struct")
	}

	// Loop through the required field names
	for _, fieldName := range requiredFields {
		// Get the field by name
		field := val.FieldByName(fieldName)

		// Check if the field exists
		if !field.IsValid() {
			return fmt.Errorf("field %s does not exist in the struct", fieldName)
		}

		// Only check string fields
		if field.Kind() == reflect.String {
			// Check if the string is empty
			if field.String() == "" {
				return fmt.Errorf("field %s is required and cannot be empty", fieldName)
			}
		}
	}

	return nil
}