Permalink
Browse files

Add json schema validator

  • Loading branch information...
1 parent 3c5eb85 commit 7481bf9b8f00e16f5defabe4a84ba69127dfc1be @masonforest masonforest committed Apr 24, 2013
Showing with 118 additions and 9 deletions.
  1. +2 −1 Gemfile
  2. +2 −0 Gemfile.lock
  3. +68 −0 config/schema.json
  4. +9 −8 lib/helpers/json_validator.rb
  5. +26 −0 spec/fixtures/non_compliant.json
  6. +11 −0 spec/json_validator_spec.rb
View
@@ -2,11 +2,12 @@ source 'https://rubygems.org'
ruby '2.0.0'
-gem 'rake'
gem 'bourne'
gem 'colored'
gem 'httparty'
gem 'json'
+gem 'json-schema'
gem 'mocha', require: false
+gem 'rake'
gem 'rspec'
gem 'webmock'
View
@@ -11,6 +11,7 @@ GEM
multi_json (~> 1.0)
multi_xml (>= 0.5.2)
json (1.7.7)
+ json-schema (1.1.1)
metaclass (0.0.1)
mocha (0.13.2)
metaclass (~> 0.0.1)
@@ -37,6 +38,7 @@ DEPENDENCIES
colored
httparty
json
+ json-schema
mocha
rake
rspec
View
@@ -0,0 +1,68 @@
+{
+ "type": "object",
+ "$schema": "http://json-schema.org/draft-03/schema",
+ "required": true,
+ "properties": {
+ "description": {
+ "type": "string",
+ "id": "description",
+ "required": true
+ },
+ "name": {
+ "type": "string",
+ "id": "name",
+ "required": true
+ },
+ "steps": {
+ "type": "array",
+ "required": true,
+ "items": {
+ "type": "object",
+ "required": true,
+ "properties": {
+ "name": {
+ "type": "string",
+ "required": true
+ },
+ "resources": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "required": true,
+ "properties": {
+ "id": {
+ "type": "string",
+ "required": true
+ },
+ "title": {
+ "type": "string",
+ "required": true
+ },
+ "uri": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "validations": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "required": true,
+ "properties": {
+ "id": {
+ "type": "string",
+ "required": true
+ },
+ "title": {
+ "type": "string",
+ "required": true
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
@@ -1,4 +1,8 @@
+require 'json-schema'
+
class JSONValidator
+ SCHEMA = 'config/schema.json'
+
def initialize(file_name)
@file_name = file_name
@contents = File.open(@file_name).read
@@ -21,20 +25,17 @@ def check_json_validation
end
def valid_json?
- begin
- JSON.parse(@contents)
- return true
- rescue Exception => e
- return false
- end
+ @errors = JSON::Validator.fully_validate(SCHEMA, @contents)
+
+ @errors == []
end
def print_progress_marker
print '.'
end
def print_error_message
- puts "\nERROR: #{@file_name} is not valid JSON. Learn more:"
- puts "cat #{@file_name} | pbcopy; open http://jsonlint.com"
+ puts "\nERROR: #{@file_name} is not valid JSON."
+ puts @errors
end
end
@@ -0,0 +1,26 @@
+{
+ "description": "A multitasking, multi-user computer operating system originally developed in 1969, variants of which now form the underpinning of many operating systems, including OS X, iOS, and Linux.",
+ "steps": [
+ {
+ "name": "Critical Learning",
+ "resources": [
+ {
+ "title": "Read The Linux Programming Interface. Chapters 1, 2, 6, 8, 10, 14, 15, 18, 25, 44, 62.",
+ "uri": "http://amzn.to/the-linux-programming-interface",
+ "id": "3e5e1e86286462f7740038c7998ad900080f2cbf"
+ },
+ {
+ "title": "Read about I/O redirection.",
+ "uri": "http://en.wikipedia.org/wiki/Redirection_%28computing%29",
+ "id": "821a57aacb161859742895bddaf3d2ecfda188cf"
+ }
+ ],
+ "validations": [
+ {
+ "title": "You can use these commands and operators: |, <, >, >>, &, ack, awk, cat, chmod, chown, cp, export, find, kill, locate, ls, mkdir, mv, ps, rm, sed, sort, tail, top, vim, whereis, xargs",
+ "id": "ba03a2224008f4078b941e61050f7623164372b9"
+ }
+ ]
+ }
+ ]
+}
@@ -23,4 +23,15 @@
result.should be_false
io.string.should =~ /ERROR/
end
+
+ it 'returns false and outputs an error if the JSON is non compliant' do
+ $stdout = io = StringIO.new
+ dir = File.dirname(__FILE__)
+ file_name = File.open(dir + '/fixtures/non_compliant.json')
+
+ result = JSONValidator.new(file_name).run
+
+ result.should be_false
+ io.string.should =~ /ERROR/
+ end
end

0 comments on commit 7481bf9

Please sign in to comment.