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

Method ValidateValue

core/field_relation.go:198–237  ·  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

196
197// ValidateValue implements [Field.ValidateValue] interface method.
198func (f *RelationField) ValidateValue(ctx context.Context, app App, record *Record) error {
199 ids := list.ToUniqueStringSlice(record.GetRaw(f.Name))
200 if len(ids) == 0 {
201 if f.Required {
202 return validation.ErrRequired
203 }
204 return nil // nothing to check
205 }
206
207 if f.MinSelect > 0 && len(ids) < f.MinSelect {
208 return validation.NewError("validation_not_enough_values", "Select at least {{.minSelect}}").
209 SetParams(map[string]any{"minSelect": f.MinSelect})
210 }
211
212 maxSelect := max(f.MaxSelect, 1)
213 if len(ids) > maxSelect {
214 return validation.NewError("validation_too_many_values", "Select no more than {{.maxSelect}}").
215 SetParams(map[string]any{"maxSelect": maxSelect})
216 }
217
218 // check if the related records exist
219 // ---
220 relCollection, err := app.FindCachedCollectionByNameOrId(f.CollectionId)
221 if err != nil {
222 return validation.NewError("validation_missing_rel_collection", "Relation connection is missing or cannot be accessed")
223 }
224
225 var total int
226 _ = app.ConcurrentDB().
227 Select("count(*)").
228 From(relCollection.Name).
229 AndWhere(dbx.In("id", list.ToInterfaceSlice(ids)...)).
230 Row(&total)
231 if total != len(ids) {
232 return validation.NewError("validation_missing_rel_records", "Failed to find all relation records with the provided ids")
233 }
234 // ---
235
236 return nil
237}
238
239// ValidateSettings implements [Field.ValidateSettings] interface method.
240func (f *RelationField) ValidateSettings(ctx context.Context, app App, collection *Collection) error {

Callers

nothing calls this directly

Calls 6

ToUniqueStringSliceFunction · 0.92
ToInterfaceSliceFunction · 0.92
GetRawMethod · 0.80
SelectMethod · 0.80
ConcurrentDBMethod · 0.65

Tested by

no test coverage detected