Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dive into and validate map keys? #1247

Open
ehsandavari opened this issue Apr 4, 2024 · 0 comments
Open

Dive into and validate map keys? #1247

ehsandavari opened this issue Apr 4, 2024 · 0 comments

Comments

@ehsandavari
Copy link

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Question

Code sample, to showcase or reproduce:

package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)

type TI18nKey string

const (
	I18nKeyInternalServer               TI18nKey = "InternalServer"
	I18nKeyBadRequest                   TI18nKey = "BadRequest"
	I18nKeyNotFound                     TI18nKey = "NotFound"
	I18nKeyUserPhoneNumberExists        TI18nKey = "UserPhoneNumberExists"
	I18nKeyPhoneNumberOrPasswordInvalid TI18nKey = "PhoneNumberOrPasswordInvalid"
)

func (r TI18nKey) String() string {
	return string(r)
}

func (r TI18nKey) IsValid() bool {
	switch r {
	case I18nKeyInternalServer,
		I18nKeyBadRequest,
		I18nKeyNotFound,
		I18nKeyUserPhoneNumberExists,
		I18nKeyPhoneNumberOrPasswordInvalid:
		return true
	default:
		return false
	}
}

type TLanguage string

const (
	LanguageEN TLanguage = "en"
	LanguageFA TLanguage = "fa"
)

func (r TLanguage) String() string {
	return string(r)
}

func (r TLanguage) IsValid() bool {
	switch r {
	case LanguageEN,
		LanguageFA:
		return true
	default:
		return false
	}
}

type Config struct {
	Messages map[TLanguage]map[TI18nKey]string `validate:"required,dive,keys,customType"`
}

func main() {
	validate := validator.New()
	validate.RegisterValidation("customType", customTypeValidator)

	messages := map[TLanguage]map[TI18nKey]string{
		LanguageEN: {
			I18nKeyInternalServer: "en internal server error",
			"NotRegisteredKey":    "in this case expect validator return validation error",
		},
		LanguageFA: {
			I18nKeyInternalServer: "fa internal server error",
			"NotRegisteredKey":    "in this case expect validator return validation error",
		},
		"NotRegisteredLanguage": {}, // in this case validator is return error : its true
	}

	err := validate.Struct(Config{Messages: messages})
	if err != nil {
		fmt.Printf("messages not valid! %s\n", err)
	}
}

type iCustomTypeValidatorValidator interface {
	IsValid() bool
}

func customTypeValidator(fl validator.FieldLevel) bool {
	value := fl.Field().Interface().(iCustomTypeValidatorValidator)
	return value.IsValid()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant