Skip to content

Commit

Permalink
add kwalify yaml schema to verify
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengroat authored and Stephen Groat committed Feb 5, 2017
1 parent a94b31a commit fbdc6ee
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: ruby
cache: bundler
rvm:
- 2.4.0
before_install:
- gem update --system --no-doc
notifications:
email: false
slack:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'github-pages', '116', group: :jekyll_plugins

group :test do
gem 'fastimage'
gem 'kwalify'
gem 'rake'
gem 'rubocop'
end
2 changes: 1 addition & 1 deletion _data/finance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ websites:
img: freeagent.png
tfa: Yes
software: Yes
docs: https://www.freeagent.com/support/kb/getting-started/two-step-verification
doc: https://www.freeagent.com/support/kb/getting-started/two-step-verification

- name: FreshBooks
url: https://www.freshbooks.com
Expand Down
57 changes: 16 additions & 41 deletions verify.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require 'yaml'
require 'fastimage'
require 'kwalify'
@output = 0

# Should the script ignore checking for Twitter handles?
@ignore_twitter = false

# YAML tags that are obligatory to all listed sites.
@obligatory_tags = %w(url img name)

# YAML tags related to TFA 'YES'.
@tfa_yes_tags = %w(doc)

Expand All @@ -34,49 +29,23 @@ def error(msg)
end

# Test an individual YAML tag
# rubocop:disable AbcSize,CyclomaticComplexity,MethodLength,PerceivedComplexity
def test_tag(tag, required, tfa_state, website, only_true = false)
if website[tag].nil? && website['tfa'] == tfa_state && required
error("#{website['name']}: The required YAML tag \'#{tag}\' tag is "\
'not present.')
end
return if website[tag].nil?
if website['tfa'] != tfa_state
error("#{website['name']}: The YAML tag \'#{tag}\' should NOT be "\
"present when TFA is #{website['tfa'] ? 'enabled' : 'disabled'}.")
end
return unless only_true && website[tag] != true
error("#{website['name']}: The YAML tag \'#{tag}\' should either have"\
" a value set to \'Yes\' or not be used at all. (Current value:"\
" \'#{website[tag]}\')")
# rubocop:disable AbcSize,CyclomaticComplexity
def test_tag(tag, tfa_state, website)
return if website[tag].nil? || website['tfa'] == tfa_state
error("#{website['name']}: The YAML tag \'#{tag}\' should NOT be "\
"present when TFA is #{website['tfa'] ? 'enabled' : 'disabled'}.")
end
# rubocop:enable PerceivedComplexity

# Check the YAML tags
def test_tags(website)
tfa = website['tfa']
# rubocop:disable DoubleNegation
if !!tfa != tfa
error("#{website['name']}: The YAML tag \'{tfa}\' should be either "\
"\'Yes\' or \'No\'. (#{tfa})")
end
# rubocop:endable DoubleNegation

# Test tags that are obligatory
@obligatory_tags.each do |t|
next unless website[t].nil?
error("#{website['name']}: The required YAML tag \'#{t}\' tag is not"\
' present.')
end

# Test tags associated with TFA 'YES'
@tfa_yes_tags.each { |tfa_form| test_tag(tfa_form, false, true, website) }
@tfa_yes_tags.each { |tfa_form| test_tag(tfa_form, true, website) }

# Test TFA form tags'
@tfa_forms.each { |tfa_form| test_tag(tfa_form, false, true, website, true) }
@tfa_forms.each { |tfa_form| test_tag(tfa_form, true, website) }

# Test tags associated with TFA 'NO'
@tfa_no_tags.each { |tfa_form| test_tag(tfa_form, false, false, website) }
@tfa_no_tags.each { |tfa_form| test_tag(tfa_form, false, website) }
end

def test_img(img, name, imgs)
Expand All @@ -99,7 +68,7 @@ def test_img(img, name, imgs)
error("#{img} should not be larger than #{@img_max_size} bytes. It is"\
" currently #{img_size} bytes.")
end
# rubocop:enable AbcSize,CyclomaticComplexity,MethodLength
# rubocop:enable AbcSize,CyclomaticComplexity

begin

Expand All @@ -108,7 +77,13 @@ def test_img(img, name, imgs)
sections = YAML.load_file('_data/sections.yml')
sections.each do |section|
data = YAML.load_file('_data/' + section['id'] + '.yml')
schema = YAML.load_file('websites_schema.yml')
websites = data['websites']
validator = Kwalify::Validator.new(schema)
errors = validator.validate(data)
if errors && !errors.empty?
errors.each { |e| error("[#{section['id']}/#{e.path}] #{e.message}") }
end

# Check section alphabetization
error("_data/#{section['id']}.yml is not alphabetized by name") \
Expand Down
54 changes: 54 additions & 0 deletions websites_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
type: map
mapping:
"websites":
type: seq
sequence:
- type: map
mapping:
"name":
type: str
required: yes
unique: yes
"url":
type: str
required: yes
unique: yes
"img":
type: str
required: yes
"tfa":
type: bool
required: yes
"software":
type: bool
pattern: /true/
"hardware":
type: bool
pattern: /true/
"sms":
type: bool
pattern: /true/
"phone":
type: bool
pattern: /true/
"exceptions":
type: map
mapping:
"text":
type: str
required: yes
"doc":
type: str
"twitter":
type: str
"facebook":
type: str
"email":
type: bool
pattern: /true/
"email_address":
type: str
"status":
type: str
"lang":
type: str

0 comments on commit fbdc6ee

Please sign in to comment.