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

Method ValidateValue

core/field_file.go:241–307  ·  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

239
240// ValidateValue implements [Field.ValidateValue] interface method.
241func (f *FileField) ValidateValue(ctx context.Context, app App, record *Record) error {
242 files := f.toSliceValue(record.GetRaw(f.Name))
243 if len(files) == 0 {
244 if f.Required {
245 return validation.ErrRequired
246 }
247 return nil // nothing to check
248 }
249
250 // validate existing and disallow new plain string filenames submission
251 // (new files must be *filesystem.File)
252 // ---
253 oldExistingStrings := f.toSliceValue(f.getLatestOldValue(app, record))
254 existingStrings := list.ToInterfaceSlice(f.extractPlainStrings(files))
255 addedStrings := f.excludeFiles(existingStrings, oldExistingStrings)
256
257 if len(addedStrings) > 0 {
258 invalidFiles := make([]string, len(addedStrings))
259 for i, invalid := range addedStrings {
260 invalidStr := cast.ToString(invalid)
261 if len(invalidStr) > 250 {
262 invalidStr = invalidStr[:250]
263 }
264 invalidFiles[i] = invalidStr
265 }
266
267 return validation.NewError("validation_invalid_file", "Invalid new files: {{.invalidFiles}}.").
268 SetParams(map[string]any{"invalidFiles": invalidFiles})
269 }
270
271 maxSelect := f.maxSelect()
272 if len(files) > maxSelect {
273 return validation.NewError("validation_too_many_files", "The maximum allowed files is {{.maxSelect}}").
274 SetParams(map[string]any{"maxSelect": maxSelect})
275 }
276
277 // validate uploaded
278 // ---
279 uploads := f.extractUploadableFiles(files)
280 for _, upload := range uploads {
281 // loosely check the filename just in case it was manually changed after the normalization
282 err := validation.Length(1, 150).Validate(upload.Name)
283 if err != nil {
284 return err
285 }
286 err = validation.Match(looseFilenameRegex).Validate(upload.Name)
287 if err != nil {
288 return err
289 }
290
291 // check size
292 err = validators.UploadedFileSize(f.maxSize())(upload)
293 if err != nil {
294 return err
295 }
296
297 // check type
298 if len(f.MimeTypes) > 0 {

Callers

nothing calls this directly

Calls 13

toSliceValueMethod · 0.95
getLatestOldValueMethod · 0.95
extractPlainStringsMethod · 0.95
excludeFilesMethod · 0.95
maxSelectMethod · 0.95
maxSizeMethod · 0.95
ToInterfaceSliceFunction · 0.92
UploadedFileSizeFunction · 0.92
UploadedFileMimeTypeFunction · 0.92
GetRawMethod · 0.80
ValidateMethod · 0.65

Tested by

no test coverage detected