json-schema 0.1.5 → 0.1.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/lib/json-schema/validator.rb +72 -114
- metadata +4 -4
| @@ -5,18 +5,21 @@ require 'bigdecimal' | |
| 5 5 |  | 
| 6 6 | 
             
            module JSON
         | 
| 7 7 |  | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
                  
         | 
| 11 | 
            -
                  def initialize(message, fragments, schema)
         | 
| 12 | 
            -
                    @fragments = fragments
         | 
| 13 | 
            -
                    @schema = schema
         | 
| 14 | 
            -
                    super(message)
         | 
| 15 | 
            -
                  end
         | 
| 16 | 
            -
                end
         | 
| 17 | 
            -
              
         | 
| 18 | 
            -
                class Validator
         | 
| 8 | 
            +
              class ValidationError < Exception
         | 
| 9 | 
            +
                attr_reader :fragments, :schema
         | 
| 19 10 |  | 
| 11 | 
            +
                def initialize(message, fragments, schema)
         | 
| 12 | 
            +
                  @fragments = fragments
         | 
| 13 | 
            +
                  @schema = schema
         | 
| 14 | 
            +
                  super(message)
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              class Validator
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                @@schemas = {}
         | 
| 21 | 
            +
                @@cache_schemas = false
         | 
| 22 | 
            +
             | 
