Sha256: d2e11f519ea672ffbaa8deae3f6a2653df5d3d11939252d14b75d097c727447e

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

require 'json-schema'

module Contentful
  module Importer
    class JsonSchemaValidator

      attr_reader :config, :logger

      def initialize(configuration)
        @config = configuration
        @logger = Logger.new(STDOUT)
      end

      def validate_schemas
        Dir.glob("#{config.collections_dir}/*") do |content_type_file|
          validate_schema(content_type_file)
        end
      end

      def validate_schema(content_type_file)
        schema = parse_content_type_schema(JSON.parse(File.read(content_type_file)))
        content_type_filename = File.basename(content_type_file, '.*')
        validate_entry(content_type_filename, schema)
      end

      def validate_entry(content_type_filename, schema)
        Dir.glob("#{config.entries_dir}/#{content_type_filename}/*") do |entry_file|
          entry_schema = JSON.parse(File.read(entry_file))
          begin
            JSON::Validator.validate!(schema, entry_schema)
          rescue JSON::Schema::ValidationError => error
            logger.info "#{error.message}! Path to invalid entry: #{entry_file}"
          end
        end
      end

      def parse_content_type_schema(ct_file)
        new_hash = base_schema_format
        ct_file['fields'].each do |key|
          type = convert_type(key['type'])
          new_hash['properties'].merge!({key['id'] => {'type' => type}})
        end
        new_hash
      end

      def base_schema_format
        {'type' => 'object', 'properties' => {}}
      end

      def convert_type(type)
        case type
          when 'Text', 'Date', 'Symbol'
            'string'
          when 'Number'
            'float'
          when 'Asset', 'Entry'
            'object'
          else
            type.downcase
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
contentful-importer-0.2.2 lib/contentful/importer/json_schema_validator.rb
contentful-importer-0.2.1 lib/contentful/importer/json_schema_validator.rb
contentful-importer-0.2.0 lib/contentful/importer/json_schema_validator.rb
contentful-importer-0.1.1 lib/contentful/importer/json_schema_validator.rb
contentful-importer-0.1.0 lib/contentful/importer/json_schema_validator.rb