jschematic 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/Rakefile +3 -0
- data/features/default.feature +17 -0
- data/features/required.feature +17 -0
- data/jschematic.gemspec +8 -5
- data/lib/jschematic/attributes/extends.rb +4 -2
- data/lib/jschematic/attributes/properties.rb +2 -2
- data/lib/jschematic/attributes/ref.rb +1 -1
- data/lib/jschematic/attributes/required.rb +2 -0
- data/lib/jschematic/context.rb +31 -0
- data/lib/jschematic/element.rb +5 -1
- data/lib/jschematic/schema.rb +7 -22
- data/lib/jschematic/version.rb +3 -0
- data/spec/jschematic/attributes/ref_spec.rb +1 -2
- metadata +100 -82
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -40
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Rakefile
CHANGED
data/features/default.feature
CHANGED
@@ -67,5 +67,22 @@ Feature: Core schema: default
|
|
67
67
|
Then '{ "somethingElse": "foo" }' is not valid JSON
|
68
68
|
But '{ "pi": 3.1415 }' is valid JSON
|
69
69
|
|
70
|
+
Scenario: does not fall back for boolean false
|
71
|
+
When the schema is:
|
72
|
+
"""
|
73
|
+
{
|
74
|
+
"properties": {
|
75
|
+
"enabled": {
|
76
|
+
"type": "boolean",
|
77
|
+
"required": true,
|
78
|
+
"default": "is delicious"
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
"""
|
83
|
+
Then '{ "enabled": false }' is valid JSON
|
84
|
+
But '{}' is not valid JSON
|
85
|
+
|
86
|
+
|
70
87
|
Scenario: default for all types
|
71
88
|
TODO: spec
|
data/features/required.feature
CHANGED
@@ -24,6 +24,7 @@ Feature: Core schema: required
|
|
24
24
|
}
|
25
25
|
"""
|
26
26
|
Then '{}' is valid JSON
|
27
|
+
Then '{ "color": "red" }' is valid JSON
|
27
28
|
|
28
29
|
Scenario: true
|
29
30
|
When the schema is:
|
@@ -40,3 +41,19 @@ Feature: Core schema: required
|
|
40
41
|
Then '{ "color": "red" }' is valid JSON
|
41
42
|
But '{}' is not valid JSON
|
42
43
|
And '{ "age": 24 }' is not valid JSON
|
44
|
+
|
45
|
+
Scenario: true for boolean value
|
46
|
+
When the schema is:
|
47
|
+
"""
|
48
|
+
{
|
49
|
+
"properties": {
|
50
|
+
"enabled": {
|
51
|
+
"type": "boolean",
|
52
|
+
"required": true
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
"""
|
57
|
+
Then '{ "enabled": false }' is valid JSON
|
58
|
+
But '{}' is not valid JSON
|
59
|
+
And '{ "age": 24 }' is not valid JSON
|
data/jschematic.gemspec
CHANGED
@@ -1,19 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/jschematic/version', __FILE__)
|
3
|
+
|
1
4
|
Gem::Specification.new do |s|
|
2
5
|
s.name = 'jschematic'
|
3
|
-
s.version =
|
4
|
-
s.authors = ["Mike Sassak"]
|
6
|
+
s.version = Jschematic::VERSION
|
7
|
+
s.authors = ["Mike Sassak", "Jari Bakken"]
|
8
|
+
s.email = ["msassak@gmail.com", "jari.bakken@gmail.com"]
|
5
9
|
s.description = "JSON Schema v3 Validator"
|
6
10
|
s.summary = "jschematic #{s.version}"
|
7
|
-
s.
|
8
|
-
s.homepage = "https://github.com/msassak/jschematic"
|
11
|
+
s.homepage = "https://github.com/jarib/jschematic"
|
9
12
|
|
10
13
|
s.add_dependency 'addressable'
|
11
14
|
|
12
15
|
s.add_development_dependency 'cucumber'
|
13
16
|
s.add_development_dependency 'rspec'
|
14
17
|
s.add_development_dependency 'yajl-ruby'
|
18
|
+
s.add_development_dependency 'rake'
|
15
19
|
|
16
|
-
s.rubygems_version = "1.3.7"
|
17
20
|
s.files = `git ls-files`.split("\n")
|
18
21
|
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
19
22
|
s.extra_rdoc_files = ["LICENSE", "README.md"]
|
@@ -15,13 +15,15 @@ module Jschematic
|
|
15
15
|
case @schema
|
16
16
|
when String
|
17
17
|
uri = Addressable::URI.parse(@schema)
|
18
|
-
schema =
|
18
|
+
schema = schema_for(uri)
|
19
19
|
schema.accepts?(instance)
|
20
20
|
when Hash
|
21
21
|
Schema.new(@schema).accepts?(instance)
|
22
22
|
when Array
|
23
23
|
@schema.all? do |schema|
|
24
|
-
Extends.new(schema)
|
24
|
+
extends = Extends.new(schema)
|
25
|
+
extends.parent = self
|
26
|
+
extends.accepts?(instance)
|
25
27
|
end
|
26
28
|
end || fail_validation!(@schema, instance)
|
27
29
|
end
|
@@ -22,11 +22,11 @@ module Jschematic
|
|
22
22
|
value = nil
|
23
23
|
end
|
24
24
|
|
25
|
-
if
|
25
|
+
if value.nil? and child.default
|
26
26
|
value = child.default
|
27
27
|
end
|
28
28
|
|
29
|
-
if
|
29
|
+
if value.nil? and child.required?
|
30
30
|
fail_validation!("#{child.name} is required", nil)
|
31
31
|
elsif !value
|
32
32
|
true
|
data/lib/jschematic/context.rb
CHANGED
@@ -2,6 +2,29 @@ require 'jschematic/schema'
|
|
2
2
|
|
3
3
|
module Jschematic
|
4
4
|
class Context
|
5
|
+
def self.schema_for(ref)
|
6
|
+
if ref.relative?
|
7
|
+
rel = ref.omit(:scheme, :host, :port)
|
8
|
+
if match = schemas.find{ |ref, _| rel == ref.omit(:scheme, :host, :port) }
|
9
|
+
match[-1]
|
10
|
+
end
|
11
|
+
else
|
12
|
+
schemas[ref]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.add_schema(id, schema)
|
17
|
+
schemas[id] = schema
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.schemas
|
21
|
+
@schemas ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.empty
|
25
|
+
new
|
26
|
+
end
|
27
|
+
|
5
28
|
def initialize(*raw_schemas)
|
6
29
|
@schemas = raw_schemas.collect{ |raw_schema| Schema.new(raw_schema) }
|
7
30
|
end
|
@@ -9,5 +32,13 @@ module Jschematic
|
|
9
32
|
def validate!(instance, raw_schema)
|
10
33
|
Schema.new(raw_schema).accepts?(instance)
|
11
34
|
end
|
35
|
+
|
36
|
+
def add_schema(id, schema)
|
37
|
+
self.class.add_schema(id, schema)
|
38
|
+
end
|
39
|
+
|
40
|
+
def schema_for(ref)
|
41
|
+
self.class.schema_for(ref)
|
42
|
+
end
|
12
43
|
end
|
13
44
|
end
|
data/lib/jschematic/element.rb
CHANGED
@@ -14,7 +14,7 @@ module Jschematic
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def id
|
17
|
-
if @parent
|
17
|
+
if @parent.respond_to?(:id)
|
18
18
|
@parent.id + @id
|
19
19
|
else
|
20
20
|
@id
|
@@ -25,6 +25,10 @@ module Jschematic
|
|
25
25
|
self.class.to_s
|
26
26
|
end
|
27
27
|
|
28
|
+
def schema_for(ref)
|
29
|
+
parent && parent.schema_for(ref)
|
30
|
+
end
|
31
|
+
|
28
32
|
private
|
29
33
|
|
30
34
|
def fail_validation!(expected, actual)
|
data/lib/jschematic/schema.rb
CHANGED
@@ -15,32 +15,13 @@ module Jschematic
|
|
15
15
|
schema
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.schema_for(ref)
|
19
|
-
if ref.relative?
|
20
|
-
rel = ref.omit(:scheme, :host, :port)
|
21
|
-
if match = schemas.find{ |ref, _| rel == ref.omit(:scheme, :host, :port) }
|
22
|
-
match[-1]
|
23
|
-
end
|
24
|
-
else
|
25
|
-
schemas[ref]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.add_schema(id, schema)
|
30
|
-
schemas[id] = schema
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.schemas
|
34
|
-
@schemas ||= {}
|
35
|
-
end
|
36
|
-
|
37
18
|
attr_reader :default, :title, :description, :schema, :unknown_attributes
|
38
19
|
|
39
20
|
attr_accessor :name
|
40
|
-
attr_writer :parent
|
41
21
|
|
42
|
-
def initialize(raw_schema)
|
22
|
+
def initialize(raw_schema, parent = Context.empty)
|
43
23
|
@raw_schema = raw_schema.dup || {}
|
24
|
+
@parent = parent
|
44
25
|
|
45
26
|
@default = @raw_schema.delete("default")
|
46
27
|
@title = @raw_schema.delete("title") || ""
|
@@ -50,7 +31,7 @@ module Jschematic
|
|
50
31
|
|
51
32
|
@unknown_attributes = {}
|
52
33
|
|
53
|
-
|
34
|
+
@parent.add_schema(@id, self) unless @id.to_s.empty?
|
54
35
|
|
55
36
|
@raw_schema.each_pair do |attribute, value|
|
56
37
|
begin
|
@@ -71,6 +52,10 @@ module Jschematic
|
|
71
52
|
children.any?{ |child| child.required? }
|
72
53
|
end
|
73
54
|
|
55
|
+
def schema_for(ref)
|
56
|
+
parent.schema_for(ref)
|
57
|
+
end
|
58
|
+
|
74
59
|
private
|
75
60
|
|
76
61
|
def add_default(instance)
|
@@ -4,8 +4,7 @@ module Jschematic
|
|
4
4
|
module Attributes
|
5
5
|
describe Ref do
|
6
6
|
describe "#accepts?" do
|
7
|
-
it "fails when the
|
8
|
-
Schema.schemas.should be_empty
|
7
|
+
it "fails when the referenced schema does not exist" do
|
9
8
|
ref = Ref.new("does_not_exist")
|
10
9
|
expect { ref.accepts?({}) }.to raise_error(Jschematic::SchemaNotFoundError)
|
11
10
|
end
|
metadata
CHANGED
@@ -1,88 +1,111 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jschematic
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 9
|
9
|
-
version: 0.0.9
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Mike Sassak
|
9
|
+
- Jari Bakken
|
13
10
|
autorequire:
|
14
11
|
bindir: bin
|
15
12
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
21
16
|
name: addressable
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
24
18
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
- 0
|
30
|
-
version: "0"
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
31
23
|
type: :runtime
|
32
|
-
version_requirements: *id001
|
33
|
-
- !ruby/object:Gem::Dependency
|
34
|
-
name: cucumber
|
35
24
|
prerelease: false
|
36
|
-
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: cucumber
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
37
34
|
none: false
|
38
|
-
requirements:
|
39
|
-
- -
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
|
42
|
-
- 0
|
43
|
-
version: "0"
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
44
39
|
type: :development
|
45
|
-
version_requirements: *id002
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: rspec
|
48
40
|
prerelease: false
|
49
|
-
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
57
55
|
type: :development
|
58
|
-
|
59
|
-
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
60
64
|
name: yajl-ruby
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
61
72
|
prerelease: false
|
62
|
-
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
74
|
none: false
|
64
|
-
requirements:
|
65
|
-
- -
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
|
68
|
-
|
69
|
-
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: rake
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
70
87
|
type: :development
|
71
|
-
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
72
95
|
description: JSON Schema v3 Validator
|
73
|
-
email:
|
96
|
+
email:
|
97
|
+
- msassak@gmail.com
|
98
|
+
- jari.bakken@gmail.com
|
74
99
|
executables: []
|
75
|
-
|
76
100
|
extensions: []
|
77
|
-
|
78
|
-
extra_rdoc_files:
|
101
|
+
extra_rdoc_files:
|
79
102
|
- LICENSE
|
80
103
|
- README.md
|
81
|
-
files:
|
104
|
+
files:
|
105
|
+
- .gitignore
|
82
106
|
- .rspec
|
83
|
-
- .
|
107
|
+
- .travis.yml
|
84
108
|
- Gemfile
|
85
|
-
- Gemfile.lock
|
86
109
|
- History.txt
|
87
110
|
- LICENSE
|
88
111
|
- README.md
|
@@ -140,6 +163,7 @@ files:
|
|
140
163
|
- lib/jschematic/errors.rb
|
141
164
|
- lib/jschematic/exceptions.rb
|
142
165
|
- lib/jschematic/schema.rb
|
166
|
+
- lib/jschematic/version.rb
|
143
167
|
- spec/jschematic/attributes/disallow_spec.rb
|
144
168
|
- spec/jschematic/attributes/divisible_by_spec.rb
|
145
169
|
- spec/jschematic/attributes/enum_spec.rb
|
@@ -154,39 +178,32 @@ files:
|
|
154
178
|
- spec/jschematic/errors_spec.rb
|
155
179
|
- spec/jschematic/schema_spec.rb
|
156
180
|
- spec/spec_helper.rb
|
157
|
-
|
158
|
-
homepage: https://github.com/msassak/jschematic
|
181
|
+
homepage: https://github.com/jarib/jschematic
|
159
182
|
licenses: []
|
160
|
-
|
161
183
|
post_install_message:
|
162
|
-
rdoc_options:
|
184
|
+
rdoc_options:
|
163
185
|
- --charset=UTF-8
|
164
|
-
require_paths:
|
186
|
+
require_paths:
|
165
187
|
- lib
|
166
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
189
|
none: false
|
168
|
-
requirements:
|
169
|
-
- -
|
170
|
-
- !ruby/object:Gem::Version
|
171
|
-
|
172
|
-
|
173
|
-
version: "0"
|
174
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ! '>='
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
195
|
none: false
|
176
|
-
requirements:
|
177
|
-
- -
|
178
|
-
- !ruby/object:Gem::Version
|
179
|
-
|
180
|
-
- 0
|
181
|
-
version: "0"
|
196
|
+
requirements:
|
197
|
+
- - ! '>='
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: '0'
|
182
200
|
requirements: []
|
183
|
-
|
184
201
|
rubyforge_project:
|
185
|
-
rubygems_version: 1.
|
202
|
+
rubygems_version: 1.8.24
|
186
203
|
signing_key:
|
187
204
|
specification_version: 3
|
188
|
-
summary: jschematic 0.0
|
189
|
-
test_files:
|
205
|
+
summary: jschematic 0.1.0
|
206
|
+
test_files:
|
190
207
|
- features/default.feature
|
191
208
|
- features/dependencies.feature
|
192
209
|
- features/disallow.feature
|
@@ -220,3 +237,4 @@ test_files:
|
|
220
237
|
- spec/jschematic/errors_spec.rb
|
221
238
|
- spec/jschematic/schema_spec.rb
|
222
239
|
- spec/spec_helper.rb
|
240
|
+
has_rdoc:
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm --create use 1.9.2@jschematic
|
data/Gemfile.lock
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
jschematic (0.0.6)
|
5
|
-
addressable
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: http://rubygems.org/
|
9
|
-
specs:
|
10
|
-
addressable (2.2.4)
|
11
|
-
builder (3.0.0)
|
12
|
-
cucumber (0.10.0)
|
13
|
-
builder (>= 2.1.2)
|
14
|
-
diff-lcs (~> 1.1.2)
|
15
|
-
gherkin (~> 2.3.2)
|
16
|
-
json (~> 1.4.6)
|
17
|
-
term-ansicolor (~> 1.0.5)
|
18
|
-
diff-lcs (1.1.2)
|
19
|
-
gherkin (2.3.3)
|
20
|
-
json (~> 1.4.6)
|
21
|
-
json (1.4.6)
|
22
|
-
rspec (2.3.0)
|
23
|
-
rspec-core (~> 2.3.0)
|
24
|
-
rspec-expectations (~> 2.3.0)
|
25
|
-
rspec-mocks (~> 2.3.0)
|
26
|
-
rspec-core (2.3.1)
|
27
|
-
rspec-expectations (2.3.0)
|
28
|
-
diff-lcs (~> 1.1.2)
|
29
|
-
rspec-mocks (2.3.0)
|
30
|
-
term-ansicolor (1.0.5)
|
31
|
-
yajl-ruby (0.7.8)
|
32
|
-
|
33
|
-
PLATFORMS
|
34
|
-
ruby
|
35
|
-
|
36
|
-
DEPENDENCIES
|
37
|
-
cucumber
|
38
|
-
jschematic!
|
39
|
-
rspec
|
40
|
-
yajl-ruby
|