Skip to content

Commit

Permalink
Fix for issue voxpupuli#472
Browse files Browse the repository at this point in the history
  • Loading branch information
jahir-husain committed Jul 24, 2023
1 parent ed559de commit e739c58
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/json-schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def self.validation_errors(validator)
'any' => Object,
}

def self.data_valid_for_type?(data, type)
def self.data_valid_for_type?(data, type, nullable=false)
valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
valid_classes = [valid_classes, NilClass] if nullable
Array(valid_classes).any? { |c| data.is_a?(c) }
end

Expand Down
4 changes: 3 additions & 1 deletion lib/json-schema/attributes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class Schema
class TypeAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
union = true
nullable = false
if options[:disallow]
types = current_schema.schema['disallow']
else
types = current_schema.schema['type']
nullable = current_schema.schema['nullable']
end

if !types.is_a?(Array)
Expand All @@ -22,7 +24,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options

types.each_with_index do |type, type_index|
if type.is_a?(String)
valid = data_valid_for_type?(data, type)
valid = data_valid_for_type?(data, type, nullable)
elsif type.is_a?(Hash) && union
# Validate as a schema
schema = JSON::Schema.new(type, current_schema.uri, validator)
Expand Down
3 changes: 2 additions & 1 deletion lib/json-schema/attributes/type_v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class TypeV4Attribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
union = true
types = current_schema.schema['type']
nullable = current_schema.schema['nullable']
if !types.is_a?(Array)
types = [types]
union = false
end

return if types.any? { |type| data_valid_for_type?(data, type) }
return if types.any? { |type| data_valid_for_type?(data, type, nullable) }

types = types.map { |type| type.is_a?(String) ? type : '(schema)' }.join(', ')
message = format(
Expand Down

0 comments on commit e739c58

Please sign in to comment.