diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 5d035c5286..704eedf796 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -50,4 +50,6 @@ jobs: - name: Install ruby dependencies run: bundle install --jobs 4 --retry 3 - name: Validate YAML - run: USEKWALIFY="yes" bundle exec rake lint + run: | + bundle exec rake lint + USEKWALIFY="yes" bundle exec rake lint \ No newline at end of file diff --git a/Gemfile b/Gemfile index cc11e8934a..b547f12aa6 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' gem 'rake' gem 'faraday', '~> 2.0' +gem 'kwalify', '~> 0.1' gem 'json_schemer', '~> 2.0' gem 'rspec', '~> 3.0' diff --git a/Rakefile b/Rakefile index 7613feaf5d..0d8972aa97 100644 --- a/Rakefile +++ b/Rakefile @@ -10,6 +10,16 @@ namespace :lint do abort "Please run `gem install rspec` to install RSpec." end end + + # json + RSpec::Core::RakeTask.new(:schema) do |t| + t.pattern = 'spec/schema_validation_spec.rb' + end + + # non-kwalify and kwalify + RSpec::Core::RakeTask.new(:yaml) do |t| + t.exclude_pattern = 'spec/schema_validation_spec.rb' + end end desc "Sync GitHub RubyGem Advisories into this project" @@ -18,5 +28,5 @@ task :sync_github_advisories, [:gem_name] do |_, args| GitHub::GitHubAdvisorySync.sync(gem_name: args[:gem_name]) end -task :lint => ['lint:yaml'] +task :lint => [ 'lint:schema', 'lint:yaml' ] task :default => :lint diff --git a/spec/gem_advisory_example.rb b/spec/gem_advisory_example.rb index 69e5fc4116..5753cf719f 100644 --- a/spec/gem_advisory_example.rb +++ b/spec/gem_advisory_example.rb @@ -51,5 +51,16 @@ end end + if ENV['USEKWALIFY'] == 'yes' + let(:schema_file) { File.join(__dir__, 'schemas/gem.yml') } + + it "should have valid schema" do + schema = YAML.safe_load_file(schema_file) + validator = Kwalify::Validator.new(schema) + errors = validator.validate(advisory) + + expect(errors).to be_empty + end + end end end diff --git a/spec/ruby_advisory_example.rb b/spec/ruby_advisory_example.rb index de5c125935..a004eb2318 100644 --- a/spec/ruby_advisory_example.rb +++ b/spec/ruby_advisory_example.rb @@ -19,5 +19,16 @@ end end + if ENV['USEKWALIFY'] == 'yes' + let(:schema_file) { File.join(__dir__, 'schemas/ruby.yml') } + + it "should have valid schema" do + schema = YAML.safe_load_file(schema_file) + validator = Kwalify::Validator.new(schema) + errors = validator.validate(advisory) + + expect(errors).to be_empty + end + end end end diff --git a/spec/schema_validation_spec.rb b/spec/schema_validation_spec.rb index 6ba619c789..bf88f5a2c0 100644 --- a/spec/schema_validation_spec.rb +++ b/spec/schema_validation_spec.rb @@ -66,10 +66,15 @@ def format_errors(errors) filename = path.split('/')[-2..].join('/') it "#{filename} conforms to schema" do - raw_data = YAML.safe_load_file(path, permitted_classes: [Date]) - data = normalize_for_json(raw_data) - errors = raw_yaml_field_checks(raw_data) + schemer.validate(data).to_a - + if ENV['USEKWALIFY'] == 'yes' + data = normalize_for_json(YAML.safe_load_file(path, + permitted_classes: [Date])) + errors = schemer.validate(data).to_a + else + raw_data = YAML.safe_load_file(path, permitted_classes: [Date]) + data = normalize_for_json(raw_data) + errors = raw_yaml_field_checks(raw_data) + schemer.validate(data).to_a + end expect(errors).to be_empty, lambda { "#{filename}\n#{format_errors(errors)}" } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7dc07a4ae0..abf2fcdc42 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,12 @@ +require 'strscan' + +class StringScanner + def peep(len) + peek(len) + end +end +require 'kwalify' + require 'date' require 'rspec'