/
githubmirror
/
GoFrame
Обзор
Документация
Войти
/
githubmirror
/
GoFrame
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
util/gvalid/internal/builtin/builtin_required.go
49 строк
1 KB
John Guo
feature/v2.2.0 (#2154)
26 сен 2022, 17:11
Не верифицирован
26 сен 2022, 17:11
141ca62
Код
Авторство
О чём код?
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package builtin import ( "errors" "reflect" "github.com/gogf/gf/v2/util/gconv" ) // RuleRequired implements `required` rule. // Format: required type RuleRequired struct{} func init() { Register(RuleRequired{}) } func (r RuleRequired) Name() string { return "required" } func (r RuleRequired) Message() string { return "The {field} field is required" } func (r RuleRequired) Run(in RunInput) error { if isRequiredEmpty(in.Value.Val()) { return errors.New(in.Message) } return nil } func isRequiredEmpty(value interface{}) bool { reflectValue := reflect.ValueOf(value) for reflectValue.Kind() == reflect.Ptr { reflectValue = reflectValue.Elem() } switch reflectValue.Kind() { case reflect.String, reflect.Map, reflect.Array, reflect.Slice: return reflectValue.Len() == 0 } return gconv.String(value) == "" }