Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it possible to have a property named "$ref" #360

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- Made sure we really do clear the cache when instructed to
- It's now possible to use reserved words in property names
- Removed support for setting "extends" to a string (it's invalid json-schema - use a "$ref" instead)

### Changed
- Made all `validate*` methods on `JSON::Validator` ultimately call `validate!`
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/additionalproperties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
if addprop.is_a?(Hash)
matching_properties = extra_properties # & addprop.keys
matching_properties.each do |key|
additional_property_schema = JSON::Schema.new(addprop[key] || addprop, current_schema.uri, validator)
additional_property_schema = JSON::Schema.new(addprop, current_schema.uri, validator)
additional_property_schema.validate(data[key], fragments + [key], processor, options)
end
extra_properties -= matching_properties
Expand Down
2 changes: 0 additions & 2 deletions lib/json-schema/attributes/extends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def self.validate(current_schema, data, fragments, processor, validator, options
def self.get_extended_uri_and_schema(s, current_schema, validator)
uri,schema = nil,nil

s = {'$ref' => s} if s.is_a?(String)

if s.is_a?(Hash)
uri = current_schema.uri
if s['$ref']
Expand Down
14 changes: 14 additions & 0 deletions test/draft4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def test_self_reference
refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
end

def test_property_named_ref
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"properties" => {
"$ref" => {
"type" => "integer"
}
}
}

assert_valid schema, { "$ref" => 1 }
refute_valid schema, { "$ref" => "#" }
end

def test_format_datetime
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
Expand Down
10 changes: 4 additions & 6 deletions test/extends_nested_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ExtendsNestedTest < Minitest::Test

def assert_validity(valid, schema_name, data, msg)
msg = "Schema should be #{valid ? :valid : :invalid}.\n(#{schema_name}) #{msg}"
schema = schema_fixture_path("#{schema_name}.schema.json")
schema = schema_fixture_path("#{schema_name}_schema.json")
errors = JSON::Validator.fully_validate(schema, data)

if valid
Expand All @@ -15,10 +15,8 @@ def assert_validity(valid, schema_name, data, msg)
end

%w[
extends_and_additionalProperties-1-filename
extends_and_additionalProperties-1-ref
extends_and_additionalProperties-2-filename
extends_and_additionalProperties-2-ref
extends_and_additionalProperties_false
extends_and_patternProperties
].each do |schema_name|
test_prefix = 'test_' + schema_name.gsub('-','_')

Expand All @@ -40,7 +38,7 @@ def #{test_prefix}_invalid_inner
end
EOB

if schema_name['extends_and_additionalProperties-1']
if schema_name['extends_and_additionalProperties_false']
class_eval <<-EOB
def #{test_prefix}_invalid_outer
assert_validity false, '#{schema_name}', {"whaaaaat"=>true}, "Outer defn allowing anything when it shouldn't"
Expand Down
34 changes: 0 additions & 34 deletions test/schemas/extends_and_additionalProperties-1-ref.schema.json

This file was deleted.

33 changes: 0 additions & 33 deletions test/schemas/extends_and_additionalProperties-2-ref.schema.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"extends": "inner.schema.json",
"extends": {"$ref":"inner_schema.json#"},
"properties": {
"outerA": {
"description": "blah",
Expand All @@ -20,7 +20,7 @@
"minItems": 1,
"maxItems": 50,
"items": {
"extends": "inner.schema.json",
"extends": {"$ref":"inner_schema.json#"},
"additionalProperties": false
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"extends": "inner.schema.json",
"additionalProperties": {
"extends": {"$ref":"inner_schema.json#"},
"patternProperties": {
"outerA": {
"description": "blah",
"required": false,
Expand All @@ -20,7 +20,7 @@
"minItems": 1,
"maxItems": 50,
"items": {
"extends": "inner.schema.json",
"extends": {"$ref":"inner_schema.json#"},
"additionalProperties": false
}
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/test-suite
Submodule test-suite updated from 935596 to 5fb3d9