valid

package
v0.0.0-...-5c1fc54 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPath               = va.NewError("valid_is_path", "must be a valid file path")
	ErrExtension          = va.NewError("valid_is_extension", "must be formatted like \".gz\" or \".tar.gz\"")
	ErrEmptyString        = va.NewError("valid_is_empty_string", "must be empty")
	ErrExistingFile       = va.NewError("valid_is_existing_file", "file does not exist")
	ErrExistingDir        = va.NewError("valid_is_existing_dir", "directory does not exist")
	ErrZero               = va.NewError("valid_is_zero", "must be empty")
	ErrBeforeTimeParam    = va.NewError("valid_before_time_param", "must not come before {{.param}}")
	ErrPathNotWithinParam = va.NewError("valid_not_within_dir", "must not be within {{.param}}")
	ErrRegexpPattern      = va.NewError("valid_regex", "must be a valid regular expression")
)
View Source
var EmptyString = va.NewStringRuleWithError(func(s string) bool { return s == "" }, ErrEmptyString)
View Source
var ExistingDir = va.NewStringRuleWithError(func(value string) bool {
	fileInfo, err := os.Stat(value)
	return !os.IsNotExist(err) && fileInfo.Mode().IsDir()
}, ErrExistingDir)
View Source
var ExistingFile = va.NewStringRuleWithError(func(value string) bool {
	fileInfo, err := os.Stat(value)
	return !os.IsNotExist(err) && fileInfo.Mode().IsRegular()
}, ErrExistingFile)
View Source
var Extension = va.NewStringRuleWithError(func(s string) bool {
	return s == "" || (strings.HasPrefix(s, ".") && !strings.HasSuffix(s, "."))
}, ErrExtension)
View Source
var Path = va.NewStringRuleWithError(func(value string) (result bool) {
	result, _ = govalidator.IsFilePath(value)
	return
}, ErrPath)
View Source
var RegexpPattern = va.By(func(value interface{}) (err error) {
	input := value.(string)
	if input == "" {
		return
	}
	if _, err := regexp.Compile(input); err != nil {
		vaErr := ErrRegexpPattern
		msg := fmt.Sprintf(`%s (rule: %s; error: %s)`, vaErr.Message(), input, err.Error())
		return vaErr.SetMessage(msg)
	}
	return
})
View Source
var RegexpTmpl = va.By(func(value interface{}) (err error) {
	input := value.(string)
	if input == "" {
		return
	}
	replacer := regexp.MustCompile(`{{[^}]+}}`)
	pattern := replacer.ReplaceAllString(input, `TMPL_REF_PLACEHOLDER`)

	return RegexpPattern.Validate(pattern)
})
View Source
var Zero = newRuleWithError(IsZero, ErrZero)

Functions

func IsZero

func IsZero(value interface{}) bool

Types

type IsBeforeTimeParamRule

type IsBeforeTimeParamRule struct{}

func (IsBeforeTimeParamRule) Validate

func (r IsBeforeTimeParamRule) Validate(param *manip.Param, value interface{}) (err error)

type IsPathNotWithinParamRule

type IsPathNotWithinParamRule struct {
	// contains filtered or unexported fields
}

func PathNotWithinParam

func PathNotWithinParam(param *manip.Param) IsPathNotWithinParamRule

func (IsPathNotWithinParamRule) Param

func (IsPathNotWithinParamRule) Validate

func (r IsPathNotWithinParamRule) Validate(value interface{}) (err error)

type ParamRule

type ParamRule struct {
	// contains filtered or unexported fields
}

func BeforeTime

func BeforeTime(param *manip.Param) ParamRule

func NewParamRule

func NewParamRule(param *manip.Param, rule validatesWithParam) ParamRule

func (ParamRule) Param

func (r ParamRule) Param() *manip.Param

func (ParamRule) Validate

func (r ParamRule) Validate(value interface{}) error

func (ParamRule) ValidateWithContext

func (r ParamRule) ValidateWithContext(ctx context.Context, value interface{}) error

type WhenBothNotZeroRule

type WhenBothNotZeroRule struct {
	// contains filtered or unexported fields
}

func WhenBothNotZero

func WhenBothNotZero(rules ...ParamRule) WhenBothNotZeroRule

func (WhenBothNotZeroRule) Validate

func (r WhenBothNotZeroRule) Validate(value interface{}) error

func (WhenBothNotZeroRule) ValidateWithContext

func (r WhenBothNotZeroRule) ValidateWithContext(ctx context.Context, value interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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