demarches-normaliennes/app/validators/geo_json_validator.rb

14 lines
476 B
Ruby
Raw Normal View History

# frozen_string_literal: true
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
unless value.blank? || GeojsonService.valid?(value)
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