MCPcopy
hub / github.com/pocketbase/pocketbase / ValidateValue

Method ValidateValue

core/field_select.go:185–211  ·  view source on GitHub ↗

ValidateValue implements [Field.ValidateValue] interface method.

(ctx context.Context, app App, record *Record)

Source from the content-addressed store, hash-verified

183
184// ValidateValue implements [Field.ValidateValue] interface method.
185func (f *SelectField) ValidateValue(ctx context.Context, app App, record *Record) error {
186 normalizedVal := list.ToUniqueStringSlice(record.GetRaw(f.Name))
187 if len(normalizedVal) == 0 {
188 if f.Required {
189 return validation.ErrRequired
190 }
191 return nil // nothing to check
192 }
193
194 maxSelect := max(f.MaxSelect, 1)
195
196 // check max selected items
197 if len(normalizedVal) > maxSelect {
198 return validation.NewError("validation_too_many_values", "Select no more than {{.maxSelect}}").
199 SetParams(map[string]any{"maxSelect": maxSelect})
200 }
201
202 // check against the allowed values
203 for _, val := range normalizedVal {
204 if !slices.Contains(f.Values, val) {
205 return validation.NewError("validation_invalid_value", "Invalid value {{.value}}").
206 SetParams(map[string]any{"value": val})
207 }
208 }
209
210 return nil
211}
212
213// ValidateSettings implements [Field.ValidateSettings] interface method.
214func (f *SelectField) ValidateSettings(ctx context.Context, app App, collection *Collection) error {

Callers

nothing calls this directly

Calls 2

ToUniqueStringSliceFunction · 0.92
GetRawMethod · 0.80

Tested by

no test coverage detected