validator

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package validator 提供了配置验证功能。 包括 URL 验证、路径验证等安全验证功能。

Package validator 提供了数据验证功能。 支持手机号和邮箱格式验证,支持多个国家/地区的手机号格式。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateConfigPath

func ValidateConfigPath(path string, allowedDirs []string) (string, error)

ValidateConfigPath 验证配置文件路径,防止路径遍历攻击

该函数对配置文件路径进行验证,包括: - 检查路径是否包含路径遍历字符(..) - 验证路径是否为绝对路径或相对路径 - 可选:限制配置文件只能从特定目录读取

参数:

  • path: 要验证的文件路径
  • allowedDirs: 允许的目录列表(可选,如果为空则不限制目录)

返回:

  • string: 规范化后的绝对路径
  • error: 如果路径无效或存在安全风险,返回错误;否则返回 nil

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail 验证邮箱格式(更严格的验证)

func ValidatePhone

func ValidatePhone(phone string) bool

ValidatePhone 验证手机号格式(支持多个国家/地区)

func ValidateRemoteURL

func ValidateRemoteURL(urlStr string) error

ValidateRemoteURL 验证远程配置 URL,防止 SSRF 攻击

该函数对远程配置 URL 进行严格验证,包括: - 只允许 http:// 和 https:// 协议 - 禁止访问私有 IP 地址(10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8) - 禁止访问 localhost - 验证 URL 格式的有效性

参数:

  • urlStr: 要验证的 URL 字符串

返回:

  • error: 如果 URL 无效或存在安全风险,返回错误;否则返回 nil

func ValidateUser

func ValidateUser(phone, email string) error

ValidateUser 验证用户数据

Types

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError 验证错误

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL