jsv.rb 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require 'execjs'
2
+ require 'jsv/environment'
3
+
4
+ module JSV
5
+ ##
6
+ # Loads the JSV library code and a Javascript <-> Ruby shim.
7
+ #
8
+ # Usage:
9
+ #
10
+ # jsv = JSV::Context.new
11
+ # env = jsv.create_environment
12
+ # report = env.validate(json, schema)
13
+ #
14
+ # report.has_errors?
15
+ #
16
+ # # Error objects are JSON objects as described in JSV's README.
17
+ # report.errors # => [error object]
18
+ class Context
19
+ JS_CODE_PATH = File.expand_path('../../../js/build/jsv.rb.js', __FILE__)
20
+
21
+ def initialize
22
+ @execjs = ExecJS.compile(File.read(JS_CODE_PATH))
23
+ end
24
+
25
+ def create_environment(env = nil)
26
+ Environment.new(self, env)
27
+ end
28
+
29
+ def validate(env, json, schema)
30
+ @execjs.call('this.require("shim").validate',
31
+ env.env_id,
32
+ env.additional_schemas,
33
+ json,
34
+ schema)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ require 'jsv/report'
2
+
3
+ module JSV
4
+ class Environment
5
+ attr_reader :env_id
6
+ attr_reader :additional_schemas
7
+
8
+ def initialize(context, env_id)
9
+ @context = context
10
+ @env_id = env_id
11
+ @additional_schemas = {}
12
+ end
13
+
14
+ def validate(json, schema)
15
+ result = @context.validate(self, json, schema)
16
+
17
+ JSV::Report.new(result)
18
+ end
19
+
20
+ def create_schema(data, schema, uri)
21
+ @additional_schemas[uri] = { 'schema' => schema, 'data' => data }
22
+ end
23
+ end
24
+ end
data/lib/jsv/report.rb ADDED
@@ -0,0 +1,17 @@
1
+ module JSV
2
+ class Report
3
+ attr_reader :result
4
+
5
+ def initialize(result)
6
+ @result = result
7
+ end
8
+
9
+ def errors
10
+ result['errors']
11
+ end
12
+
13
+ def has_errors?
14
+ !errors.empty?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module JSV
2
+ VERSION = '0.0.1.pre'
3
+ end
data/lib/jsv.rb ADDED
@@ -0,0 +1 @@
1
+ require 'jsv/context'
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ module JSV
4
+ describe Environment do
5
+ let(:context) { JSV::Context.new }
6
+
7
+ describe 'for Draft 03' do
8
+ let(:env) { context.create_environment('json-schema-draft-03') }
9
+
10
+ describe '#validate' do
11
+ let(:schema) do
12
+ %Q{
13
+ {
14
+ "properties": {
15
+ "foo": {
16
+ "type": "integer"
17
+ }
18
+ }
19
+ }
20
+ }
21
+ end
22
+
23
+ it 'returns a report with zero errors for valid JSON' do
24
+ report = env.validate('{"foo":3}', schema)
25
+
26
+ report.should_not have_errors
27
+ end
28
+
29
+ it 'returns a report with errors for invalid JSON' do
30
+ report = env.validate('{"foo":"nope"}', schema)
31
+
32
+ report.should have_errors
33
+ end
34
+ end
35
+
36
+ describe '#create_schema' do
37
+ let(:referenced_schema) do
38
+ %Q{
39
+ {
40
+ "properties": {
41
+ "bar": {
42
+ "type": "integer"
43
+ }
44
+ }
45
+ }
46
+ }
47
+ end
48
+
49
+ let(:schema) do
50
+ %Q{
51
+ {
52
+ "properties": {
53
+ "foo": {
54
+ "extends": {
55
+ "$ref": "urn:fooSchema#"
56
+ },
57
+ "type": "object"
58
+ }
59
+ }
60
+ }
61
+ }
62
+ end
63
+
64
+ let(:json) do
65
+ %Q{
66
+ { "foo": { "bar": "baz" } }
67
+ }
68
+ end
69
+
70
+ it 'makes a schema available for use in validation' do
71
+ env.create_schema(referenced_schema, nil, 'urn:fooSchema#')
72
+
73
+ report = env.validate(json, schema)
74
+ report.errors.first['message'].should =~ /not a required type/
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'jsv'
4
+
5
+ RSpec.configure do |config|
6
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsv.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - David Yip
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: execjs
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: travis-lint
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Provides access to JSV, a JSON Schema validator, from Ruby
79
+ email:
80
+ - yipdw@member.fsf.org
81
+ executables:
82
+ - jsv-rb-repl
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - bin/jsv-rb-repl
87
+ - lib/jsv.rb
88
+ - lib/jsv/context.rb
89
+ - lib/jsv/environment.rb
90
+ - lib/jsv/report.rb
91
+ - lib/jsv/version.rb
92
+ - spec/jsv/environment_spec.rb
93
+ - spec/spec_helper.rb
94
+ - js/build/jsv.rb.js
95
+ - README
96
+ - LICENSE
97
+ homepage: https://github.com/NUBIC/jsv.rb
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 361765318298519476
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>'
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.1
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Run JSV in Ruby applications
124
+ test_files:
125
+ - spec/jsv/environment_spec.rb
126
+ - spec/spec_helper.rb