jschematic 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jschematic (0.0.2)
4
+ jschematic (0.0.5)
5
5
  addressable
6
6
 
7
7
  GEM
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.0.6
2
+ * Improve and document the Context API (Mike Sassak)
3
+
1
4
  == 0.0.5
2
5
  * Initial support for validation contexts (Mike Sassak)
3
6
  * $ref support (Mike Sassak)
data/README.md CHANGED
@@ -31,7 +31,12 @@ fine yajl-ruby gem for testing).
31
31
 
32
32
  ## Advanced Usage
33
33
 
34
- Hasn't been documented yet.
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
- And this schema:
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
- add_schema(parse(schema))
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
- add_schema(parse(schema))
6
+ set_schema(schema)
7
7
  end
8
8
 
9
9
  When "the schema is:" do |schema|
10
- add_schema(parse(schema))
10
+ set_schema(schema)
11
11
  end
12
12
 
13
13
  Then /^'(.+)' is valid JSON$/ do |json|
14
- assert_valid(parse(json), all_schemas)
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), all_schemas)
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), all_schemas)
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), all_schemas)
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]), all_schemas)
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]), all_schemas)
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, raw_schemas)
54
- Jschematic.validate(json, *raw_schemas).should be_true
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, raw_schemas)
58
- Jschematic.validate(json, *raw_schemas).should be_false
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
- @_schema ||= Jschematic::Schema.new(all_schemas.first)
62
+ Jschematic::Schema.new(current_schema)
63
63
  end
64
64
 
65
- def add_schema(schema)
66
- @_schemas ||= []
67
- @_schemas << schema
65
+ def set_schema(schema)
66
+ @_schema = parse(schema)
68
67
  end
69
68
 
70
- def all_schemas
71
- @_schemas ||= []
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jschematic'
3
- s.version = '0.0.5'
3
+ s.version = '0.0.6'
4
4
  s.authors = ["Mike Sassak"]
5
5
  s.description = "JSON Schema v3 Validator"
6
6
  s.summary = "jschematic #{s.version}"
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, *schemas)
5
- validate!(instance, *schemas)
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, *schemas)
11
- Context.new(*schemas).validate!(instance)
10
+ def self.validate!(instance, schema, opts={})
11
+ Context.new(*opts[:context]).validate!(instance, schema)
12
12
  end
13
13
  end
@@ -2,12 +2,12 @@ require 'jschematic/schema'
2
2
 
3
3
  module Jschematic
4
4
  class Context
5
- def initialize(*schemas)
6
- @schemas = schemas.collect{ |raw_schema| Schema.new(raw_schema) }
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
- @schemas.last.accepts?(instance)
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
- - 5
9
- version: 0.0.5
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.5
180
+ summary: jschematic 0.0.6
181
181
  test_files:
182
182
  - features/default.feature
183
183
  - features/dependencies.feature