Class: Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/kaba/validate.rb

Overview

用于将所有数据进行验证

Defined Under Namespace

Classes: ValidateReponse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, type_name:) ⇒ Validate



3
4
5
6
# File 'lib/kaba/validate.rb', line 3

def initialize(schema:, type_name:)
  @schema = schema
  @type_name = type_name
end

Instance Method Details

#run(input) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/kaba/validate.rb', line 8

def run(input)
  request_body = {
    schema: @schema,
    typeName: @type_name,
    jsonData: input
  }

  Application.connection.post('/validate', request_body)
end

#run_file(file) ⇒ Object

读取某个文件然后运行



19
20
21
22
# File 'lib/kaba/validate.rb', line 19

def run_file(file)
  input = JSON.parse File.read(File.expand_path file)
  ValidateReponse.new run(input), file: file
end

#run_files(dir, limit: nil, &block) ⇒ Object

读取某个文件夹下的然后运行,运行有结果了 block 会被调用limit 用于限制读取的文件数量, 为 nil 时读取所有文件



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kaba/validate.rb', line 26

def run_files(dir, limit: nil, &block)
  files = Dir[File.join(File.expand_path(dir), '*.target.json')]
  files = files.first(limit) if limit

  progressbar = TTY::ProgressBar.new("Validate: [:bar] :percent :current/:total", total: files.size)

  Async do
    files.each do |file|
      Async do
        input = JSON.parse(File.read(file))
        response = ValidateReponse.new run(input), file: file

        if block
          block.call(response, progressbar)
        else
          unless response.success?
            progressbar.log "validate failed".colorize(:red)
            response.to_s.split("\n").each do |line|
              progressbar.log line
            end
            progressbar.log "\n"
          end
        end
        progressbar.advance
      end
    end
  end.wait
end