refactored LegendDefinitionValidator

pull/9809/head
Guido Fioravantti 8 years ago
parent 3d56ac9a52
commit 331eab52dd

@ -6,22 +6,27 @@ require_dependency 'carto/definition'
module Carto
class LegendDefinitionValidator
VALIDATION_FORMAT_LOCATIONS_MAP = {
bubble: 'lib/formats/legends/bubble.json',
category: 'lib/formats/legends/category.json',
choropleth: 'lib/formats/legends/choropleth.json',
custom: 'lib/formats/legends/custom.json',
html: 'lib/formats/legends/html.json'
}.with_indifferent_access.freeze
def self.errors(type, definition)
raw_schema_location = VALIDATION_FORMAT_LOCATIONS_MAP[type]
return ['could not be validated'] unless definition && raw_schema_location
schema_location = "#{Rails.root}/#{raw_schema_location}"
schema = Carto::Definition.instance.load_from_file(schema_location)
def initialize(type, definition)
@type = type
@definition = definition
end
def errors
return ['could not be validated'] unless definition && location
schema = Carto::Definition.instance.load_from_file(schema_location)
JSON::Validator.fully_validate(schema, definition.with_indifferent_access)
end
private
LEGEND_FORMATS_LOCATION = 'lib/formats/legends/'.freeze
def location
return @location if @location
location = "#{Rails.root}/#{LEGEND_FORMATS_LOCATION}/#{@type}.json"
@location = location if File.exists?(location)
end
end
end

Loading…
Cancel
Save