2021-03-11 10:22:49 +01:00
|
|
|
class GeoJSONValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2023-04-21 17:26:28 +02:00
|
|
|
if options[:allow_nil] == false && value.nil?
|
|
|
|
record.errors.add(attribute, :blank, message: options[:message] || "ne peut pas être vide")
|
|
|
|
end
|
|
|
|
|
2021-03-11 10:22:49 +01:00
|
|
|
begin
|
|
|
|
RGeo::GeoJSON.decode(value.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
|
|
|
rescue RGeo::Error::InvalidGeometry
|
2023-04-21 17:26:28 +02:00
|
|
|
record.errors.add(attribute, :invalid_geometry, message: options[:message] || "n'est pas un GeoJSON valide")
|
2021-03-11 10:22:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|