demarches-normaliennes/app/validators/geo_json_validator.rb

10 lines
350 B
Ruby
Raw Normal View History

2021-03-11 10:22:49 +01:00
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