diff options
author | Tulio Leao <tupaschoal@gmail.com> | 2020-06-06 00:23:59 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-06 00:23:59 -0300 |
commit | ff1145ddec30f378b837f597ea9923a8ef3b0d86 (patch) | |
tree | c4f02e8ee12ddabf5eb3bfa448b610778da7f267 /script/validate_json.rb | |
parent | e63a7fd45226d2dc9c316c12de474ea4b69044f6 (diff) |
Make sure linting disallow unsupported json keys (#526)
* Make sure linting disallow unsupported json keys
Co-authored-by: Jordan <Nightfirecat@users.noreply.github.com>
Diffstat (limited to 'script/validate_json.rb')
-rwxr-xr-x | script/validate_json.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/script/validate_json.rb b/script/validate_json.rb index d4a3c671..c3b4ff45 100755 --- a/script/validate_json.rb +++ b/script/validate_json.rb @@ -16,9 +16,12 @@ module ExitCodes UNEXPECTED_DIFFICULTY = 8 # Unexpected value for 'difficulty' field UNEXPECTED_LANGUAGE = 9 # Unexpected language code for 'url_code' field UNEXPECTED_LANGUAGE_KEY = 10 # Unexpected language key for translation + UNSUPPORTED_FIELD = 11 # Unsupported field for site entry + UNEXPECTED_NOTES = 12 # Unexpected notes key for translation end SupportedDifficulties = ["easy", "medium", "hard", "impossible"] +SupportedEntryKeys = ["difficulty", "domains", "email", "email_body", "email_subject", "meta", "name", "notes", "url"] SupportedLanguageKeys = ["about", "difficulty", "difficulty_easy", "difficulty_hard", "difficulty_impossible", "difficulty_medium", "extension", "extensionguide", "extensionp1", "extensionp2", "extensionp3", "extensionp4", "extensionp5", "extensionp6", "footercredits", "fork", @@ -37,6 +40,23 @@ def get_transformed_name(site_object) return site_object['name'].downcase.sub(/^the\s+/, '') end +def validate_accepted_keys(key) + key.keys.each do |entry_key| + if entry_key.start_with?('url_') || entry_key.start_with?('notes_') + # These have their own validation methods + next + end + + unless SupportedEntryKeys.include?(entry_key) + STDERR.puts "Entry '#{key['name']}' has unsupported field: "\ + "'#{entry_key}'.\n"\ + "Use one of the supported fields:\n"\ + "\t#{SupportedEntryKeys}" + exit ExitCodes::UNSUPPORTED_FIELD + end + end +end + def error_on_missing_field(key, field, exit_code) unless key.key?(field) STDERR.puts "Entry '#{key['name']}' has no '#{field}' field" @@ -67,16 +87,30 @@ def validate_localized_urls(key) end end +def validate_localized_notes(key) + key.keys.each do |entry_key| + if entry_key.start_with?('notes_') && !SupportedLanguages.any? { |lang| entry_key.eql?("notes_#{lang}") } + STDERR.puts "Entry '#{key['name']}' has unrecognized notes code: "\ + "'#{entry_key}'.\n"\ + "Use one of the supported languages:\n"\ + "\t#{SupportedLanguages}" + exit ExitCodes::UNEXPECTED_NOTES + end + end +end + def validate_website_entry(key, i) unless key.key?('name') STDERR.puts "Entry #{i} has no 'name' field" exit ExitCodes::MISSING_NAME end + validate_accepted_keys(key) error_on_missing_field(key, 'url', ExitCodes::MISSING_URL) error_on_missing_field(key, 'difficulty', ExitCodes::MISSING_DIFFICULTY) error_on_missing_field(key, 'domains', ExitCodes::MISSING_DOMAINS) validate_difficulty(key) validate_localized_urls(key) + validate_localized_notes(key) end def add_valid_language_key(keys_in_language_json, key, file) |