Sha256: cf50bc00af2b70a2d00710e698dfb4506b2af751b8f31de00e6a913b202edabc

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'geojsonlint/version'
require 'json'
require 'json-schema'

if defined?(ActiveModel)
  require 'geojsonlint/geojson_validator'
end

module Geojsonlint

  class Parser
    attr_reader :schema, :data

    # @param data [String, Hash]
    def initialize(data)
      @data = data
      @schema = JSON.parse(File.read('./lib/geojsonlint/geojson_schema.json'))
      self
    end

    # @return [Geojson]
    def run
      options = {
        errors_as_objects: true,
        validate_schema: true
      }

      geojson = Geojson.new
      geojson.errors = JSON::Validator.fully_validate(@schema, @data, **options)

      geojson
    end
  end

  class Geojson
    attr_accessor :errors

    # @return [Geojson]
    def initialize
      self.errors = Array.new
      self
    end

    # @return [Boolean]
    def valid?
      !invalid?
    end

    # @return [Boolean]
    def invalid?
      errors.any?
    end
  end

  # Validates a geojson object
  # @param geojson [String, Hash] a JSON string or a Ruby object representing JSON data
  #
  # @return [Geojson]
  def validate(geojson)
    Parser.new(geojson).run
  end
  module_function :validate
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geojsonlint-0.1.1 lib/geojsonlint.rb
geojsonlint-0.1.0 lib/geojsonlint.rb