Sha256: 2cecf93575330b1f06e92dd51221f8308fe28c2ede6482284656789851445f8b

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module Csvlint
  
  class Schema
    
    include Csvlint::ErrorCollector
    
    attr_reader :uri, :fields, :title, :description
    
    def initialize(uri, fields=[], title=nil, description=nil)
      @uri = uri
      @fields = fields
      @title = title
      @description = description
      reset
    end

    def validate_header(header)
      reset
      header.each_with_index do |name,i|
        build_warnings(:header_name, :schema, nil, i+1, name) if fields[i].name != name
      end
      return valid?
    end
        
    def validate_row(values, row=nil)
      reset
      if values.length < fields.length
        fields[values.size..-1].each_with_index do |field, i|
          build_warnings(:missing_column, :schema, row, values.size+i+1)
        end
      end
      if values.length > fields.length
        values[fields.size..-1].each_with_index do |data_column, i|
          build_warnings(:extra_column, :schema, row, fields.size+i+1)
        end
      end
      
      fields.each_with_index do |field,i|
        value = values[i] || ""
        result = field.validate_column(value, row, i+1)
        @errors += fields[i].errors
        @warnings += fields[i].warnings        
      end
            
      return valid?
    end
    
    def Schema.from_json_table(uri, json)
      fields = []
      json["fields"].each do |field_desc|
        fields << Csvlint::Field.new( field_desc["name"] , field_desc["constraints"], 
          field_desc["title"], field_desc["description"] )
      end if json["fields"]
      return Schema.new( uri , fields, json["title"], json["description"] )
    end
    
    def Schema.load_from_json_table(uri)
      begin
        json = JSON.parse( open(uri).read )
        return Schema.from_json_table(uri,json)
      rescue
        return nil
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csvlint-0.1.1 lib/csvlint/schema.rb