json_schema 0.5.0 → 0.6.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.rb +9 -0
- data/lib/json_schema/configuration.rb +16 -0
- data/lib/json_schema/parser.rb +13 -2
- data/lib/json_schema/validator.rb +1 -0
- data/test/json_schema/parser_test.rb +22 -0
- metadata +17 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 9bfb85015a2b3334dfbcdcd269fd7031f74f3b42
         | 
| 4 | 
            +
              data.tar.gz: 1d2050d6970717a4a083470ed3250a87321bdf3b
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 95c592714aaab7510d4faa0fa94ee82253ade1cae378e5712b872a013ab66e9e525c42208376c13d9cef35a686859db5a748337b5d30841ec3c6a9dab8ddd88e
         | 
| 7 | 
            +
              data.tar.gz: 203d25e5c27568618d4c51494ed915cbc87dc8da2362caacb3d7a0a59e938e74510bc94df81587269e4043138eb2f4ed601371f5cb33012c3e52bdf474fcf44e
         | 
    
        data/lib/json_schema.rb
    CHANGED
    
    | @@ -1,3 +1,4 @@ | |
| 1 | 
            +
            require_relative "json_schema/configuration"
         | 
| 1 2 | 
             
            require_relative "json_schema/document_store"
         | 
| 2 3 | 
             
            require_relative "json_schema/parser"
         | 
| 3 4 | 
             
            require_relative "json_schema/reference_expander"
         | 
| @@ -6,6 +7,14 @@ require_relative "json_schema/schema_error" | |
| 6 7 | 
             
            require_relative "json_schema/validator"
         | 
| 7 8 |  | 
| 8 9 | 
             
            module JsonSchema
         | 
| 10 | 
            +
              def self.configure
         | 
| 11 | 
            +
                yield configuration
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def self.configuration
         | 
| 15 | 
            +
                @configuration ||= Configuration.new
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 9 18 | 
             
              def self.parse(data)
         | 
| 10 19 | 
             
                parser = Parser.new
         | 
| 11 20 | 
             
                if schema = parser.parse(data)
         | 
    
        data/lib/json_schema/parser.rb
    CHANGED
    
    | @@ -211,12 +211,23 @@ module JsonSchema | |
| 211 211 | 
             
                    # leave the original data reference intact
         | 
| 212 212 | 
             
                    properties = schema.pattern_properties.dup
         | 
| 213 213 | 
             
                    properties = properties.map do |k, s|
         | 
| 214 | 
            -
                      [ | 
| 214 | 
            +
                      [parse_regex(schema, k), parse_data(s, schema, "patternProperties/#{k}")]
         | 
| 215 215 | 
             
                    end
         | 
| 216 216 | 
             
                    schema.pattern_properties = Hash[*properties.flatten]
         | 
| 217 217 | 
             
                  end
         | 
| 218 218 | 
             
                end
         | 
| 219 219 |  | 
| 220 | 
            +
                def parse_regex(schema, regex)
         | 
| 221 | 
            +
                  case JsonSchema.configuration.validate_regex_with
         | 
| 222 | 
            +
                  when :'ecma-re-validator'
         | 
| 223 | 
            +
                    unless EcmaReValidator.valid?(regex)
         | 
| 224 | 
            +
                      message = %{#{regex.inspect} is not an ECMA-262 regular expression.}
         | 
| 225 | 
            +
                      @errors << SchemaError.new(schema, message, :regex_failed)
         | 
| 226 | 
            +
                    end
         | 
| 227 | 
            +
                  end
         | 
| 228 | 
            +
                  Regexp.new(regex)
         | 
| 229 | 
            +
                end
         | 
| 230 | 
            +
             | 
| 220 231 | 
             
                def parse_properties(schema)
         | 
| 221 232 | 
             
                  # leave the original data reference intact
         | 
| 222 233 | 
             
                  schema.properties = schema.properties.dup
         | 
| @@ -287,7 +298,7 @@ module JsonSchema | |
| 287 298 | 
             
                  schema.max_length = validate_type(schema, [Integer], "maxLength")
         | 
| 288 299 | 
             
                  schema.min_length = validate_type(schema, [Integer], "minLength")
         | 
| 289 300 | 
             
                  schema.pattern    = validate_type(schema, [String], "pattern")
         | 
| 290 | 
            -
                  schema.pattern    =  | 
| 301 | 
            +
                  schema.pattern    = parse_regex(schema, schema.pattern) if schema.pattern
         | 
| 291 302 | 
             
                  validate_format(schema, schema.format) if schema.format
         | 
| 292 303 |  | 
| 293 304 | 
             
                  # hyperschema
         | 
| @@ -3,6 +3,12 @@ require "test_helper" | |
| 3 3 | 
             
            require "json_schema"
         | 
| 4 4 |  | 
| 5 5 | 
             
            describe JsonSchema::Parser do
         | 
| 6 | 
            +
              after do
         | 
| 7 | 
            +
                JsonSchema.configure do |c|
         | 
| 8 | 
            +
                  c.validate_regex_with = nil
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 6 12 | 
             
              it "parses the basic attributes of a schema" do
         | 
| 7 13 | 
             
                schema = parse
         | 
| 8 14 | 
             
                assert_nil schema.id
         | 
| @@ -266,6 +272,22 @@ describe JsonSchema::Parser do | |
| 266 272 | 
             
                assert_includes error_types, :unknown_format
         | 
| 267 273 | 
             
              end
         | 
| 268 274 |  | 
| 275 | 
            +
              it "passes for an invalid regex when not asked to check" do
         | 
| 276 | 
            +
                schema_sample["pattern"] = "\\Ameow"
         | 
| 277 | 
            +
                assert parse
         | 
| 278 | 
            +
              end
         | 
| 279 | 
            +
             | 
| 280 | 
            +
              it "errors for an invalid regex when asked to check" do
         | 
| 281 | 
            +
                require 'ecma-re-validator'
         | 
| 282 | 
            +
                JsonSchema.configure do |c|
         | 
| 283 | 
            +
                  c.validate_regex_with = :'ecma-re-validator'
         | 
| 284 | 
            +
                end
         | 
| 285 | 
            +
                schema_sample["pattern"] = "\\Ameow"
         | 
| 286 | 
            +
                refute parse
         | 
| 287 | 
            +
                assert_includes error_messages, '"\\\\Ameow" is not an ECMA-262 regular expression.'
         | 
| 288 | 
            +
                assert_includes error_types, :regex_failed
         | 
| 289 | 
            +
              end
         | 
| 290 | 
            +
             | 
| 269 291 | 
             
              def error_messages
         | 
| 270 292 | 
             
                @parser.errors.map { |e| e.message }
         | 
| 271 293 | 
             
              end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,15 +1,29 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: json_schema
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.6.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brandur
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2015-03- | 
| 11 | 
            +
            date: 2015-03-31 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: ecma-re-validator
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '0.1'
         | 
| 20 | 
            +
              type: :development
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '0.1'
         | 
| 13 27 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 28 | 
             
              name: minitest
         | 
| 15 29 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -53,6 +67,7 @@ files: | |
| 53 67 | 
             
            - lib/json_pointer/evaluator.rb
         | 
| 54 68 | 
             
            - lib/json_reference.rb
         | 
| 55 69 | 
             
            - lib/json_schema.rb
         | 
| 70 | 
            +
            - lib/json_schema/configuration.rb
         | 
| 56 71 | 
             
            - lib/json_schema/document_store.rb
         | 
| 57 72 | 
             
            - lib/json_schema/parser.rb
         | 
| 58 73 | 
             
            - lib/json_schema/reference_expander.rb
         |