json_schema 0.17.2 → 0.18.0
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.
- checksums.yaml +4 -4
- data/lib/json_schema/attributes.rb +9 -1
- data/lib/json_schema/schema.rb +19 -1
- data/lib/json_schema/validator.rb +1 -12
- data/test/json_reference/reference_test.rb +4 -4
- data/test/json_schema/attribute_test.rb +19 -4
- data/test/json_schema/schema_test.rb +9 -0
- data/test/json_schema_test.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 2d133299adf9a9c71dad5525df64e25d4846e35e
         | 
| 4 | 
            +
              data.tar.gz: 4d9aed1582b80eefcfc12656dddaf65b77fd894a
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6f156d37e4067bae3bbd2e7a471f921d362afea8e2900c88c13278b383efd52a5f7fefd3129418c2dd406a0ec5a103b748aa7183238d7445e67dbbe969f63b4b
         | 
| 7 | 
            +
              data.tar.gz: 9e118b7e172ed7c52b8b9cddd26a29e6a8e872e0f8782f6a57f34bdd323165b3996b4d405e8639441fb4a5f93fbb155f24b5fd59ecf98cfead00e4a49dee0ad6
         | 
| @@ -43,10 +43,18 @@ module JsonSchema | |
| 43 43 | 
             
                        end
         | 
| 44 44 | 
             
                      end
         | 
| 45 45 | 
             
                    end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                    if options[:clear_cache]
         | 
| 48 | 
            +
                      remove_method(:"#{attr}=")
         | 
| 49 | 
            +
                      define_method(:"#{attr}=") do |value|
         | 
| 50 | 
            +
                        instance_variable_set(options[:clear_cache], nil)
         | 
| 51 | 
            +
                        instance_variable_set(ref, value)
         | 
| 52 | 
            +
                      end
         | 
| 53 | 
            +
                    end
         | 
| 46 54 | 
             
                  end
         | 
| 47 55 |  | 
| 48 56 | 
             
                  def attr_schema(attr, options = {})
         | 
| 49 | 
            -
                    attr_copyable(attr, :default => options[:default])
         | 
| 57 | 
            +
                    attr_copyable(attr, :default => options[:default], :clear_cache => options[:clear_cache])
         | 
| 50 58 | 
             
                    self.schema_attrs[options[:schema_name] || attr] = attr
         | 
| 51 59 | 
             
                  end
         | 
| 52 60 |  | 
    
        data/lib/json_schema/schema.rb
    CHANGED
    
    | @@ -2,6 +2,16 @@ require "json" | |
| 2 2 |  | 
| 3 3 | 
             
            module JsonSchema
         | 
| 4 4 | 
             
              class Schema
         | 
| 5 | 
            +
                TYPE_MAP = {
         | 
| 6 | 
            +
                  "array"   => Array,
         | 
| 7 | 
            +
                  "boolean" => [FalseClass, TrueClass],
         | 
| 8 | 
            +
                  "integer" => Integer,
         | 
| 9 | 
            +
                  "number"  => [Integer, Float],
         | 
| 10 | 
            +
                  "null"    => NilClass,
         | 
| 11 | 
            +
                  "object"  => Hash,
         | 
| 12 | 
            +
                  "string"  => String,
         | 
| 13 | 
            +
                }
         | 
| 14 | 
            +
             | 
| 5 15 | 
             
                include Attributes
         | 
| 6 16 |  | 
| 7 17 | 
             
                def initialize
         | 
| @@ -129,7 +139,7 @@ module JsonSchema | |
| 129 139 | 
             
                # of strings.
         | 
| 130 140 | 
             
                #
         | 
| 131 141 | 
             
                # Type: Array[String]
         | 
| 132 | 
            -
                attr_schema :type, :default => []
         | 
| 142 | 
            +
                attr_schema :type, :default => [], :clear_cache => :@type_parsed
         | 
| 133 143 |  | 
| 134 144 | 
             
                # validation: array
         | 
| 135 145 | 
             
                attr_schema :additional_items, :default => true, :schema_name => :additionalItems
         | 
| @@ -199,6 +209,14 @@ module JsonSchema | |
| 199 209 | 
             
                  true
         | 
