Skip to content

Commit c638bda

Browse files
committed
feat: Add Required helper to key rules
1 parent 7c189ed commit c638bda

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

map.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,22 @@ func Key(key interface{}, rules ...Rule) *KeyRules {
133133
}
134134
}
135135

136+
// Deprecated: Use Required instead.
137+
//
136138
// Optional configures the rule to ignore the key if missing.
137139
func (r *KeyRules) Optional() *KeyRules {
138140
r.optional = true
139141
return r
140142
}
141143

144+
// Required sets whether or not this key is required. If it is optional then you
145+
// can pass false to this function. Not calling this function will default to
146+
// the key being required.
147+
func (r *KeyRules) Required(required bool) *KeyRules {
148+
r.optional = !required
149+
return r
150+
}
151+
142152
// getErrorKeyName returns the name that should be used to represent the validation error of a map key.
143153
func getErrorKeyName(key interface{}) string {
144154
return fmt.Sprintf("%v", key)

0 commit comments

Comments
 (0)