| 20 23 | 
             
                ValidationMethods = [
         | 
| 21 24 | 
             
                  "type",
         | 
| 22 25 | 
             
                  "disallow",
         | 
| @@ -42,10 +45,9 @@ module JSON | |
| 42 45 |  | 
| 43 46 |  | 
| 44 47 | 
             
                def initialize(schema_data, data)
         | 
| 45 | 
            -
                  @schemas = {}
         | 
| 46 48 | 
             
                  @base_schema = initialize_schema(schema_data)
         | 
| 47 49 | 
             
                  @data = initialize_data(data)
         | 
| 48 | 
            -
                   | 
| 50 | 
            +
                  Validator.add_schema(@base_schema)
         | 
| 49 51 |  | 
| 50 52 | 
             
                  build_schemas(@base_schema)
         | 
| 51 53 | 
             
                end  
         | 
| @@ -55,8 +57,10 @@ module JSON | |
| 55 57 | 
             
                def validate()
         | 
| 56 58 | 
             
                  begin
         | 
| 57 59 | 
             
                    validate_schema(@base_schema, @data, [])
         | 
| 60 | 
            +
                    Validator.clear_cache
         | 
| 58 61 | 
             
                    return true
         | 
| 59 62 | 
             
                  rescue ValidationError
         | 
| 63 | 
            +
                    Validator.clear_cache
         | 
| 60 64 | 
             
                    return false
         | 
| 61 65 | 
             
                  end
         | 
| 62 66 | 
             
                end
         | 
| @@ -66,7 +70,13 @@ module JSON | |
| 66 70 | 
             
                # a ValidationError will be raised with links to the specific location that the first error
         | 
| 67 71 | 
             
                # occurred during validation 
         | 
| 68 72 | 
             
                def validate2()
         | 
| 69 | 
            -
                   | 
| 73 | 
            +
                  begin
         | 
| 74 | 
            +
                    validate_schema(@base_schema, @data, [])
         | 
| 75 | 
            +
                    Validator.clear_cache
         | 
| 76 | 
            +
                  rescue ValidationError
         | 
| 77 | 
            +
                    Validator.clear_cache
         | 
| 78 | 
            +
                    raise $!
         | 
| 79 | 
            +
                  end
         | 
| 70 80 | 
             
                  nil
         | 
| 71 81 | 
             
                end
         | 
| 72 82 |  | 
| @@ -453,7 +463,7 @@ module JSON | |
| 453 463 |  | 
| 454 464 | 
             
                  # Grab the parent schema from the schema list
         | 
| 455 465 | 
             
                  schema_key = temp_uri.to_s.split("#")[0]
         | 
| 456 | 
            -
                  ref_schema =  | 
| 466 | 
            +
                  ref_schema = Validator.schemas[schema_key]
         | 
| 457 467 |  | 
| 458 468 | 
             
                  if ref_schema
         | 
| 459 469 | 
             
                    # Perform fragment resolution to retrieve the appropriate level for the schema
         | 
| @@ -495,10 +505,10 @@ module JSON | |
| 495 505 | 
             
                    uri.fragment = nil
         | 
| 496 506 | 
             
                  end
         | 
| 497 507 |  | 
| 498 | 
            -
                  if  | 
| 508 | 
            +
                  if Validator.schemas[uri.to_s].nil?
         | 
| 499 509 | 
             
                    begin
         | 
| 500 510 | 
             
                      schema = JSON::Schema.new(JSON.parse(open(uri.to_s).read), uri)
         | 
| 501 | 
            -
                       | 
| 511 | 
            +
                      Validator.add_schema(schema)
         | 
| 502 512 | 
             
                      build_schemas(schema)
         | 
| 503 513 | 
             
                    rescue JSON::ParserError
         | 
| 504 514 | 
             
                      # Don't rescue this error, we want JSON formatting issues to bubble up
         | 
| @@ -513,69 +523,26 @@ module JSON | |
| 513 523 |  | 
| 514 524 | 
             
                # Build all schemas with IDs, mapping out the namespace
         | 
| 515 525 | 
             
                def build_schemas(parent_schema)
         | 
| 516 | 
            -
                  if parent_schema.schema["type"] && parent_schema.schema["type"].is_a?(Array) # If we're dealing with a Union type, there might be schemas a-brewin'
         | 
| 517 | 
            -
                    parent_schema.schema["type"].each_with_index do |type,i|
         | 
| 518 | 
            -
                      if type.is_a?(Hash) 
         | 
| 519 | 
            -
                        if type['$ref']
         | 
| 520 | 
            -
                          load_ref_schema(parent_schema, type['$ref'])
         | 
| 521 | 
            -
                        else
         | 
| 522 | 
            -
                          schema_uri = parent_schema.uri.clone
         | 
| 523 | 
            -
                          schema = JSON::Schema.new(type,schema_uri)
         | 
| 524 | 
            -
                          if type['id']
         | 
| 525 | 
            -
                            @schemas[schema.uri.to_s] = schema
         | 
| 526 | 
            -
                          end
         | 
| 527 | 
            -
                          build_schemas(schema)
         | 
| 528 | 
            -
                        end
         | 
| 529 | 
            -
                      end
         | 
| 530 | 
            -
                    end
         | 
| 531 | 
            -
                  end
         | 
| 532 526 |  | 
| 533 | 
            -
                   | 
| 534 | 
            -
             | 
| 535 | 
            -
             | 
| 536 | 
            -
             | 
| 537 | 
            -
             | 
| 538 | 
            -
             | 
| 539 | 
            -
                          type['id']
         | 
| 540 | 
            -
                          schema_uri = parent_schema.uri.clone
         | 
| 541 | 
            -
                          schema = JSON::Schema.new(type,schema_uri)
         | 
| 542 | 
            -
                          if type['id']
         | 
| 543 | 
            -
                            @schemas[schema.uri.to_s] = schema
         | 
| 544 | 
            -
                          end
         | 
| 545 | 
            -
                          build_schemas(schema)
         | 
| 527 | 
            +
                  # Check for schemas in union types
         | 
| 528 | 
            +
                  ["type", "disallow"].each do |key|
         | 
| 529 | 
            +
                    if parent_schema.schema[key] && parent_schema.schema[key].is_a?(Array)
         | 
| 530 | 
            +
                      parent_schema.schema[key].each_with_index do |type,i|
         | 
| 531 | 
            +
                        if type.is_a?(Hash) 
         | 
| 532 | 
            +
                          handle_schema(parent_schema, type)
         | 
| 546 533 | 
             
                        end
         | 
| 547 534 | 
             
                      end
         | 
| 548 535 | 
             
                    end
         | 
| 549 536 | 
             
                  end
         | 
| 550 537 |  | 
| 538 | 
            +
                  # All properties are schemas
         | 
| 551 539 | 
             
                  if parent_schema.schema["properties"]
         | 
| 552 540 | 
             
                    parent_schema.schema["properties"].each do |k,v|
         | 
| 553 | 
            -
                       | 
| 554 | 
            -
                        load_ref_schema(parent_schema, v['$ref'])
         | 
| 555 | 
            -
                      else
         | 
| 556 | 
            -
                        schema_uri = parent_schema.uri.clone
         | 
| 557 | 
            -
                        schema = JSON::Schema.new(v,schema_uri)
         | 
| 558 | 
            -
                        if v['id']
         | 
| 559 | 
            -
                          @schemas[schema.uri.to_s] = schema
         | 
| 560 | 
            -
                        end
         | 
| 561 | 
            -
                        build_schemas(schema)
         | 
| 562 | 
            -
                      end
         | 
| 563 | 
            -
                    end
         | 
| 564 | 
            -
                  end
         | 
| 565 | 
            -
                  
         | 
| 566 | 
            -
                  if parent_schema.schema["additionalProperties"].is_a?(Hash) 
         | 
| 567 | 
            -
                    if parent_schema.schema["additionalProperties"]["$ref"]
         | 
| 568 | 
            -
                      load_ref_schema(parent_schema, parent_schema.schema["additionalProperties"]["$ref"])
         | 
| 569 | 
            -
                    else
         | 
| 570 | 
            -
                      schema_uri = parent_schema.uri.clone
         | 
| 571 | 
            -
                      schema = JSON::Schema.new(parent_schema.schema["additionalProperties"],schema_uri)
         | 
| 572 | 
            -
                      if parent_schema.schema["additionalProperties"]['id']
         | 
| 573 | 
            -
                        @schemas[schema.uri.to_s] = schema
         | 
| 574 | 
            -
                      end
         | 
| 575 | 
            -
                      build_schemas(schema)
         | 
| 541 | 
            +
                      handle_schema(parent_schema, v)
         | 
| 576 542 | 
             
                    end
         | 
| 577 543 | 
             
                  end
         | 
| 578 544 |  | 
| 545 | 
            +
                  # Items are always schemas
         | 
| 579 546 | 
             
                  if parent_schema.schema["items"]
         | 
| 580 547 | 
             
                    items = parent_schema.schema["items"].clone
         | 
| 581 548 | 
             
                    single = false
         | 
| @@ -584,57 +551,31 @@ module JSON | |
| 584 551 | 
             
                      single = true
         | 
| 585 552 | 
             
                    end
         | 
| 586 553 | 
             
                    items.each_with_index do |item,i|
         | 
| 587 | 
            -
                       | 
| 588 | 
            -
                        load_ref_schema(parent_schema, item['$ref'])
         | 
| 589 | 
            -
                      else
         | 
| 590 | 
            -
                        schema_uri = parent_schema.uri.clone
         | 
| 591 | 
            -
                        schema = JSON::Schema.new(item,schema_uri)
         | 
| 592 | 
            -
                        if item['id']
         | 
| 593 | 
            -
                          @schemas[schema.uri.to_s] = schema
         | 
| 594 | 
            -
                        end
         | 
| 595 | 
            -
                        build_schemas(schema)
         | 
| 596 | 
            -
                      end
         | 
| 554 | 
            +
                      handle_schema(parent_schema, item)
         | 
| 597 555 | 
             
                    end
         | 
| 598 556 | 
             
                  end
         | 
| 599 557 |  | 
| 600 | 
            -
                   | 
| 601 | 
            -
             | 
| 602 | 
            -
             | 
| 603 | 
            -
             | 
| 604 | 
            -
                      schema_uri = parent_schema.uri.clone
         | 
| 605 | 
            -
                      schema = JSON::Schema.new(parent_schema.schema["additionalItems"],schema_uri)
         | 
| 606 | 
            -
                      if parent_schema.schema["additionalItems"]['id']
         | 
| 607 | 
            -
                        @schemas[schema.uri.to_s] = schema
         | 
| 608 | 
            -
                      end
         | 
| 609 | 
            -
                      build_schemas(schema)
         | 
| 558 | 
            +
                  # Each of these might be schemas
         | 
| 559 | 
            +
                  ["additionalProperties", "additionalItems", "dependencies", "extends"].each do |key|
         | 
| 560 | 
            +
                    if parent_schema.schema[key].is_a?(Hash) 
         | 
| 561 | 
            +
                      handle_schema(parent_schema, parent_schema.schema[key])
         | 
| 610 562 | 
             
                    end
         | 
| 611 563 | 
             
                  end
         | 
| 612 564 |  | 
| 613 | 
            -
             | 
| 614 | 
            -
             | 
| 615 | 
            -
             | 
| 616 | 
            -
             | 
| 617 | 
            -
             | 
| 618 | 
            -
             | 
| 619 | 
            -
             | 
| 620 | 
            -
             | 
| 621 | 
            -
             | 
| 622 | 
            -
             | 
| 623 | 
            -
             | 
| 624 | 
            -
             | 
| 625 | 
            -
             | 
| 626 | 
            -
             | 
| 627 | 
            -
                    if parent_schema.schema["extends"]['$ref']
         | 
| 628 | 
            -
                      load_ref_schema(parent_schema, parent_schema.schema["extends"]['$ref'])
         | 
| 629 | 
            -
                    else
         | 
| 630 | 
            -
                      schema_uri = parent_schema.uri.clone
         | 
| 631 | 
            -
                      schema = JSON::Schema.new(parent_schema.schema["extends"],schema_uri)
         | 
| 632 | 
            -
                      if parent_schema.schema["extends"]['id']
         | 
| 633 | 
            -
                        @schemas[schema.uri.to_s] = schema
         | 
| 634 | 
            -
                      end
         | 
| 635 | 
            -
                      build_schemas(schema)
         | 
| 636 | 
            -
                    end
         | 
| 637 | 
            -
                  end
         | 
| 565 | 
            +
                end
         | 
| 566 | 
            +
                
         | 
| 567 | 
            +
                # Either load a reference schema or create a new schema
         | 
| 568 | 
            +
                def handle_schema(parent_schema, obj)
         | 
| 569 | 
            +
                  if obj['$ref']
         | 
| 570 | 
            +
                     load_ref_schema(parent_schema, obj['$ref'])
         | 
| 571 | 
            +
                   else
         | 
| 572 | 
            +
                     schema_uri = parent_schema.uri.clone
         | 
| 573 | 
            +
                     schema = JSON::Schema.new(obj,schema_uri)
         | 
| 574 | 
            +
                     if obj['id']
         | 
| 575 | 
            +
                       Validator.add_schema(schema)
         | 
| 576 | 
            +
                     end
         | 
| 577 | 
            +
                     build_schemas(schema)
         | 
| 578 | 
            +
                   end
         | 
| 638 579 | 
             
                end
         | 
| 639 580 |  | 
| 640 581 |  | 
| @@ -648,6 +589,23 @@ module JSON | |
| 648 589 | 
             
                    validator = JSON::Validator.new(schema, data)
         | 
| 649 590 | 
             
                    validator.validate2
         | 
| 650 591 | 
             
                  end
         | 
| 592 | 
            +
                  
         | 
| 593 | 
            +
                  def clear_cache
         | 
| 594 | 
            +
                    @@schemas = {} if @@cache_schemas == false
         | 
| 595 | 
            +
                  end
         | 
| 596 | 
            +
                  
         | 
| 597 | 
            +
                  def schemas
         | 
| 598 | 
            +
                    @@schemas
         | 
| 599 | 
            +
                  end
         | 
| 600 | 
            +
                  
         | 
| 601 | 
            +
                  def add_schema(schema)
         | 
| 602 | 
            +
                    @@schemas[schema.uri.to_s] = schema if @@schemas[schema.uri.to_s].nil?
         | 
| 603 | 
            +
                  end
         | 
| 604 | 
            +
                  
         | 
| 605 | 
            +
                  def cache_schemas=(val)
         | 
| 606 | 
            +
                    @@cache_schemas = val == true ? true : false
         | 
| 607 | 
            +
                  end
         | 
| 608 | 
            +
                    
         | 
| 651 609 | 
             
                end
         | 
| 652 610 |  | 
| 653 611 |  | 
    
        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:  | 
| 4 | 
            +
              hash: 23
         | 
| 5 5 | 
             
              prerelease: false
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 8 | 
             
              - 1
         | 
| 9 | 
            -
              -  | 
| 10 | 
            -
              version: 0.1. | 
| 9 | 
            +
              - 6
         | 
| 10 | 
            +
              version: 0.1.6
         | 
| 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- | 
| 18 | 
            +
            date: 2010-12-02 00:00:00 -05:00
         | 
| 19 19 | 
             
            default_executable: 
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         |