Class: Validate
- Inherits:
-
Object
- Object
- Validate
- Defined in:
- lib/kaba/validate.rb
Overview
用于将所有数据进行验证
Defined Under Namespace
Classes: ValidateReponse
Instance Method Summary collapse
-
#initialize(schema:, type_name:) ⇒ Validate
constructor
A new instance of Validate.
- #run(input) ⇒ Object
-
#run_file(file) ⇒ Object
读取某个文件然后运行.
-
#run_files(dir, limit: nil, &block) ⇒ Object
读取某个文件夹下的然后运行,运行有结果了 block 会被调用 limit 用于限制读取的文件数量, 为 nil 时读取所有文件.
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. 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.(dir), '*.target.json')] files = files.first(limit) if limit = 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, ) else unless response.success? .log "validate failed".colorize(:red) response.to_s.split("\n").each do |line| .log line end .log "\n" end end .advance end end end.wait end |