Documentation
¶
Index ¶
- type Options
- func WithAlphaNumericPersianValidator(messages map[string]string, rule ...string) Options
- func WithAlphaNumericValidator(messages map[string]string, rule ...string) Options
- func WithFiberTagResolver() Options
- func WithIranianCreditNumberValidator(messages map[string]string, rule ...string) Options
- func WithIranianIBANValidator(messages map[string]string, rule ...string) Options
- func WithIranianIdNumberValidator(messages map[string]string, rule ...string) Options
- func WithIranianMobileValidator(messages map[string]string, rule ...string) Options
- func WithIranianNationalCodeValidator(messages map[string]string, rule ...string) Options
- func WithIranianPhoneValidator(messages map[string]string, rule ...string) Options
- func WithIranianPostalCodeValidator(messages map[string]string, rule ...string) Options
- func WithJalaaliValidator(messages map[string]string, rule ...string) Options
- func WithTranslator(translator i18n.Translator, prefix string) Options
- func WithUsernameValidator(messages map[string]string, rule ...string) Options
- type Translatable
- type TranslatableField
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options func(*i18nValidator)
Options defines a function type that modifies I18nValidator.
func WithAlphaNumericPersianValidator ¶
WithAlphaNumericPersianValidator adds validation for English, Persian letters, and numbers.
func WithAlphaNumericValidator ¶
WithAlphaNumericValidator adds validation for English letters and numbers.
func WithFiberTagResolver ¶
func WithFiberTagResolver() Options
WithFiberTagResolver returns an option for configuring the I18nValidator to resolve field names from various tags. It checks the "field", "json", "form", and "xml" tags in priority order. If no valid tag is found, it defaults to the field name. Fields with the "-" tag are ignored.
func WithIranianCreditNumberValidator ¶
WithIranianCreditNumberValidator adds validation for 16-digit Iranian credit card numbers.
func WithIranianIBANValidator ¶
WithIranianIBANValidator adds validation for 24-digit Iranian IBAN numbers.
func WithIranianIdNumberValidator ¶
WithIranianIdNumberValidator adds validation for Iranian birth certificate numbers.
func WithIranianMobileValidator ¶
WithIranianMobileValidator adds validation for 11-digit Iranian mobile numbers.
func WithIranianNationalCodeValidator ¶
WithIranianNationalCodeValidator adds validation for 10-digit Iranian national ID numbers.
func WithIranianPhoneValidator ¶
WithIranianPhoneValidator adds validation for 11-digit Iranian phone numbers.
func WithIranianPostalCodeValidator ¶
WithIranianPostalCodeValidator adds validation for 10-digit Iranian postal codes.
func WithJalaaliValidator ¶
WithJalaaliValidator adds validation for Jalaali datetime strings.
func WithTranslator ¶
func WithTranslator(translator i18n.Translator, prefix string) Options
WithTranslator configures the I18nValidator with a translator and a translation key prefix.
type Translatable ¶
type Translatable interface { // TranslateError returns a localized error message for a given rule and field. TranslateError(locale, rule, field string) string }
Translatable defines an interface for translating validation error messages.
type TranslatableField ¶
type TranslatableField interface { // TranslateTitle returns a localized display name for a given field. TranslateTitle(locale, field string) string }
TranslatableField defines an interface for translating field names.
type ValidationError ¶
type ValidationError interface { // HasError checks if there is any validation or internal error. HasError() bool // HasInternalError checks if there is a internal system error. HasInternalError() bool // HasValidationErrors returns true if there are validation errors. HasValidationErrors() bool // IsFailed checks if a specific field has validation errors. IsFailed(field string) bool // IsFailedOn checks if a specific field failed on a given validation rule. IsFailedOn(field, rule string) bool // InternalError returns the internal system error related to the validation process, if any. InternalError() error // Errors returns a nested map of validation errors for each field and rule. Errors() map[string]map[string]string // Messages returns a map of validation error messages for each field. Messages() map[string][]string // Rules returns a map of validation error rules for each field. Rules() map[string][]string // MarshalJSON serializes the validation errors into JSON format. MarshalJSON() ([]byte, error) // String returns a string representation of all validation errors. String() string // AddError records a validation error for a specific field and validation rule. AddError(field, rule string, message ...string) }
ValidationError defines an interface for managing validation errors.
func NewEmptyError ¶
func NewEmptyError() ValidationError
NewEmptyError creates a new empty ValidationError without any internal error.
func NewError ¶
func NewError(err error) ValidationError
NewError creates a new ValidationError with an internal error.
type Validator ¶
type Validator interface { // AddValidation registers a custom validation rule with a custom validation function. AddValidation(rule string, f validator.Func) // AddTranslation adds a translation message for a validation rule in a specified locale. AddTranslation(locale, rule, message string, options ...i18n.PluralOption) // Struct validates an entire struct based on its defined validation rules. // Returns validation errors in the provided locale, defaulting to the translator locale if empty. Struct(locale string, value any) ValidationError // StructExpect validates a struct while ignoring specified fields. // Returns validation errors in the provided locale, defaulting to the translator locale if empty. StructExpect(locale string, value any, fields ...string) ValidationError // StructPartial validates only specified fields of a struct. // Returns validation errors in the provided locale, defaulting to the translator locale if empty. StructPartial(locale string, value any, fields ...string) ValidationError // Var validates a single variable against a rule with optional custom messages. Var(locale, name string, value any, rules string) ValidationError // VarWithValue validates a variable against another using a rule with custom error messages. VarWithValue(locale, name string, value any, other any, rules string) ValidationError }
Validator defines an interface for localized validation functionality.
func NewValidator ¶
NewValidator creates a new Validator instance with optional configurations. Initializes the I18nValidator with the provided translator and base validator, and applies any options.