enhance validateable interface (#7714)
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

This PR is part of https://codeberg.org/forgejo/forgejo/pulls/4767 filed by @algernon

Validateable is enhanced for less duplication.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Co-authored-by: zam <mirco.zachmann@meissa.de>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7714
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
This commit is contained in:
Michael Jerger 2025-05-30 13:07:35 +02:00 committed by Earl Warren
parent 161924abf8
commit baa93ec66e
5 changed files with 51 additions and 29 deletions

View file

@ -10,6 +10,8 @@ import (
"unicode/utf8"
"forgejo.org/modules/timeutil"
ap "github.com/go-ap/activitypub"
)
// ErrNotValid represents an validation error
@ -41,6 +43,13 @@ func IsValid(v Validateable) (bool, error) {
return true, nil
}
func ValidateIDExists(value ap.Item, name string) []string {
if value == nil {
return []string{fmt.Sprintf("%v should not be nil", name)}
}
return ValidateNotEmpty(value.GetID().String(), name)
}
func ValidateNotEmpty(value any, name string) []string {
isValid := true
switch v := value.(type) {
@ -83,5 +92,5 @@ func ValidateOneOf(value any, allowed []any, name string) []string {
return []string{}
}
}
return []string{fmt.Sprintf("Value %v is not contained in allowed values %v", value, allowed)}
return []string{fmt.Sprintf("Field %s contains the value %v, which is not in allowed subset %v", name, value, allowed)}
}