Skip to content

Commit

Permalink
rubocop: Fix Layout/SpaceAfterComma
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Feb 17, 2023
1 parent 2eeb50e commit 42b9a88
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 162 deletions.
5 changes: 0 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ Layout/MultilineOperationIndentation:
Exclude:
- 'lib/json-schema/attributes/properties.rb'

# Offense count: 276
# Cop supports --auto-correct.
Layout/SpaceAfterComma:
Enabled: false

# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
Expand Down
2 changes: 1 addition & 1 deletion json-schema.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.summary = "Ruby JSON Schema Validator"
s.files = Dir[ "lib/**/*", "resources/*.json" ]
s.require_path = "lib"
s.extra_rdoc_files = ["README.md","LICENSE.md"]
s.extra_rdoc_files = ["README.md", "LICENSE.md"]
s.required_ruby_version = ">= 2.5"
s.license = "MIT"
s.required_rubygems_version = ">= 2.5"
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.data_valid_for_type?(data, type)

# Lookup Schema type of given class instance
def self.type_of_data(data)
type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)|
type, _ = TYPE_CLASS_MAPPINGS.map { |k, v| [k, v] }.sort_by { |(_, v)|
-Array(v).map { |klass| klass.ancestors.size }.max
}.find { |(_, v)|
Array(v).any? { |klass| data.kind_of?(klass) }
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/allof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def self.validate(current_schema, data, fragments, processor, validator, options
valid = true

current_schema.schema['allOf'].each_with_index do |element, schema_index|
schema = JSON::Schema.new(element,current_schema.uri,validator)
schema = JSON::Schema.new(element, current_schema.uri, validator)

# We're going to add a little cruft here to try and maintain any validation errors that occur in the allOf
# We'll handle this by keeping an error count before and after validation, extracting those errors and pushing them onto an error array
pre_validation_error_count = validation_errors(processor).count

begin
schema.validate(data,fragments,processor,options)
schema.validate(data, fragments, processor, options)
rescue ValidationError
valid = false
end
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/anyof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def self.validate(current_schema, data, fragments, processor, validator, options
original_data = data.is_a?(Hash) ? data.clone : data

current_schema.schema['anyOf'].each_with_index do |element, schema_index|
schema = JSON::Schema.new(element,current_schema.uri,validator)
schema = JSON::Schema.new(element, current_schema.uri, validator)

# We're going to add a little cruft here to try and maintain any validation errors that occur in the anyOf
# We'll handle this by keeping an error count before and after validation, extracting those errors and pushing them onto a union error
pre_validation_error_count = validation_errors(processor).count

begin
schema.validate(data,fragments,processor,options)
schema.validate(data, fragments, processor, options)
valid = true
rescue ValidationError
# We don't care that these schemas don't validate - we only care that one validated
Expand Down
12 changes: 6 additions & 6 deletions lib/json-schema/attributes/extends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
schemas = current_schema.schema['extends']
schemas = [schemas] if !schemas.is_a?(Array)
schemas.each do |s|
uri,schema = get_extended_uri_and_schema(s, current_schema, validator)
uri, schema = get_extended_uri_and_schema(s, current_schema, validator)
if schema
schema.validate(data, fragments, processor, options)
elsif uri
Expand All @@ -22,26 +22,26 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

def self.get_extended_uri_and_schema(s, current_schema, validator)
uri,schema = nil,nil
uri, schema = nil, nil

if s.is_a?(Hash)
uri = current_schema.uri
if s['$ref']
ref_uri,ref_schema = JSON::Schema::RefAttribute.get_referenced_uri_and_schema(s, current_schema, validator)
ref_uri, ref_schema = JSON::Schema::RefAttribute.get_referenced_uri_and_schema(s, current_schema, validator)
if ref_schema
if s.size == 1 # Check if anything else apart from $ref
uri,schema = ref_uri,ref_schema
uri, schema = ref_uri, ref_schema
else
s = s.dup
s.delete '$ref'
s = ref_schema.schema.merge(s)
end
end
end
schema ||= JSON::Schema.new(s,uri,validator)
schema ||= JSON::Schema.new(s, uri, validator)
end

[uri,schema]
[uri, schema]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module JSON
class Schema
class NotAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
schema = JSON::Schema.new(current_schema.schema['not'],current_schema.uri,validator)
schema = JSON::Schema.new(current_schema.schema['not'], current_schema.uri, validator)
failed = true
errors_copy = processor.validation_errors.clone

begin
schema.validate(data,fragments,processor,options)
schema.validate(data, fragments, processor, options)
# If we're recording errors, we don't throw an exception. Instead, check the errors array length
if options[:record_errors] && errors_copy.length != processor.validation_errors.length
processor.validation_errors.replace(errors_copy)
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/oneof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def self.validate(current_schema, data, fragments, processor, validator, options
valid = false

one_of.each_with_index do |element, schema_index|
schema = JSON::Schema.new(element,current_schema.uri,validator)
schema = JSON::Schema.new(element, current_schema.uri, validator)
pre_validation_error_count = validation_errors(processor).count
begin
schema.validate(data,fragments,processor,options)
schema.validate(data, fragments, processor, options)
success_data = data.is_a?(Hash) ? data.clone : data
valid = true
rescue ValidationError
Expand Down
8 changes: 4 additions & 4 deletions lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module JSON
class Schema
class RefAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
uri,schema = get_referenced_uri_and_schema(current_schema.schema, current_schema, validator)
uri, schema = get_referenced_uri_and_schema(current_schema.schema, current_schema, validator)

if schema
schema.validate(data, fragments, processor, options)
Expand All @@ -20,7 +20,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

def self.get_referenced_uri_and_schema(s, current_schema, validator)
uri,schema = nil,nil
uri, schema = nil, nil

temp_uri = JSON::Util::URI.normalize_ref(s['$ref'], current_schema.uri)

Expand Down Expand Up @@ -51,10 +51,10 @@ def self.get_referenced_uri_and_schema(s, current_schema, validator)

# We have the schema finally, build it and validate!
uri = temp_uri
schema = JSON::Schema.new(target_schema,temp_uri,validator)
schema = JSON::Schema.new(target_schema, temp_uri, validator)
end

[uri,schema]
[uri, schema]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def self.validate(current_schema, data, fragments, processor, validator, options
valid = data_valid_for_type?(data, type)
elsif type.is_a?(Hash) && union
# Validate as a schema
schema = JSON::Schema.new(type,current_schema.uri,validator)
schema = JSON::Schema.new(type, current_schema.uri, validator)

# We're going to add a little cruft here to try and maintain any validation errors that occur in this union type
# We'll handle this by keeping an error count before and after validation, extracting those errors and pushing them onto a union error
pre_validation_error_count = validation_errors(processor).count

begin
schema.validate(data,fragments,processor,options.merge(:disallow => false))
schema.validate(data, fragments, processor, options.merge(:disallow => false))
valid = true
rescue ValidationError
# We don't care that these schemas don't validate - we only care that one validated
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module JSON
class Schema
attr_accessor :schema, :uri, :validator

def initialize(schema,uri,parent_validator=nil)
def initialize(schema, uri, parent_validator=nil)
@schema = schema
@uri = uri

Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def extend_schema_definition(schema_uri)
end

def validate(current_schema, data, fragments, processor, options = {})
current_schema.schema.each do |attr_name,attribute|
current_schema.schema.each do |attr_name, attribute|
if @attributes.has_key?(attr_name.to_s)
@attributes[attr_name.to_s].validate(current_schema, data, fragments, processor, self, options)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.normalized_uri(uri, base_path = Dir.pwd)
# Check for absolute path
if normalized_uri.relative?
data = normalized_uri
data = File.join(base_path, data) if data.path[0,1] != "/"
data = File.join(base_path, data) if data.path[0, 1] != "/"
normalized_uri = file_uri(data)
end
@normalize_cache[uri] = normalized_uri.freeze
Expand Down Expand Up @@ -45,7 +45,7 @@ def self.normalize_ref(ref, base)
path, fragment = ref.to_s.split("#")
if path.nil? || path == ''
ref_uri.path = base_uri.path
elsif path[0,1] == "/"
elsif path[0, 1] == "/"
ref_uri.path = Pathname.new(path).cleanpath.to_s
else
ref_uri.join!(path)
Expand Down
8 changes: 4 additions & 4 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def schema_from_fragment(base_schema, fragment)
def validate(data)
original_data = data
data = initialize_data(data)
@base_schema.validate(data,[],self,@validation_options)
@base_schema.validate(data, [], self, @validation_options)

if @options[:record_errors]
if @options[:errors_as_objects]
Expand Down Expand Up @@ -228,7 +228,7 @@ def validation_errors
end

class << self
def validate(schema, data,opts={})
def validate(schema, data, opts={})
begin
validate!(schema, data, opts)
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
Expand All @@ -244,7 +244,7 @@ def validate_uri(schema, data, opts={})
validate(schema, data, opts.merge(:uri => true))
end

def validate!(schema, data,opts={})
def validate!(schema, data, opts={})
validator = new(schema, opts)
validator.validate(data)
end
Expand Down Expand Up @@ -498,7 +498,7 @@ def merge_missing_values(source, destination)
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
else
require 'json-schema/util/uuid'
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s, JSON::Util::UUID::Nil).to_s }
end

def serialize schema
Expand Down
8 changes: 4 additions & 4 deletions test/bad_schema_ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def test_bad_uri_ref
"items" => { "$ref" => "../google.json"},
}

data = [1,2,3]
data = [1, 2, 3]
error = assert_raises(JSON::Schema::ReadFailed) do
JSON::Validator.validate(schema,data)
JSON::Validator.validate(schema, data)
end

expanded_path = File.expand_path("../../google.json", __FILE__)
Expand All @@ -36,9 +36,9 @@ def test_bad_host_ref
"items" => { "$ref" => "http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema"},
}

data = [1,2,3]
data = [1, 2, 3]
error = assert_raises(JSON::Schema::ReadFailed) do
JSON::Validator.validate(schema,data)
JSON::Validator.validate(schema, data)
end

assert_equal(:uri, error.type)
Expand Down
2 changes: 1 addition & 1 deletion test/draft1_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_disallow
data["a"] = 5
refute_valid schema, data

schema["properties"]["a"]["disallow"] = ["integer","string"]
schema["properties"]["a"]["disallow"] = ["integer", "string"]
data["a"] = 'string'
refute_valid schema, data

Expand Down
2 changes: 1 addition & 1 deletion test/draft2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_disallow
data["a"] = 5
refute_valid schema, data

schema["properties"]["a"]["disallow"] = ["integer","string"]
schema["properties"]["a"]["disallow"] = ["integer", "string"]
data["a"] = 'string'
refute_valid schema, data

Expand Down
Loading

0 comments on commit 42b9a88

Please sign in to comment.