Skip to content

Commit

Permalink
fix(server): geojson validation bug (#1228)
Browse files Browse the repository at this point in the history
fix: multiple geo bug
  • Loading branch information
nourbalaha authored Sep 13, 2024
1 parent c6a76a3 commit b60c873
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 5 additions & 0 deletions server/pkg/schema/field_geometry_editor.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions server/pkg/schema/field_geometry_object.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions server/pkg/schema/field_geometry_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit b60c873

Please sign in to comment.