initial v0.8 pre-release
This commit is contained in:
@@ -13,21 +13,55 @@ import (
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var schemaFieldNameRegex = regexp.MustCompile(`^\#?\w+$`)
|
||||
var schemaFieldNameRegex = regexp.MustCompile(`^\w+$`)
|
||||
|
||||
// reserved internal field names
|
||||
// commonly used field names
|
||||
const (
|
||||
ReservedFieldNameId = "id"
|
||||
ReservedFieldNameCreated = "created"
|
||||
ReservedFieldNameUpdated = "updated"
|
||||
FieldNameId = "id"
|
||||
FieldNameCreated = "created"
|
||||
FieldNameUpdated = "updated"
|
||||
FieldNameCollectionId = "collectionId"
|
||||
FieldNameCollectionName = "collectionName"
|
||||
FieldNameExpand = "expand"
|
||||
FieldNameUsername = "username"
|
||||
FieldNameEmail = "email"
|
||||
FieldNameEmailVisibility = "emailVisibility"
|
||||
FieldNameVerified = "verified"
|
||||
FieldNameTokenKey = "tokenKey"
|
||||
FieldNamePasswordHash = "passwordHash"
|
||||
FieldNameLastResetSentAt = "lastResetSentAt"
|
||||
FieldNameLastVerificationSentAt = "lastVerificationSentAt"
|
||||
)
|
||||
|
||||
// ReservedFieldNames returns slice with reserved/system field names.
|
||||
func ReservedFieldNames() []string {
|
||||
// BaseModelFieldNames returns the field names that all models have (id, created, updated).
|
||||
func BaseModelFieldNames() []string {
|
||||
return []string{
|
||||
ReservedFieldNameId,
|
||||
ReservedFieldNameCreated,
|
||||
ReservedFieldNameUpdated,
|
||||
FieldNameId,
|
||||
FieldNameCreated,
|
||||
FieldNameUpdated,
|
||||
}
|
||||
}
|
||||
|
||||
// SystemFields returns special internal field names that are usually readonly.
|
||||
func SystemFieldNames() []string {
|
||||
return []string{
|
||||
FieldNameCollectionId,
|
||||
FieldNameCollectionName,
|
||||
FieldNameExpand,
|
||||
}
|
||||
}
|
||||
|
||||
// AuthFieldNames returns the reserved "auth" collection auth field names.
|
||||
func AuthFieldNames() []string {
|
||||
return []string{
|
||||
FieldNameUsername,
|
||||
FieldNameEmail,
|
||||
FieldNameEmailVisibility,
|
||||
FieldNameVerified,
|
||||
FieldNameTokenKey,
|
||||
FieldNamePasswordHash,
|
||||
FieldNameLastResetSentAt,
|
||||
FieldNameLastVerificationSentAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +77,9 @@ const (
|
||||
FieldTypeJson string = "json"
|
||||
FieldTypeFile string = "file"
|
||||
FieldTypeRelation string = "relation"
|
||||
FieldTypeUser string = "user"
|
||||
|
||||
// Deprecated: Will be removed in v0.9!
|
||||
FieldTypeUser string = "user"
|
||||
)
|
||||
|
||||
// FieldTypes returns slice with all supported field types.
|
||||
@@ -59,7 +95,6 @@ func FieldTypes() []string {
|
||||
FieldTypeJson,
|
||||
FieldTypeFile,
|
||||
FieldTypeRelation,
|
||||
FieldTypeUser,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +104,6 @@ func ArraybleFieldTypes() []string {
|
||||
FieldTypeSelect,
|
||||
FieldTypeFile,
|
||||
FieldTypeRelation,
|
||||
FieldTypeUser,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +124,7 @@ func (f *SchemaField) ColDefinition() string {
|
||||
case FieldTypeNumber:
|
||||
return "REAL DEFAULT 0"
|
||||
case FieldTypeBool:
|
||||
return "Boolean DEFAULT FALSE"
|
||||
return "BOOLEAN DEFAULT FALSE"
|
||||
case FieldTypeJson:
|
||||
return "JSON DEFAULT NULL"
|
||||
default:
|
||||
@@ -133,9 +167,11 @@ func (f SchemaField) Validate() error {
|
||||
// init field options (if not already)
|
||||
f.InitOptions()
|
||||
|
||||
// add commonly used filter literals to the exclude names list
|
||||
excludeNames := ReservedFieldNames()
|
||||
excludeNames := BaseModelFieldNames()
|
||||
// exclude filter literals
|
||||
excludeNames = append(excludeNames, "null", "true", "false")
|
||||
// exclude system literals
|
||||
excludeNames = append(excludeNames, SystemFieldNames()...)
|
||||
|
||||
return validation.ValidateStruct(&f,
|
||||
validation.Field(&f.Options, validation.Required, validation.By(f.checkOptions)),
|
||||
@@ -198,8 +234,11 @@ func (f *SchemaField) InitOptions() error {
|
||||
options = &FileOptions{}
|
||||
case FieldTypeRelation:
|
||||
options = &RelationOptions{}
|
||||
|
||||
// Deprecated: Will be removed in v0.9!
|
||||
case FieldTypeUser:
|
||||
options = &UserOptions{}
|
||||
|
||||
default:
|
||||
return errors.New("Missing or unknown field field type.")
|
||||
}
|
||||
@@ -259,19 +298,7 @@ func (f *SchemaField) PrepareValue(value any) any {
|
||||
ids := list.ToUniqueStringSlice(value)
|
||||
|
||||
options, _ := f.Options.(*RelationOptions)
|
||||
if options.MaxSelect <= 1 {
|
||||
if len(ids) > 0 {
|
||||
return ids[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
return ids
|
||||
case FieldTypeUser:
|
||||
ids := list.ToUniqueStringSlice(value)
|
||||
|
||||
options, _ := f.Options.(*UserOptions)
|
||||
if options.MaxSelect <= 1 {
|
||||
if options.MaxSelect != nil && *options.MaxSelect <= 1 {
|
||||
if len(ids) > 0 {
|
||||
return ids[0]
|
||||
}
|
||||
@@ -426,13 +453,18 @@ type SelectOptions struct {
|
||||
}
|
||||
|
||||
func (o SelectOptions) Validate() error {
|
||||
max := len(o.Values)
|
||||
if max == 0 {
|
||||
max = 1
|
||||
}
|
||||
|
||||
return validation.ValidateStruct(&o,
|
||||
validation.Field(&o.Values, validation.Required),
|
||||
validation.Field(
|
||||
&o.MaxSelect,
|
||||
validation.Required,
|
||||
validation.Min(1),
|
||||
validation.Max(len(o.Values)),
|
||||
validation.Max(max),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -469,27 +501,27 @@ func (o FileOptions) Validate() error {
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
type RelationOptions struct {
|
||||
MaxSelect int `form:"maxSelect" json:"maxSelect"`
|
||||
MaxSelect *int `form:"maxSelect" json:"maxSelect"`
|
||||
CollectionId string `form:"collectionId" json:"collectionId"`
|
||||
CascadeDelete bool `form:"cascadeDelete" json:"cascadeDelete"`
|
||||
}
|
||||
|
||||
func (o RelationOptions) Validate() error {
|
||||
return validation.ValidateStruct(&o,
|
||||
validation.Field(&o.MaxSelect, validation.Required, validation.Min(1)),
|
||||
validation.Field(&o.CollectionId, validation.Required),
|
||||
validation.Field(&o.MaxSelect, validation.NilOrNotEmpty, validation.Min(1)),
|
||||
)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// Deprecated: Will be removed in v0.9!
|
||||
type UserOptions struct {
|
||||
MaxSelect int `form:"maxSelect" json:"maxSelect"`
|
||||
CascadeDelete bool `form:"cascadeDelete" json:"cascadeDelete"`
|
||||
}
|
||||
|
||||
// Deprecated: Will be removed in v0.9!
|
||||
func (o UserOptions) Validate() error {
|
||||
return validation.ValidateStruct(&o,
|
||||
validation.Field(&o.MaxSelect, validation.Required, validation.Min(1)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
+116
-132
@@ -11,27 +11,48 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestReservedFieldNames(t *testing.T) {
|
||||
result := schema.ReservedFieldNames()
|
||||
func TestBaseModelFieldNames(t *testing.T) {
|
||||
result := schema.BaseModelFieldNames()
|
||||
expected := 3
|
||||
|
||||
if len(result) != 3 {
|
||||
t.Fatalf("Expected %d names, got %d (%v)", 3, len(result), result)
|
||||
if len(result) != expected {
|
||||
t.Fatalf("Expected %d field names, got %d (%v)", expected, len(result), result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemFieldNames(t *testing.T) {
|
||||
result := schema.SystemFieldNames()
|
||||
expected := 3
|
||||
|
||||
if len(result) != expected {
|
||||
t.Fatalf("Expected %d field names, got %d (%v)", expected, len(result), result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthFieldNames(t *testing.T) {
|
||||
result := schema.AuthFieldNames()
|
||||
expected := 8
|
||||
|
||||
if len(result) != expected {
|
||||
t.Fatalf("Expected %d auth field names, got %d (%v)", expected, len(result), result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldTypes(t *testing.T) {
|
||||
result := schema.FieldTypes()
|
||||
expected := 10
|
||||
|
||||
if len(result) != 11 {
|
||||
t.Fatalf("Expected %d types, got %d (%v)", 3, len(result), result)
|
||||
if len(result) != expected {
|
||||
t.Fatalf("Expected %d types, got %d (%v)", expected, len(result), result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArraybleFieldTypes(t *testing.T) {
|
||||
result := schema.ArraybleFieldTypes()
|
||||
expected := 3
|
||||
|
||||
if len(result) != 4 {
|
||||
t.Fatalf("Expected %d types, got %d (%v)", 3, len(result), result)
|
||||
if len(result) != expected {
|
||||
t.Fatalf("Expected %d arrayble types, got %d (%v)", expected, len(result), result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +71,7 @@ func TestSchemaFieldColDefinition(t *testing.T) {
|
||||
},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeBool, Name: "test"},
|
||||
"Boolean DEFAULT FALSE",
|
||||
"BOOLEAN DEFAULT FALSE",
|
||||
},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeEmail, Name: "test"},
|
||||
@@ -80,10 +101,6 @@ func TestSchemaFieldColDefinition(t *testing.T) {
|
||||
schema.SchemaField{Type: schema.FieldTypeRelation, Name: "test"},
|
||||
"TEXT DEFAULT ''",
|
||||
},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeUser, Name: "test"},
|
||||
"TEXT DEFAULT ''",
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
@@ -297,7 +314,7 @@ func TestSchemaFieldValidate(t *testing.T) {
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.ReservedFieldNameId,
|
||||
Name: schema.FieldNameId,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
@@ -306,7 +323,7 @@ func TestSchemaFieldValidate(t *testing.T) {
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.ReservedFieldNameCreated,
|
||||
Name: schema.FieldNameCreated,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
@@ -315,7 +332,34 @@ func TestSchemaFieldValidate(t *testing.T) {
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.ReservedFieldNameUpdated,
|
||||
Name: schema.FieldNameUpdated,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
{
|
||||
"reserved name (collectionId)",
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.FieldNameCollectionId,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
{
|
||||
"reserved name (collectionName)",
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.FieldNameCollectionName,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
{
|
||||
"reserved name (expand)",
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeText,
|
||||
Id: "1234567890",
|
||||
Name: schema.FieldNameExpand,
|
||||
},
|
||||
[]string{"name"},
|
||||
},
|
||||
@@ -456,7 +500,7 @@ func TestSchemaFieldInitOptions(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeRelation},
|
||||
false,
|
||||
`{"system":false,"id":"","name":"","type":"relation","required":false,"unique":false,"options":{"maxSelect":0,"collectionId":"","cascadeDelete":false}}`,
|
||||
`{"system":false,"id":"","name":"","type":"relation","required":false,"unique":false,"options":{"maxSelect":null,"collectionId":"","cascadeDelete":false}}`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeUser},
|
||||
@@ -548,8 +592,9 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, nil, `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, "", `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, "test", `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, 1641024040, `"2022-01-01 08:00:40.000"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, "2022-01-01 11:27:10.123", `"2022-01-01 11:27:10.123"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, 1641024040, `"2022-01-01 08:00:40.000Z"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, "2022-01-01 11:27:10.123", `"2022-01-01 11:27:10.123Z"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, "2022-01-01 11:27:10.123Z", `"2022-01-01 11:27:10.123Z"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, types.DateTime{}, `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeDate}, time.Time{}, `""`},
|
||||
|
||||
@@ -697,13 +742,48 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
},
|
||||
|
||||
// relation (single)
|
||||
{schema.SchemaField{Type: schema.FieldTypeRelation}, nil, `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeRelation}, "", `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeRelation}, 123, `"123"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeRelation}, "abc", `"abc"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeRelation}, "1ba88b4f-e9da-42f0-9764-9a55c953e724", `"1ba88b4f-e9da-42f0-9764-9a55c953e724"`},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeRelation},
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)},
|
||||
},
|
||||
nil,
|
||||
`""`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)},
|
||||
},
|
||||
"",
|
||||
`""`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)},
|
||||
},
|
||||
123,
|
||||
`"123"`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)},
|
||||
},
|
||||
"abc",
|
||||
`"abc"`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)},
|
||||
},
|
||||
"1ba88b4f-e9da-42f0-9764-9a55c953e724",
|
||||
`"1ba88b4f-e9da-42f0-9764-9a55c953e724"`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeRelation, Options: &schema.RelationOptions{MaxSelect: types.Pointer(1)}},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`"1ba88b4f-e9da-42f0-9764-9a55c953e724"`,
|
||||
},
|
||||
@@ -711,7 +791,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
nil,
|
||||
`[]`,
|
||||
@@ -719,7 +799,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
"",
|
||||
`[]`,
|
||||
@@ -727,7 +807,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
[]string{},
|
||||
`[]`,
|
||||
@@ -735,7 +815,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
123,
|
||||
`["123"]`,
|
||||
@@ -743,7 +823,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
[]string{"", "abc"},
|
||||
`["abc"]`,
|
||||
@@ -752,7 +832,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
// no values validation
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`["1ba88b4f-e9da-42f0-9764-9a55c953e724","2ba88b4f-e9da-42f0-9764-9a55c953e724"]`,
|
||||
@@ -761,77 +841,7 @@ func TestSchemaFieldPrepareValue(t *testing.T) {
|
||||
// duplicated values
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeRelation,
|
||||
Options: &schema.RelationOptions{MaxSelect: 2},
|
||||
},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724", "1ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`["1ba88b4f-e9da-42f0-9764-9a55c953e724","2ba88b4f-e9da-42f0-9764-9a55c953e724"]`,
|
||||
},
|
||||
|
||||
// user (single)
|
||||
{schema.SchemaField{Type: schema.FieldTypeUser}, nil, `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeUser}, "", `""`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeUser}, 123, `"123"`},
|
||||
{schema.SchemaField{Type: schema.FieldTypeUser}, "1ba88b4f-e9da-42f0-9764-9a55c953e724", `"1ba88b4f-e9da-42f0-9764-9a55c953e724"`},
|
||||
{
|
||||
schema.SchemaField{Type: schema.FieldTypeUser},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`"1ba88b4f-e9da-42f0-9764-9a55c953e724"`,
|
||||
},
|
||||
// user (multiple)
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
nil,
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
"",
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
[]string{},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
123,
|
||||
`["123"]`,
|
||||
},
|
||||
{
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
[]string{"", "abc"},
|
||||
`["abc"]`,
|
||||
},
|
||||
{
|
||||
// no values validation
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`["1ba88b4f-e9da-42f0-9764-9a55c953e724","2ba88b4f-e9da-42f0-9764-9a55c953e724"]`,
|
||||
},
|
||||
{
|
||||
// duplicated values
|
||||
schema.SchemaField{
|
||||
Type: schema.FieldTypeUser,
|
||||
Options: &schema.UserOptions{MaxSelect: 2},
|
||||
Options: &schema.RelationOptions{MaxSelect: types.Pointer(2)},
|
||||
},
|
||||
[]string{"1ba88b4f-e9da-42f0-9764-9a55c953e724", "2ba88b4f-e9da-42f0-9764-9a55c953e724", "1ba88b4f-e9da-42f0-9764-9a55c953e724"},
|
||||
`["1ba88b4f-e9da-42f0-9764-9a55c953e724","2ba88b4f-e9da-42f0-9764-9a55c953e724"]`,
|
||||
@@ -1277,13 +1287,13 @@ func TestRelationOptionsValidate(t *testing.T) {
|
||||
{
|
||||
"empty",
|
||||
schema.RelationOptions{},
|
||||
[]string{"maxSelect", "collectionId"},
|
||||
[]string{"collectionId"},
|
||||
},
|
||||
{
|
||||
"empty CollectionId",
|
||||
schema.RelationOptions{
|
||||
CollectionId: "",
|
||||
MaxSelect: 1,
|
||||
MaxSelect: types.Pointer(1),
|
||||
},
|
||||
[]string{"collectionId"},
|
||||
},
|
||||
@@ -1291,7 +1301,7 @@ func TestRelationOptionsValidate(t *testing.T) {
|
||||
"MaxSelect <= 0",
|
||||
schema.RelationOptions{
|
||||
CollectionId: "abc",
|
||||
MaxSelect: 0,
|
||||
MaxSelect: types.Pointer(0),
|
||||
},
|
||||
[]string{"maxSelect"},
|
||||
},
|
||||
@@ -1299,33 +1309,7 @@ func TestRelationOptionsValidate(t *testing.T) {
|
||||
"MaxSelect > 0 && non-empty CollectionId",
|
||||
schema.RelationOptions{
|
||||
CollectionId: "abc",
|
||||
MaxSelect: 1,
|
||||
},
|
||||
[]string{},
|
||||
},
|
||||
}
|
||||
|
||||
checkFieldOptionsScenarios(t, scenarios)
|
||||
}
|
||||
|
||||
func TestUserOptionsValidate(t *testing.T) {
|
||||
scenarios := []fieldOptionsScenario{
|
||||
{
|
||||
"empty",
|
||||
schema.UserOptions{},
|
||||
[]string{"maxSelect"},
|
||||
},
|
||||
{
|
||||
"MaxSelect <= 0",
|
||||
schema.UserOptions{
|
||||
MaxSelect: 0,
|
||||
},
|
||||
[]string{"maxSelect"},
|
||||
},
|
||||
{
|
||||
"MaxSelect > 0",
|
||||
schema.UserOptions{
|
||||
MaxSelect: 1,
|
||||
MaxSelect: types.Pointer(1),
|
||||
},
|
||||
[]string{},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user