| 200 210 | 
             
                end
         | 
| 201 211 |  | 
| 212 | 
            +
                # An array of Ruby classes that are equivalent to the types defined in the
         | 
| 213 | 
            +
                # schema.
         | 
| 214 | 
            +
                #
         | 
| 215 | 
            +
                # Type: Array[Class]
         | 
| 216 | 
            +
                def type_parsed
         | 
| 217 | 
            +
                  @type_parsed ||= type.flat_map { |t| TYPE_MAP[t] }.compact
         | 
| 218 | 
            +
                end
         | 
| 219 | 
            +
             | 
| 202 220 | 
             
                def inspect
         | 
| 203 221 | 
             
                  "\#<JsonSchema::Schema pointer=#{pointer}>"
         | 
| 204 222 | 
             
                end
         | 
| @@ -2,16 +2,6 @@ require "uri" | |
| 2 2 |  | 
| 3 3 | 
             
            module JsonSchema
         | 
| 4 4 | 
             
              class Validator
         | 
| 5 | 
            -
                TYPE_MAP = {
         | 
| 6 | 
            -
                  "array"   => Array,
         | 
| 7 | 
            -
                  "boolean" => [FalseClass, TrueClass],
         | 
| 8 | 
            -
                  "integer" => Integer,
         | 
| 9 | 
            -
                  "number"  => [Integer, Float],
         | 
| 10 | 
            -
                  "null"    => NilClass,
         | 
| 11 | 
            -
                  "object"  => Hash,
         | 
| 12 | 
            -
                  "string"  => String,
         | 
| 13 | 
            -
                }
         | 
| 14 | 
            -
             | 
| 15 5 | 
             
                attr_accessor :errors
         | 
| 16 6 |  | 
| 17 7 | 
             
                def initialize(schema)
         | 
| @@ -512,8 +502,7 @@ module JsonSchema | |
| 512 502 |  | 
| 513 503 | 
             
                def validate_type(schema, data, errors, path)
         | 
| 514 504 | 
             
                  return true if !schema.type || schema.type.empty?
         | 
| 515 | 
            -
                   | 
| 516 | 
            -
                  if valid_types.any? { |t| data.is_a?(t) }
         | 
| 505 | 
            +
                  if schema.type_parsed.include?(data.class)
         | 
| 517 506 | 
             
                    true
         | 
| 518 507 | 
             
                  else
         | 
| 519 508 | 
             
                    key = find_parent(schema)
         | 
| @@ -5,7 +5,7 @@ require "json_reference" | |
| 5 5 | 
             
            describe JsonReference::Reference do
         | 
| 6 6 | 
             
              it "expands a reference without a URI" do
         | 
| 7 7 | 
             
                ref = reference("#/definitions")
         | 
| 8 | 
            -
                 | 
| 8 | 
            +
                assert_nil ref.uri
         | 
| 9 9 | 
             
                assert_equal "#/definitions", ref.pointer
         | 
| 10 10 | 
             
              end
         | 
| 11 11 |  | 
| @@ -17,7 +17,7 @@ describe JsonReference::Reference do | |
| 17 17 |  | 
| 18 18 | 
             
              it "expands just a root sign" do
         | 
| 19 19 | 
             
                ref = reference("#")
         | 
| 20 | 
            -
                 | 
| 20 | 
            +
                assert_nil ref.uri
         | 
| 21 21 | 
             
                assert_equal "#", ref.pointer
         | 
| 22 22 | 
             
              end
         | 
| 23 23 |  | 
| @@ -29,13 +29,13 @@ describe JsonReference::Reference do | |
| 29 29 |  | 
| 30 30 | 
             
              it "normalizes pointers by adding a root sign prefix" do
         | 
| 31 31 | 
             
                ref = reference("/definitions")
         | 
| 32 | 
            -
                 | 
| 32 | 
            +
                assert_nil ref.uri
         | 
| 33 33 | 
             
                assert_equal "#/definitions", ref.pointer
         | 
| 34 34 | 
             
              end
         | 
| 35 35 |  | 
| 36 36 | 
             
              it "normalizes pointers by stripping a trailing slash" do
         | 
