-
Notifications
You must be signed in to change notification settings - Fork 4
/
exists.go
23 lines (20 loc) · 701 Bytes
/
exists.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package govalidator
const (
// Exists represents rule name which will be used to find the default error message.
Exists = "exists"
// ExistsMsg is default error message format for fields with Exists validation rule.
ExistsMsg = "%s does not exist"
)
// Exists checks if given value exists in the desired table or not.
//
// Example:
//
// v := validator.New()
// v.Exists(42, "users", "id", "user_id", "user with id 42 does not exist.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) Exists(value any, table, column, field, msg string) Validator {
v.check(v.repo.Exists(value, table, column), field, v.msg(Exists, msg, field))
return v
}