From 84309446506a047c5cb6ad77f07c6d90531ef04f Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Wed, 22 Mar 2023 15:47:34 +0200 Subject: [PATCH] deprecated SchemaField.Unique --- forms/validators/record_data.go | 11 --- forms/validators/record_data_test.go | 100 --------------------------- models/schema/schema_field.go | 8 ++- 3 files changed, 6 insertions(+), 113 deletions(-) diff --git a/forms/validators/record_data.go b/forms/validators/record_data.go index c7dbd176..39e87283 100644 --- a/forms/validators/record_data.go +++ b/forms/validators/record_data.go @@ -86,17 +86,6 @@ func (validator *RecordDataValidator) Validate(data map[string]any) error { errs[key] = err continue } - - // check unique constraint - if field.Unique && !validator.dao.IsRecordValueUnique( - validator.record.Collection().Id, - key, - value, - validator.record.GetId(), - ) { - errs[key] = validation.NewError("validation_not_unique", "Value must be unique") - continue - } } if len(errs) == 0 { diff --git a/forms/validators/record_data_test.go b/forms/validators/record_data_test.go index 263c7394..ab55c2f7 100644 --- a/forms/validators/record_data_test.go +++ b/forms/validators/record_data_test.go @@ -99,16 +99,6 @@ func TestRecordDataValidatorValidateText(t *testing.T) { nil, []string{"field2"}, }, - { - "(text) check unique constraint", - map[string]any{ - "field1": "test", - "field2": "test", - "field3": "test", - }, - nil, - []string{"field3"}, - }, { "(text) check min constraint", map[string]any{ @@ -225,16 +215,6 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) { nil, []string{"field2"}, }, - { - "(number) check unique constraint", - map[string]any{ - "field1": 123, - "field2": 123, - "field3": 123, - }, - nil, - []string{"field3"}, - }, { "(number) check min constraint", map[string]any{ @@ -345,16 +325,6 @@ func TestRecordDataValidatorValidateBool(t *testing.T) { nil, []string{"field2"}, }, - { - "(bool) check unique constraint", - map[string]any{ - "field1": true, - "field2": true, - "field3": true, - }, - nil, - []string{"field3"}, - }, { "(bool) valid data (only required)", map[string]any{ @@ -441,16 +411,6 @@ func TestRecordDataValidatorValidateEmail(t *testing.T) { nil, []string{"field1", "field2", "field3"}, }, - { - "(email) check unique constraint", - map[string]any{ - "field1": "test@example.com", - "field2": "test@test.com", - "field3": "test@example.com", - }, - nil, - []string{"field3"}, - }, { "(email) check ExceptDomains constraint", map[string]any{ @@ -557,16 +517,6 @@ func TestRecordDataValidatorValidateUrl(t *testing.T) { nil, []string{"field1", "field3"}, }, - { - "(url) check unique constraint", - map[string]any{ - "field1": "http://example.com", - "field2": "http://test.com", - "field3": "http://example.com", - }, - nil, - []string{"field3"}, - }, { "(url) check ExceptDomains constraint", map[string]any{ @@ -695,16 +645,6 @@ func TestRecordDataValidatorValidateDate(t *testing.T) { nil, []string{"field2"}, }, - { - "(date) check unique constraint", - map[string]any{ - "field1": "2029-01-01 01:01:01.123", - "field2": "2029-01-01 01:01:01.123", - "field3": "2029-01-01 01:01:01.123", - }, - nil, - []string{"field3"}, - }, { "(date) check min date constraint", map[string]any{ @@ -827,26 +767,6 @@ func TestRecordDataValidatorValidateSelect(t *testing.T) { nil, []string{}, }, - { - "(select) check unique constraint", - map[string]any{ - "field1": "a", - "field2": "b", - "field3": []string{"a", "b", "c"}, - }, - nil, - []string{"field3"}, - }, - { - "(select) check unique constraint - same elements but different order", - map[string]any{ - "field1": "a", - "field2": "b", - "field3": []string{"a", "c", "b"}, - }, - nil, - []string{}, - }, { "(select) check Values constraint", map[string]any{ @@ -977,16 +897,6 @@ func TestRecordDataValidatorValidateJson(t *testing.T) { nil, []string{"field2"}, }, - { - "(json) check unique constraint", - map[string]any{ - "field1": `{"test":123}`, - "field2": `{"test":123}`, - "field3": map[string]any{"test": 123}, - }, - nil, - []string{"field3"}, - }, { "(json) check json text invalid obj, array and number normalizations", map[string]any{ @@ -1268,16 +1178,6 @@ func TestRecordDataValidatorValidateRelation(t *testing.T) { nil, []string{"field3"}, }, - { - "check unique constraint", - map[string]any{ - "field1": relId1, - "field2": relId2, - "field3": []string{relId1, relId2, relId3, relId3}, // repeating values are collapsed - }, - nil, - []string{"field3"}, - }, { "check nonexisting collection id", map[string]any{ diff --git a/models/schema/schema_field.go b/models/schema/schema_field.go index ab065153..5f74d137 100644 --- a/models/schema/schema_field.go +++ b/models/schema/schema_field.go @@ -131,8 +131,12 @@ type SchemaField struct { Name string `form:"name" json:"name"` Type string `form:"type" json:"type"` Required bool `form:"required" json:"required"` - Unique bool `form:"unique" json:"unique"` - Options any `form:"options" json:"options"` + + // Deprecated: This field is no-op and will be removed in future versions. + // Please use the collection.Indexes field to define a unique constraint. + Unique bool `form:"unique" json:"unique"` + + Options any `form:"options" json:"options"` } // ColDefinition returns the field db column type definition as string.