normalized values on maxSelect change
This commit is contained in:
@@ -321,7 +321,7 @@ func (f *SchemaField) PrepareValue(value any) any {
|
||||
val := list.ToUniqueStringSlice(value)
|
||||
|
||||
options, _ := f.Options.(*SelectOptions)
|
||||
if options.MaxSelect <= 1 {
|
||||
if !options.IsMultiple() {
|
||||
if len(val) > 0 {
|
||||
return val[len(val)-1] // the last selected
|
||||
}
|
||||
@@ -333,7 +333,7 @@ func (f *SchemaField) PrepareValue(value any) any {
|
||||
val := list.ToUniqueStringSlice(value)
|
||||
|
||||
options, _ := f.Options.(*FileOptions)
|
||||
if options.MaxSelect <= 1 {
|
||||
if !options.IsMultiple() {
|
||||
if len(val) > 0 {
|
||||
return val[len(val)-1] // the last selected
|
||||
}
|
||||
@@ -345,7 +345,7 @@ func (f *SchemaField) PrepareValue(value any) any {
|
||||
ids := list.ToUniqueStringSlice(value)
|
||||
|
||||
options, _ := f.Options.(*RelationOptions)
|
||||
if options.MaxSelect != nil && *options.MaxSelect <= 1 {
|
||||
if !options.IsMultiple() {
|
||||
if len(ids) > 0 {
|
||||
return ids[len(ids)-1] // the last selected
|
||||
}
|
||||
@@ -399,7 +399,12 @@ func (f *SchemaField) PrepareValueWithModifier(baseValue any, modifier string, m
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// FieldOptions interfaces that defines common methods that every field options struct has.
|
||||
// MultiValuer defines common interface methods that every multi-valued (eg. with MaxSelect) field option struct has.
|
||||
type MultiValuer interface {
|
||||
IsMultiple() bool
|
||||
}
|
||||
|
||||
// FieldOptions defines common interface methods that every field option struct has.
|
||||
type FieldOptions interface {
|
||||
Validate() error
|
||||
}
|
||||
@@ -564,6 +569,12 @@ func (o SelectOptions) Validate() error {
|
||||
)
|
||||
}
|
||||
|
||||
// IsMultiple implements MultiValuer interface and checks whether the
|
||||
// current field options support multiple values.
|
||||
func (o SelectOptions) IsMultiple() bool {
|
||||
return o.MaxSelect > 1
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
type JsonOptions struct {
|
||||
@@ -575,6 +586,8 @@ func (o JsonOptions) Validate() error {
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
var _ MultiValuer = (*FileOptions)(nil)
|
||||
|
||||
type FileOptions struct {
|
||||
MaxSelect int `form:"maxSelect" json:"maxSelect"`
|
||||
MaxSize int `form:"maxSize" json:"maxSize"` // in bytes
|
||||
@@ -593,8 +606,16 @@ func (o FileOptions) Validate() error {
|
||||
)
|
||||
}
|
||||
|
||||
// IsMultiple implements MultiValuer interface and checks whether the
|
||||
// current field options support multiple values.
|
||||
func (o FileOptions) IsMultiple() bool {
|
||||
return o.MaxSelect > 1
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
var _ MultiValuer = (*RelationOptions)(nil)
|
||||
|
||||
type RelationOptions struct {
|
||||
// CollectionId is the id of the related collection.
|
||||
CollectionId string `form:"collectionId" json:"collectionId"`
|
||||
@@ -632,6 +653,12 @@ func (o RelationOptions) Validate() error {
|
||||
)
|
||||
}
|
||||
|
||||
// IsMultiple implements MultiValuer interface and checks whether the
|
||||
// current field options support multiple values.
|
||||
func (o RelationOptions) IsMultiple() bool {
|
||||
return o.MaxSelect == nil || *o.MaxSelect > 1
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// Deprecated: Will be removed in v0.9+
|
||||
|
||||
@@ -1983,6 +1983,28 @@ func TestSelectOptionsValidate(t *testing.T) {
|
||||
checkFieldOptionsScenarios(t, scenarios)
|
||||
}
|
||||
|
||||
func TestSelectOptionsIsMultiple(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
maxSelect int
|
||||
expect bool
|
||||
}{
|
||||
{-1, false},
|
||||
{0, false},
|
||||
{1, false},
|
||||
{2, true},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
opt := schema.SelectOptions{
|
||||
MaxSelect: s.maxSelect,
|
||||
}
|
||||
|
||||
if v := opt.IsMultiple(); v != s.expect {
|
||||
t.Errorf("[%d] Expected %v, got %v", i, s.expect, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonOptionsValidate(t *testing.T) {
|
||||
scenarios := []fieldOptionsScenario{
|
||||
{
|
||||
@@ -2053,6 +2075,28 @@ func TestFileOptionsValidate(t *testing.T) {
|
||||
checkFieldOptionsScenarios(t, scenarios)
|
||||
}
|
||||
|
||||
func TestFileOptionsIsMultiple(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
maxSelect int
|
||||
expect bool
|
||||
}{
|
||||
{-1, false},
|
||||
{0, false},
|
||||
{1, false},
|
||||
{2, true},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
opt := schema.FileOptions{
|
||||
MaxSelect: s.maxSelect,
|
||||
}
|
||||
|
||||
if v := opt.IsMultiple(); v != s.expect {
|
||||
t.Errorf("[%d] Expected %v, got %v", i, s.expect, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelationOptionsValidate(t *testing.T) {
|
||||
scenarios := []fieldOptionsScenario{
|
||||
{
|
||||
@@ -2088,3 +2132,26 @@ func TestRelationOptionsValidate(t *testing.T) {
|
||||
|
||||
checkFieldOptionsScenarios(t, scenarios)
|
||||
}
|
||||
|
||||
func TestRelationOptionsIsMultiple(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
maxSelect *int
|
||||
expect bool
|
||||
}{
|
||||
{nil, true},
|
||||
{types.Pointer(-1), false},
|
||||
{types.Pointer(0), false},
|
||||
{types.Pointer(1), false},
|
||||
{types.Pointer(2), true},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
opt := schema.RelationOptions{
|
||||
MaxSelect: s.maxSelect,
|
||||
}
|
||||
|
||||
if v := opt.IsMultiple(); v != s.expect {
|
||||
t.Errorf("[%d] Expected %v, got %v", i, s.expect, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user