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

Fix more rubocop violations #484

Merged
merged 5 commits into from
Feb 17, 2023
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
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ require:
AllCops:
TargetRubyVersion: 2.5
NewCops: enable

Style/TrailingCommaInHashLiteral:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we agreed on using those in voxpupuli/voxpupuli-test#86

Enabled: True
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
Enabled: True
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArguments:
Enabled: True
EnforcedStyleForMultiline: consistent_comma
32 changes: 0 additions & 32 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 Expand Up @@ -853,27 +848,6 @@ Style/SymbolProc:
- 'lib/json-schema/errors/validation_error.rb'
- 'lib/json-schema/validator.rb'

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Style/TrailingCommaInArrayLiteral:
Exclude:
- 'lib/json-schema/util/uuid.rb'
- 'test/support/test_helper.rb'
- 'test/uri_parsing_test.rb'

# Offense count: 15
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Style/TrailingCommaInHashLiteral:
Exclude:
- 'test/custom_format_test.rb'
- 'test/draft6_test.rb'
- 'test/full_validation_test.rb'
- 'test/one_of_test.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowNamedUnderscoreVariables.
Expand All @@ -889,12 +863,6 @@ Style/WordArray:
EnforcedStyle: percent
MinSize: 3

# Offense count: 1
# Cop supports --auto-correct.
Style/ZeroLengthPredicate:
Exclude:
- 'lib/json-schema/attributes/properties.rb'

# Offense count: 74
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ task :update_meta_schemas do
puts "Updating meta-schemas..."

id_mappings = {
'http://json-schema.org/draft/schema#' => 'https://raw.githubusercontent.com/json-schema-org/json-schema-spec/master/schema.json'
'http://json-schema.org/draft/schema#' => 'https://raw.githubusercontent.com/json-schema-org/json-schema-spec/master/schema.json',
}

require 'open-uri'
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
4 changes: 2 additions & 2 deletions lib/json-schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.validation_errors(validator)
"object" => Hash,
"array" => Array,
"null" => NilClass,
"any" => Object
"any" => Object,
}

def self.data_valid_for_type?(data, type)
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end
end

if diff.size > 0
unless diff.empty?
properties = diff.keys.join(', ')
message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{properties}'"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
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/attributes/type_v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
build_fragment(fragments),
type_of_data(data),
union ? 'one or more of the following types' : 'the following type',
types
types,
)

validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
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
10 changes: 5 additions & 5 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Validator
:insert_defaults => false,
:clear_cache => false,
:strict => false,
:parse_data => true
:parse_data => true,
}
@@validators = {}
@@default_validator = nil
Expand Down 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
4 changes: 2 additions & 2 deletions lib/json-schema/validators/draft1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def initialize
"pattern" => JSON::Schema::PatternAttribute,
"additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
"items" => JSON::Schema::ItemsAttribute,
"extends" => JSON::Schema::ExtendsAttribute
"extends" => JSON::Schema::ExtendsAttribute,
}
@default_formats = {
'date-time' => DateTimeFormat,
'date' => DateFormat,
'time' => TimeFormat,
'ip-address' => IP4Format,
'ipv6' => IP6Format,
'uri' => UriFormat
'uri' => UriFormat,
}
@formats = @default_formats.clone
@uri = JSON::Util::URI.parse("http://json-schema.org/draft-01/schema#")
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/validators/draft2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def initialize
"pattern" => JSON::Schema::PatternAttribute,
"additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
"items" => JSON::Schema::ItemsAttribute,
"extends" => JSON::Schema::ExtendsAttribute
"extends" => JSON::Schema::ExtendsAttribute,
}
@default_formats = {
'date-time' => DateTimeFormat,
'date' => DateFormat,
'time' => TimeFormat,
'ip-address' => IP4Format,
'ipv6' => IP6Format,
'uri' => UriFormat
'uri' => UriFormat,
}
@formats = @default_formats.clone
@uri = JSON::Util::URI.parse("http://json-schema.org/draft-02/schema#")
Expand Down
Loading