jschematic 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/History.txt +3 -0
- data/README.md +6 -1
- data/features/ref.feature +2 -2
- data/features/step_definitions/jschematic_steps.rb +27 -20
- data/jschematic.gemspec +1 -1
- data/lib/jschematic.rb +4 -4
- data/lib/jschematic/context.rb +4 -4
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -31,7 +31,12 @@ fine yajl-ruby gem for testing).
|
|
31
31
|
|
32
32
|
## Advanced Usage
|
33
33
|
|
34
|
-
|
34
|
+
Pass extra schemas to inform the validation context:
|
35
|
+
|
36
|
+
Jschematic.validate(json, schema, :context => [cs1, cs2])
|
37
|
+
|
38
|
+
Each context schema will be consulting in those cases where cross-schema
|
39
|
+
referencing is allowed, e.g. $ref.
|
35
40
|
|
36
41
|
## Testing
|
37
42
|
|
data/features/ref.feature
CHANGED
@@ -90,7 +90,7 @@ Feature: Core schema: $ref
|
|
90
90
|
"""
|
91
91
|
|
92
92
|
Scenario: Ref resolves to different schema
|
93
|
-
Given this schema:
|
93
|
+
Given the validation context contains this schema:
|
94
94
|
"""
|
95
95
|
{
|
96
96
|
"id": "http://www.example.com/schemas/person",
|
@@ -101,7 +101,7 @@ Feature: Core schema: $ref
|
|
101
101
|
}
|
102
102
|
}
|
103
103
|
"""
|
104
|
-
|
104
|
+
When the schema is:
|
105
105
|
"""
|
106
106
|
{
|
107
107
|
"title": "A Happy Family Much Like All Other Happy Families",
|
@@ -1,40 +1,40 @@
|
|
1
|
-
Given "this schema:" do |schema|
|
2
|
-
|
1
|
+
Given "the validation context contains this schema:" do |schema|
|
2
|
+
add_context_schema(schema)
|
3
3
|
end
|
4
4
|
|
5
5
|
When /^the schema is '(.+)'$/ do |schema|
|
6
|
-
|
6
|
+
set_schema(schema)
|
7
7
|
end
|
8
8
|
|
9
9
|
When "the schema is:" do |schema|
|
10
|
-
|
10
|
+
set_schema(schema)
|
11
11
|
end
|
12
12
|
|
13
13
|
Then /^'(.+)' is valid JSON$/ do |json|
|
14
|
-
assert_valid(parse(json),
|
14
|
+
assert_valid(parse(json), current_schema, context_schemas)
|
15
15
|
end
|
16
16
|
|
17
17
|
Then /^'(.+)' is not valid JSON$/ do |json|
|
18
|
-
assert_invalid(parse(json),
|
18
|
+
assert_invalid(parse(json), current_schema, context_schemas)
|
19
19
|
end
|
20
20
|
|
21
21
|
Then "this is valid JSON:" do |json|
|
22
|
-
assert_valid(parse(json),
|
22
|
+
assert_valid(parse(json), current_schema, context_schemas)
|
23
23
|
end
|
24
24
|
|
25
25
|
Then "this is not valid JSON:" do |json|
|
26
|
-
assert_invalid(parse(json),
|
26
|
+
assert_invalid(parse(json), current_schema, context_schemas)
|
27
27
|
end
|
28
28
|
|
29
29
|
Then "these are valid JSON:" do |instances|
|
30
30
|
instances.raw.each do |row|
|
31
|
-
assert_valid(parse(row[0]),
|
31
|
+
assert_valid(parse(row[0]), current_schema, context_schemas)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
Then "these are not valid JSON:" do |instances|
|
36
36
|
instances.raw.each do |row|
|
37
|
-
assert_invalid(parse(row[0]),
|
37
|
+
assert_invalid(parse(row[0]), current_schema, context_schemas)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -50,25 +50,32 @@ module JschematicWorld
|
|
50
50
|
raise "Parsing '#{json}' failed with #{e.to_s}"
|
51
51
|
end
|
52
52
|
|
53
|
-
def assert_valid(json,
|
54
|
-
Jschematic.validate(json,
|
53
|
+
def assert_valid(json, current_schema, context_schemas)
|
54
|
+
Jschematic.validate(json, current_schema, :context => context_schemas).should be_true
|
55
55
|
end
|
56
56
|
|
57
|
-
def assert_invalid(json,
|
58
|
-
Jschematic.validate(json,
|
57
|
+
def assert_invalid(json, current_schema, context_schemas)
|
58
|
+
Jschematic.validate(json, current_schema, :context => context_schemas).should be_false
|
59
59
|
end
|
60
60
|
|
61
61
|
def build_schema
|
62
|
-
|
62
|
+
Jschematic::Schema.new(current_schema)
|
63
63
|
end
|
64
64
|
|
65
|
-
def
|
66
|
-
@
|
67
|
-
@_schemas << schema
|
65
|
+
def set_schema(schema)
|
66
|
+
@_schema = parse(schema)
|
68
67
|
end
|
69
68
|
|
70
|
-
def
|
71
|
-
@
|
69
|
+
def current_schema
|
70
|
+
@_schema ||= nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def add_context_schema(schema)
|
74
|
+
context_schemas << parse(schema)
|
75
|
+
end
|
76
|
+
|
77
|
+
def context_schemas
|
78
|
+
@_context_schemas ||= []
|
72
79
|
end
|
73
80
|
end
|
74
81
|
|
data/jschematic.gemspec
CHANGED
data/lib/jschematic.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'jschematic/context'
|
2
2
|
|
3
3
|
module Jschematic
|
4
|
-
def self.validate(instance,
|
5
|
-
validate!(instance,
|
4
|
+
def self.validate(instance, schema, opts={})
|
5
|
+
validate!(instance, schema, opts)
|
6
6
|
rescue ValidationError
|
7
7
|
false
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.validate!(instance,
|
11
|
-
Context.new(*
|
10
|
+
def self.validate!(instance, schema, opts={})
|
11
|
+
Context.new(*opts[:context]).validate!(instance, schema)
|
12
12
|
end
|
13
13
|
end
|
data/lib/jschematic/context.rb
CHANGED
@@ -2,12 +2,12 @@ require 'jschematic/schema'
|
|
2
2
|
|
3
3
|
module Jschematic
|
4
4
|
class Context
|
5
|
-
def initialize(*
|
6
|
-
@schemas =
|
5
|
+
def initialize(*raw_schemas)
|
6
|
+
@schemas = raw_schemas.collect{ |raw_schema| Schema.new(raw_schema) }
|
7
7
|
end
|
8
8
|
|
9
|
-
def validate!(instance)
|
10
|
-
|
9
|
+
def validate!(instance, raw_schema)
|
10
|
+
Schema.new(raw_schema).accepts?(instance)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 6
|
9
|
+
version: 0.0.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Mike Sassak
|
@@ -177,7 +177,7 @@ rubyforge_project:
|
|
177
177
|
rubygems_version: 1.3.7
|
178
178
|
signing_key:
|
179
179
|
specification_version: 3
|
180
|
-
summary: jschematic 0.0.
|
180
|
+
summary: jschematic 0.0.6
|
181
181
|
test_files:
|
182
182
|
- features/default.feature
|
183
183
|
- features/dependencies.feature
|