# File lib/edi4r/edifact.rb, line 1263
    def validate( err_count=0 )
      # Check sequence of segments against library,
      # thereby adding location information to each segment

      par = {
        :d0065 => @name, :d0052=> @version, :d0054=> @release, 
        :d0051 => @resp_agency, :d0057 => @subset, 
        :d0002 => root.version, :is_iedi => root.is_iedi?,
        :d0076 => nil  # SV 4-1 support still missing here
      }
      diag = EDI::Diagram::Diagram.create( root.syntax, par )
      ni = EDI::Diagram::NodeInstance.new(diag)

      ni.seek!( @header )
      @header.update_with( ni )
      each do |seg| 
        if ni.seek!(seg)
          seg.update_with( ni )
        else
          # FIXME: Do we really have to fail here, or would a "warn" suffice?
          raise "seek! failed for #{seg.name} when starting at #{ni.name}"
        end 
      end
      ni.seek!( @trailer )
      @trailer.update_with( ni )


      # Consistency checks

      if (a=@trailer.d0074) != (b=self.size+2)
        warn "DE 0074 (#{a}) does not match number of segments (#{b})"
        err_count += 1
      end

      if root.is_iedi?
        a, b = @trailer.d0340, @header.d0340
      else
        a, b = @trailer.d0062, @header.d0062
      end
      if a != b
        warn "Trailer reference (#{a}) does not match header reference (#{b})"
        err_count += 1
      end

      if parent.is_a? MsgGroup
        ung = parent.header; s008 = ung.cS008; s009 = header.cS009
        a, b = s009.d0065, ung.d0038
        if a != b
          warn "Message type (#{a}) does not match that of group (#{b})"
          err_count += 1
        end
        a, b = s009.d0052, s008.d0052
        if a != b
          warn "Message version (#{a}) does not match that of group (#{b})"
          err_count += 1
        end
        a, b = s009.d0054, s008.d0054
        if a != b
          warn "Message release (#{a}) does not match that of group (#{b})"
          err_count += 1
        end
        a, b = s009.d0051, ung.d0051
        if a != b
          warn "Message responsible agency (#{a}) does not match that of group (#{b})"
          err_count += 1
        end
        a, b = s009.d0057, s008.d0057
        if a != b
          warn "Message association assigned code (#{a}) does not match that of group (#{b})"
          err_count += 1
        end

      end

      # Now check each segment
      super( err_count )
    end