Skip to content

Commit

Permalink
Merge 110ec69 into e697968
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak authored Feb 17, 2023
2 parents e697968 + 110ec69 commit 1d32f61
Show file tree
Hide file tree
Showing 38 changed files with 540 additions and 622 deletions.
82 changes: 0 additions & 82 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 28
# Cop supports --auto-correct.
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
# SupportedHashRocketStyles: key, separator, table
# SupportedColonStyles: key, separator, table
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
Layout/HashAlignment:
Exclude:
- 'test/draft4_test.rb'
- 'test/support/string_validation.rb'
- 'test/support/type_validation.rb'
- 'test/uri_parsing_test.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
Expand All @@ -27,75 +14,6 @@ Layout/MultilineMethodCallBraceLayout:
Exclude:
- 'test/common_test_suite_test.rb'

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented
Layout/MultilineOperationIndentation:
Exclude:
- 'lib/json-schema/attributes/properties.rb'

# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: space, no_space
Layout/SpaceAroundEqualsInParameterDefault:
Exclude:
- 'lib/json-schema/schema.rb'
- 'lib/json-schema/util/uuid.rb'
- 'lib/json-schema/validator.rb'

# Offense count: 48
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator.
# SupportedStylesForExponentOperator: space, no_space
Layout/SpaceAroundOperators:
Exclude:
- 'Gemfile'
- 'lib/json-schema/util/uuid.rb'
- 'test/common_test_suite_test.rb'
- 'test/draft4_test.rb'
- 'test/full_validation_test.rb'
- 'test/uri_parsing_test.rb'

# Offense count: 11
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceBeforeBlockBraces:
Exclude:
- 'lib/json-schema/errors/validation_error.rb'
- 'lib/json-schema/validator.rb'

# Offense count: 13
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideBlockBraces:
Exclude:
- 'lib/json-schema.rb'
- 'lib/json-schema/errors/validation_error.rb'
- 'lib/json-schema/validator.rb'

# Offense count: 1103
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
Enabled: false

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBrackets: space, no_space
Layout/SpaceInsideReferenceBrackets:
Exclude:
- 'json-schema.gemspec'

# Offense count: 1
Lint/AmbiguousBlockAssociation:
Exclude:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ group :release do
gem 'github_changelog_generator', require: false
end

group :coverage, optional: ENV['COVERAGE']!='yes' do
group :coverage, optional: ENV['COVERAGE'] != 'yes' do
gem 'codecov', require: false
gem 'simplecov-console', require: false
end
Expand Down
2 changes: 1 addition & 1 deletion json-schema.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.email = '[email protected]'
s.homepage = 'http://github.com/voxpupuli/json-schema/'
s.summary = 'Ruby JSON Schema Validator'
s.files = Dir[ 'lib/**/*', 'resources/*.json' ]
s.files = Dir['lib/**/*', 'resources/*.json']
s.require_path = 'lib'
s.extra_rdoc_files = ['README.md', 'LICENSE.md']
s.required_ruby_version = '>= 2.5'
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
require 'json-schema/schema/reader'
require 'json-schema/validator'

