From b60c873d247d26d70530b068b0ae124cffbcbd89 Mon Sep 17 00:00:00 2001 From: Nour Balaha Date: Fri, 13 Sep 2024 10:31:41 +0900 Subject: [PATCH] fix(server): geojson validation bug (#1228) fix: multiple geo bug --- server/pkg/schema/field_geometry_editor.go | 5 +++++ server/pkg/schema/field_geometry_object.go | 5 +++++ server/pkg/schema/field_geometry_validation.go | 6 +----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/server/pkg/schema/field_geometry_editor.go b/server/pkg/schema/field_geometry_editor.go index fa29386127..fd142db0cc 100644 --- a/server/pkg/schema/field_geometry_editor.go +++ b/server/pkg/schema/field_geometry_editor.go @@ -1,6 +1,8 @@ package schema import ( + "strings" + "github.com/reearth/reearth-cms/server/pkg/value" "github.com/samber/lo" "golang.org/x/exp/slices" @@ -54,6 +56,9 @@ func (f *FieldGeometryEditor) Clone() *FieldGeometryEditor { func (f *FieldGeometryEditor) Validate(v *value.Value) (err error) { v.Match(value.Match{ GeometryEditor: func(a value.String) { + if len(strings.TrimSpace(a)) == 0 { + return + } t, ok := isValidGeoJSON(a) if !ok { err = ErrInvalidValue diff --git a/server/pkg/schema/field_geometry_object.go b/server/pkg/schema/field_geometry_object.go index b090764fd0..4215d44086 100644 --- a/server/pkg/schema/field_geometry_object.go +++ b/server/pkg/schema/field_geometry_object.go @@ -1,6 +1,8 @@ package schema import ( + "strings" + "github.com/reearth/reearth-cms/server/pkg/value" "github.com/samber/lo" "golang.org/x/exp/slices" @@ -51,6 +53,9 @@ func (f *FieldGeometryObject) Clone() *FieldGeometryObject { func (f *FieldGeometryObject) Validate(v *value.Value) (err error) { v.Match(value.Match{ GeometryObject: func(a value.String) { + if len(strings.TrimSpace(a)) == 0 { + return + } t, ok := isValidGeoJSON(a) if !ok { err = ErrInvalidValue diff --git a/server/pkg/schema/field_geometry_validation.go b/server/pkg/schema/field_geometry_validation.go index 15f058f160..484c21bdfa 100644 --- a/server/pkg/schema/field_geometry_validation.go +++ b/server/pkg/schema/field_geometry_validation.go @@ -2,9 +2,9 @@ package schema import ( "encoding/json" + "github.com/reearth/reearthx/i18n" "github.com/reearth/reearthx/rerror" - "strings" geojson "github.com/paulmach/go.geojson" "golang.org/x/exp/slices" @@ -14,10 +14,6 @@ var ErrUnsupportedType = rerror.NewE(i18n.T("unsupported geometry type")) // isValidGeoJSON uses the go.geojson library to validate a GeoJSON string func isValidGeoJSON(data string) (geojson.GeometryType, bool) { - if len(strings.TrimSpace(data)) == 0 { - return "", false - } - var raw map[string]interface{} if err := json.Unmarshal([]byte(data), &raw); err != nil { return "", false