cartodb/lib/carto/legend_definition_validator.rb

32 lines
790 B
Ruby
Raw Normal View History

2020-06-15 10:58:47 +08:00
require 'json-schema'
require_dependency 'carto/definition'
module Carto
class LegendDefinitionValidator
def initialize(type, definition)
@type = type
@definition = definition.with_indifferent_access if definition
end
def errors
return @errors if @errors
return ['could not be validated'] unless @definition && location
schema = Carto::Definition.instance.load_from_file(location)
@errors = JSON::Validator.fully_validate(schema, @definition)
end
private
LEGEND_FORMATS_LOCATION = 'lib/formats/legends/definitions'.freeze
def location
return @location if @location
location = "#{Rails.root}/#{LEGEND_FORMATS_LOCATION}/#{@type}.json"
@location = location if File.exists?(location)
end
end
end