| 37 37 | 
             
                ref = reference("#/definitions/")
         | 
| 38 | 
            -
                 | 
| 38 | 
            +
                assert_nil ref.uri
         | 
| 39 39 | 
             
                assert_equal "#/definitions", ref.pointer
         | 
| 40 40 | 
             
              end
         | 
| 41 41 |  | 
| @@ -14,7 +14,7 @@ describe JsonSchema::Attributes do | |
| 14 14 | 
             
                obj = TestAttributes.new
         | 
| 15 15 | 
             
                obj.schema = "foo"
         | 
| 16 16 | 
             
                assert_equal "foo", obj.schema
         | 
| 17 | 
            -
                assert_equal({:schema => :schema, :named => :schema_named},
         | 
| 17 | 
            +
                assert_equal({:schema => :schema, :named => :schema_named, :cached => :cached},
         | 
| 18 18 | 
             
                  obj.class.schema_attrs)
         | 
| 19 19 | 
             
              end
         | 
| 20 20 |  | 
| @@ -48,7 +48,7 @@ describe JsonSchema::Attributes do | |
| 48 48 | 
             
                obj.schema = "schema"
         | 
| 49 49 |  | 
| 50 50 | 
             
                assert_raises NoMethodError do
         | 
| 51 | 
            -
                   | 
| 51 | 
            +
                  assert_nil obj[:copyable]
         | 
| 52 52 | 
             
                end
         | 
| 53 53 |  | 
| 54 54 | 
             
                assert_equal "schema", obj[:schema]
         | 
| @@ -71,8 +71,18 @@ describe JsonSchema::Attributes do | |
| 71 71 | 
             
                obj = TestAttributes.new
         | 
| 72 72 |  | 
| 73 73 | 
             
                # should produce a nil value *without* a Ruby warning
         | 
| 74 | 
            -
                 | 
| 75 | 
            -
                 | 
| 74 | 
            +
                assert_nil obj.copyable
         | 
| 75 | 
            +
                assert_nil obj.schema
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              it "cleans cached values when assigning parent attribute" do
         | 
| 79 | 
            +
                obj = TestAttributes.new
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                obj.cached = "test"
         | 
| 82 | 
            +
                assert_equal "test_123", obj.cached_parsed
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                obj.cached = "other"
         | 
| 85 | 
            +
                assert_equal "other_123", obj.cached_parsed
         | 
| 76 86 | 
             
              end
         | 
| 77 87 |  | 
| 78 88 | 
             
              class TestAttributes
         | 
| @@ -87,6 +97,11 @@ describe JsonSchema::Attributes do | |
| 87 97 | 
             
                attr_schema :schema
         | 
| 88 98 | 
             
                attr_schema :schema_named, :schema_name => :named
         | 
| 89 99 |  | 
| 100 | 
            +
                attr_schema :cached, :clear_cache => :@cached_parsed
         | 
| 101 | 
            +
                def cached_parsed
         | 
| 102 | 
            +
                  @cached_parsed ||= "#{cached}_123"
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 90 105 | 
             
                attr_copyable :copyable_default, :default => []
         | 
| 91 106 | 
             
                attr_copyable :copyable_default_with_string, :default => "application/json"
         | 
| 92 107 | 
             
                attr_copyable :copyable_default_with_object, :default => {}
         | 
| @@ -34,4 +34,13 @@ describe JsonSchema::Schema do | |
| 34 34 | 
             
                  schema[:expanded]
         | 
| 35 35 | 
             
                end
         | 
| 36 36 | 
             
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              it "updates type_parsed when type is changed" do
         | 
| 39 | 
            +
                schema = JsonSchema::Schema.new
         | 
| 40 | 
            +
                schema.type = ["integer"]
         | 
| 41 | 
            +
                assert_equal [Integer], schema.type_parsed
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                schema.type = ["string"]
         | 
| 44 | 
            +
                assert_equal [String], schema.type_parsed
         | 
| 45 | 
            +
              end
         | 
| 37 46 | 
             
            end
         | 
    
        data/test/json_schema_test.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: json_schema
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.18.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brandur
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2018- | 
| 11 | 
            +
            date: 2018-03-26 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: ecma-re-validator
         |