forked from stephanpavlovic/blood-bank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_texts.rb
30 lines (25 loc) · 1.05 KB
/
validate_texts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'minitest/autorun'
require 'json-schema'
require 'json'
schema_file = File.join(File.dirname(File.expand_path(__FILE__)) , 'schema', 'text.json')
texts_path = File.join(File.dirname(File.expand_path(__FILE__)) , 'texts')
schema = File.open(schema_file) { |f| JSON.parse(f.read) }
describe 'texts' do
Dir["#{texts_path}/*\.*"].each do |file|
it "#{file.sub(/(\..*$)/,'').sub(/^#{texts_path}/,'')} contains valid json" do
begin
json_str = File.open(file) { |f| f.read }
# force_encoding only available in ruby 1.9.2
job = JSON.parse( json_str.respond_to?(:force_encoding ) ? json_str.force_encoding('UTF-8') : json_str)['text']
JSON::Validator.validate!(schema, job, :version=> :draft3 )
rescue JSON::Schema::ValidationError => schema_error
assertion = false, schema_error.message + "\nIn: #{file}\nplease fix this!"
rescue JSON::ParserError => parser_error
assertion = false, parser_error.message
else
assertion = true
end
assert *assertion
end
end
end