Files
nairobi/app/models/concerns/geojson_format_validator.rb
Matheus Miranda de13e789dd Add polygon geographies to Budgets' map
Note that in the budgets wizard test we now create district with no
associated geozone, so the text "all city" will appear in the districts
table too, meaning we can't use `within "section", text: "All city" do`
anymore since it would result in an ambiguous match.

Co-Authored-By: Julian Herrero <microweb10@gmail.com>
Co-Authored-By: Javi Martín <javim@elretirao.net>
2023-05-31 16:56:15 +02:00

24 lines
502 B
Ruby

class GeojsonFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.present?
geojson = parse_json(value)
unless geojson?(geojson)
record.errors.add(attribute, :invalid)
end
end
end
private
def parse_json(geojson_data)
JSON.parse(geojson_data) rescue nil
end
def geojson?(geojson)
return false unless geojson.is_a?(Hash)
geojson.dig("geometry", "coordinates").is_a?(Array)
end
end