json-schema 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,6 @@ h1. Ruby JSON Schema Validator
2
2
 
3
3
  This library is intended to provide Ruby with an interface for validating JSON objects against a JSON schema conforming to "JSON Schema Draft 3":http://tools.ietf.org/html/draft-zyp-json-schema-03. The project originally started as a fork of "Constellation's ruby-jsonschema":https://github.com/Constellation/ruby-jsonchema project, but differences in the JSON schema draft versions implemented as well as assumptions made by the ruby-jsonschema library forced this to become a new project.
4
4
 
5
- This project is far from complete but is in a somewhat usable state...
6
-
7
5
  h2. Usage
8
6
 
9
7
  Install:
@@ -16,7 +14,7 @@ If downloading the git repo, build the gem and install it:
16
14
 
17
15
  <pre>
18
16
  $ rake package
19
- $ gem install pkg/json-schema-0.1.0.gem
17
+ $ gem install pkg/json-schema-0.1.6.gem
20
18
  </pre>
21
19
 
22
20
  <pre>
@@ -85,20 +83,20 @@ h2. Not implemented
85
83
 
86
84
  The following core schema definitions are not implemented:
87
85
 
86
+ * format - Some of the formats listed will be supported, others are fairly vague in their definition and may be left out
88
87
  * default - This library aims to solely be a validator and does not modify an object it is validating.
89
88
  * $schema - Support for this will come later, possibly with the ability to validate against other JSON Schema draft versions
90
89
 
91
90
  In addition, the following hyper schema attributes are not implemented at this time:
92
91
 
93
- * links (not much to do with validation...)
92
+ * links
94
93
  * fragmentResolution (only handles default slash-delimited)
95
- * readonly
96
94
  * contentEncoding
97
95
  * pathStart
98
- * mediaType
99
96
 
100
97
 
101
98
  h2. To Do
102
99
 
103
100
  * (Much) More testing
104
- * Adding a persistence option to schemas that enables the validator to only load schemas once across multiple validations
101
+ * Documentation
102
+ * Breaking the current validator out into a subclass, acting as the foundation for supporting multiple versions of validators
@@ -2,6 +2,7 @@ require 'uri'
2
2
  require 'open-uri'
3
3
  require 'pathname'
4
4
  require 'bigdecimal'
5
+ require 'digest/sha1'
5
6
 
6
7
  module JSON
7
8
 
@@ -46,9 +47,7 @@ module JSON
46
47
 
47
48
  def initialize(schema_data, data)
48
49
  @base_schema = initialize_schema(schema_data)
49
- @data = initialize_data(data)
50
- Validator.add_schema(@base_schema)
51
-
50
+ @data = initialize_data(data)
52
51
  build_schemas(@base_schema)
53
52
  end
54
53
 
@@ -613,10 +612,11 @@ module JSON
613
612
  private
614
613
 
615
614
  def initialize_schema(schema)
616
- schema_uri = URI.parse("file://#{Dir.pwd}/__base_schema__.json")
617
615
  if schema.is_a?(String)
618
616
  begin
619
- schema = JSON.parse(schema)
617
+ # Build a fake URI for this
618
+ schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}.json")
619
+ schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
620
620
  rescue
621
621
  # Build a uri for it
622
622
  schema_uri = URI.parse(schema)
@@ -628,11 +628,22 @@ module JSON
628
628
  schema_uri = URI.parse("file://#{Dir.pwd}/#{schema}")
629
629
  end
630
630
  end
631
- schema = JSON.parse(open(schema_uri.to_s).read)
631
+ if Validator.schemas[schema_uri.to_s].nil?
632
+ schema = JSON::Schema.new(JSON.parse(open(schema_uri.to_s).read),schema_uri)
633
+ Validator.add_schema(schema)
634
+ else
635
+ schema = Validator.schemas[schema_uri.to_s]
636
+ end
632
637
  end
638
+ elsif schema.is_a?(Hash)
639
+ schema = schema.to_json
640
+ schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}.json")
641
+ schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
642
+ else
643
+ raise "Invalid schema - must be either a string or a hash"
633
644
  end
634
645
 
635
- JSON::Schema.new(schema,schema_uri)
646
+ schema
636
647
  end
637
648
 
638
649
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kenny Hoxworth
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-02 00:00:00 -05:00
18
+ date: 2010-12-03 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency