demarches-normaliennes/app/validators/geo_json_validator.rb
2021-03-11 15:51:09 +01:00

9 lines
350 B
Ruby

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