Dir[File.join(File.dirname(__FILE__), 'json-schema/attributes/**/*.rb')].each {|file| require file }
Dir[File.join(File.dirname(__FILE__), 'json-schema/validators/*.rb')].sort!.each {|file| require file }
Dir[File.join(File.dirname(__FILE__), 'json-schema/attributes/**/*.rb')].each { |file| require file }
Dir[File.join(File.dirname(__FILE__), 'json-schema/validators/*.rb')].sort!.each { |file| require file }
6 changes: 3 additions & 3 deletions lib/json-schema/attributes/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def self.validate(current_schema, data, fragments, processor, validator, options
property = property.to_s

if !data.key?(property) &&
options[:insert_defaults] &&
property_schema.has_key?('default') &&
!property_schema['readonly']
options[:insert_defaults] &&
property_schema.has_key?('default') &&
!property_schema['readonly']
default = property_schema['default']
data[property] = default.is_a?(Hash) ? default.clone : default
end
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/errors/validation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def to_string(subschema_level = 0)
end

def to_hash
base = {schema: @schema.uri, fragment: ::JSON::Schema::Attribute.build_fragment(fragments), message: message_with_schema, failed_attribute: @failed_attribute.to_s.split(':').last.split('Attribute').first}
base = { schema: @schema.uri, fragment: ::JSON::Schema::Attribute.build_fragment(fragments), message: message_with_schema, failed_attribute: @failed_attribute.to_s.split(':').last.split('Attribute').first }
if !@sub_errors.empty?
base[:errors] = @sub_errors.each_with_object({}) do |(subschema, errors), hsh|
subschema_sym = subschema.downcase.gsub(/\W+/, '_').to_sym
hsh[subschema_sym] = Array(errors).map{|e| e.to_hash}
hsh[subschema_sym] = Array(errors).map { |e| e.to_hash }
end
end
base
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
4 changes: 2 additions & 2 deletions lib/json-schema/util/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def write_state fp, c, m # :nodoc:
# invokation. If you want to speed this up, try remounting tmpdir with a
# memory based filesystem (such as tmpfs). STILL slow? then no way but
# rewrite it with c :)
def create clock=nil, time=nil, mac_addr=nil
def create clock = nil, time = nil, mac_addr = nil
c = t = m = nil
Dir.chdir Dir.tmpdir do
unless FileTest.exist? STATE_FILE then
Expand Down Expand Up @@ -177,7 +177,7 @@ def create clock=nil, time=nil, mac_addr=nil
if t.nil? then
# UUID epoch is 1582/Oct/15
tt = Time.now
t = tt.to_i*10000000 + tt.tv_usec*10 + 0x01B21DD213814000
t = tt.to_i * 10000000 + tt.tv_usec * 10 + 0x01B21DD213814000
end
c = c.succ # important; increment here
write_state fp, c, m
Expand Down
52 changes: 26 additions & 26 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class Validator
@@serializer = nil
@@mutex = Mutex.new

def initialize(schema_data, opts={})
def initialize(schema_data, opts = {})
@options = @@default_opts.clone.merge(opts)
@errors = []

configured_validator = self.class.validator_for_name(@options[:version])
@options[:schema_reader] ||= self.class.schema_reader

@validation_options = @options[:record_errors] ? {record_errors: true} : {}
@validation_options = @options[:record_errors] ? { record_errors: true } : {}
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
@validation_options[:strict] = true if @options[:strict] == true
@validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
Expand All @@ -54,7 +54,7 @@ def initialize(schema_data, opts={})
# validate the schema, if requested
if @options[:validate_schema]
# Don't clear the cache during metaschema validation!
meta_validator = self.class.new(@base_schema.validator.metaschema, {clear_cache: false})
meta_validator = self.class.new(@base_schema.validator.metaschema, { clear_cache: false })
meta_validator.validate(@base_schema.schema)
end

Expand Down Expand Up @@ -106,9 +106,9 @@ def validate(data)

if @options[:record_errors]
if @options[:errors_as_objects]
@errors.map{|e| e.to_hash}
@errors.map { |e| e.to_hash }
else
@errors.map{|e| e.to_string}
@errors.map { |e| e.to_string }
end
else
true
Expand Down Expand Up @@ -228,55 +228,55 @@ 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
return false
end
end

def validate_json(schema, data, opts={})
def validate_json(schema, data, opts = {})
validate(schema, data, opts.merge(json: true))
end

def validate_uri(schema, data, opts={})
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

def validate2(schema, data, opts={})
def validate2(schema, data, opts = {})
warn '[DEPRECATION NOTICE] JSON::Validator#validate2 has been replaced by JSON::Validator#validate! and will be removed in version >= 3. Please use the #validate! method instead.'
validate!(schema, data, opts)
end

def validate_json!(schema, data, opts={})
def validate_json!(schema, data, opts = {})
validate!(schema, data, opts.merge(json: true))
end

def validate_uri!(schema, data, opts={})
def validate_uri!(schema, data, opts = {})
validate!(schema, data, opts.merge(uri: true))
end

def fully_validate(schema, data, opts={})
def fully_validate(schema, data, opts = {})
validate!(schema, data, opts.merge(record_errors: true))
end

def fully_validate_schema(schema, opts={})
def fully_validate_schema(schema, opts = {})
data = schema
schema = validator_for_name(opts[:version]).metaschema
fully_validate(schema, data, opts)
end

def fully_validate_json(schema, data, opts={})
def fully_validate_json(schema, data, opts = {})
fully_validate(schema, data, opts.merge(json: true))
end

def fully_validate_uri(schema, data, opts={})
def fully_validate_uri(schema, data, opts = {})
fully_validate(schema, data, opts.merge(uri: true))
end

Expand Down Expand Up @@ -329,7 +329,7 @@ def default_validator
@@default_validator
end

def validator_for_uri(schema_uri, raise_not_found=true)
def validator_for_uri(schema_uri, raise_not_found = true)
return default_validator unless schema_uri

u = JSON::Util::URI.parse(schema_uri)
Expand All @@ -341,7 +341,7 @@ def validator_for_uri(schema_uri, raise_not_found=true)
end
end

def validator_for_name(schema_name, raise_not_found=true)
def validator_for_name(schema_name, raise_not_found = true)
return default_validator unless schema_name

schema_name = schema_name.to_s
Expand All @@ -368,22 +368,22 @@ def register_default_validator(v)
@@default_validator = v
end

def register_format_validator(format, validation_proc, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
def register_format_validator(format, validation_proc, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
custom_format_validator = JSON::Schema::CustomFormat.new(validation_proc)
versions.each do |version|
validator = validator_for_name(version)
validator.formats[format.to_s] = custom_format_validator
end
end

def deregister_format_validator(format, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
def deregister_format_validator(format, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
versions.each do |version|
validator = validator_for_name(version)
validator.formats[format.to_s] = validator.default_formats[format.to_s]
end
end

def restore_default_formats(versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
def restore_default_formats(versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
versions.each do |version|
validator = validator_for_name(version)
validator.formats = validator.default_formats.clone
Expand Down Expand Up @@ -482,11 +482,11 @@ def merge_missing_values(source, destination)
end

if @@json_backend == 'yajl'
@@serializer = lambda{|o| Yajl::Encoder.encode(o) }
@@serializer = lambda { |o| Yajl::Encoder.encode(o) }
elsif @@json_backend == 'json'
@@serializer = lambda{|o| JSON.dump(o) }
@@serializer = lambda { |o| JSON.dump(o) }
else
@@serializer = lambda{|o| YAML.dump(o) }
@@serializer = lambda { |o| YAML.dump(o) }
end
end
end
Expand All @@ -495,10 +495,10 @@ def merge_missing_values(source, destination)

if Gem::Specification::find_all_by_name('uuidtools').any?
require 'uuidtools'
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
@@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 test/bad_schema_ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_bad_uri_ref
schema = {
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'array',
'items' => { '$ref' => '../google.json'},
'items' => { '$ref' => '../google.json' },
}

data = [1, 2, 3]
Expand All @@ -33,7 +33,7 @@ def test_bad_host_ref
schema = {
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'array',
'items' => { '$ref' => 'http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema'},
'items' => { '$ref' => 'http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema' },
}

data = [1, 2, 3]
Expand Down
2 changes: 1 addition & 1 deletion test/common_test_suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.skip?(current_test, file_path)
version = File.basename(suite).to_sym
Dir["#{suite}/**/*.json"].each do |tfile|
test_list = JSON.parse(File.read(tfile))
rel_file = tfile[TEST_DIR.length+1..-1]
rel_file = tfile[TEST_DIR.length + 1..-1]

test_list.each do |test|
schema = test['schema']
Expand Down
Loading

0 comments on commit 1d32f61

Please sign in to comment.