Add script for validating all JSON files #80

Merged
merged 1 commit into from Dec 22, 2012

Projects

None yet

2 participants

@adarsh
Contributor
adarsh commented Nov 23, 2012
  • Run via ruby lib/validate_json.rb
  • Add classes to validate JSON
  • Add Gemfile for supporting libraries
  • Add color to output messages
  • Add script to contribution guidelines
@croaky croaky and 1 other commented on an outdated diff Nov 23, 2012
CONTRIBUTING.md
@@ -13,7 +13,8 @@ Contributing
> In which format should my contributions be submitted?
* Pull requests should be in JSON format
-* PRs should be validated before submission using [jsonlint](https://github.com/zaach/jsonlint).
+* Run the json validator script (`ruby lib/json_validator.rb`)
+* For more detailed output, use [jsonlint](https://github.com/zaach/jsonlint).
@croaky
croaky Nov 23, 2012 Member

Does JSONLint provide much benefit over this approach? If yes, should it be used in the script? If no, maybe remove this line?

@adarsh
adarsh Nov 23, 2012 Contributor

JSONLint is implemented in Node - I prefer the pure ruby approach. I can't find another reasonable CLI tool to validate JSON. Suggestions welcome.

I'll update this to point to the web version.

@croaky croaky and 1 other commented on an outdated diff Nov 23, 2012
lib/json_validator.rb
+
+ def self.print_starting_message
+ puts 'Starting JSON validation'
+ puts
+ end
+
+ def self.run_validations_on_json_files
+ Dir['**/*.json'].each do |file_name|
+ JSONValidator.new(file_name).run
+ end
+ end
+
+ def self.print_closing_puts
+ puts
+ end
+end
@croaky
croaky Nov 23, 2012 Member

Feels a little strange to have a class with all class methods. Would be nice to convert them to instance methods.

@adarsh
adarsh Nov 23, 2012 Contributor

Yeah I tend to agree on second thought.

@croaky croaky commented on an outdated diff Nov 23, 2012
lib/json_validator.rb
+ private
+
+ def self.check_directory
+ if current_directory != 'trail-map'
+ print_directory_error
+ end
+ end
+
+ def self.current_directory
+ File.basename(Dir.getwd)
+ end
+
+ def self.print_directory_error
+ puts 'ERROR: Run from trail-map project root'.red
+ exit
+ end
@croaky
croaky Nov 23, 2012 Member

I think these three methods could be avoided by using File.dirname(__FILE__)

@croaky
Member
croaky commented Nov 23, 2012

Specs?

@adarsh
Contributor
adarsh commented Dec 13, 2012

@croaky Added specs. Please re-review

@croaky croaky commented on an outdated diff Dec 19, 2012
@@ -0,0 +1 @@
+rvm 1.9.3-p327
@croaky
croaky Dec 19, 2012 Member

Does it work to instead include the Ruby version in the Gemfile?

ruby '1.9.3'
@croaky croaky commented on an outdated diff Dec 19, 2012
@@ -0,0 +1,8 @@
+source :rubygems
+
+gem 'awesome_print'
+gem 'bourne'
+gem 'colored'
+gem 'json'
+gem 'rspec'
+gem 'mocha', require: false
@croaky
croaky Dec 19, 2012 Member

Sort alphabetically.

@croaky croaky commented on an outdated diff Dec 19, 2012
@@ -0,0 +1,8 @@
+source :rubygems
+
+gem 'awesome_print'
@croaky
croaky Dec 19, 2012 Member

Doesn't look like awesome_print is used. Can be removed?

@croaky croaky commented on an outdated diff Dec 19, 2012
lib/helpers/trail_runner.rb
@@ -0,0 +1,33 @@
+require 'awesome_print'
@croaky
croaky Dec 19, 2012 Member

Doesn't look like awesome_print is used. Can be removed?

@croaky croaky commented on the diff Dec 19, 2012
CONTRIBUTING.md
@@ -13,7 +13,8 @@ Contributing
> In which format should my contributions be submitted?
* Pull requests should be in JSON format
-* PRs should be validated before submission using [jsonlint](http://jsonlint.com/).
+* Run the json validator script (`ruby lib/json_validator.rb`)
+* For more detailed output, use [JSONlint.com](http://jsonlint.com).
@croaky
croaky Dec 19, 2012 Member

Is the idea that the Ruby-based JSON validator will catch errors and then you need to use JSON Lint to find out more information?

@adarsh
adarsh Dec 22, 2012 Contributor

Yes. I like the idea of including it in the output. Committed in 0786540065320680100abe811cdc15bb77894d05

@croaky croaky commented on an outdated diff Dec 19, 2012
lib/helpers/json_validator.rb
+
+ def valid_json?
+ begin
+ JSON.parse(@contents)
+ return true
+ rescue Exception => e
+ return false
+ end
+ end
+
+ def print_progress_marker
+ print '.'
+ end
+
+ def print_error_message
+ puts "\nERROR: #{@file_name} is not valid JSON."
@croaky
croaky Dec 19, 2012 Member

Maybe move the line from the README into the error output?

def print_error_message
  puts "\nERROR: #{@file_name} is not valid JSON. Learn more:"
  puts "cat #{@file_name} | pbcopy; open http://jsonlint.com"
end
Adarsh Pandit Add script for validating JSON
* Add classes to validate JSON
* Add Gemfile for supporting libraries
* Add script to contribution guidelines
* Split out classes into separate files
* Require gems in main TrailRunner class
* Expand error message to recommend jsonlint.com
* Write specs

Other changes:
* Move script into `lib/tasks/` and make directory-agnostic
* Sort gems alphabetically
* Don't require mocha
* Add awesome_print to Gemfile for debugging
* Clean up Gemfile
* Specify ruby version in Gemfile
* Sort gems
* Remove awesome_print (used only for diagnostics)
* Change mocha require to remove deprecation message
197b074
@adarsh adarsh merged commit 197b074 into master Dec 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment