Add geo json validation
This commit is contained in:
parent
4c9305dbb1
commit
a7fb7bf1ef
2 changed files with 11 additions and 0 deletions
|
@ -42,6 +42,8 @@ class GeoArea < ApplicationRecord
|
|||
scope :selections_utilisateur, -> { where(source: sources.fetch(:selection_utilisateur)) }
|
||||
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
|
||||
|
||||
validates :geometry, geo_json: true, allow_blank: false
|
||||
|
||||
def to_feature
|
||||
{
|
||||
type: 'Feature',
|
||||
|
|
9
app/validators/geo_json_validator.rb
Normal file
9
app/validators/geo_json_validator.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class GeoJSONValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
begin
|
||||
RGeo::GeoJSON.decode(value.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
||||
rescue RGeo::Error::InvalidGeometry
|
||||
record.errors[attribute] << (options[:message] || "n'est pas un GeoJSON valide")
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue