Skip to content

Commit

Permalink
Refactor integer check into keyword class method
Browse files Browse the repository at this point in the history
  • Loading branch information
davishmcclurg committed Jan 28, 2025
1 parent 386c2a6 commit 752818c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
6 changes: 5 additions & 1 deletion lib/json_schemer/draft202012/vocab/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Draft202012
module Vocab
module Validation
class Type < Keyword
def self.valid_integer?(instance)
instance.is_a?(Numeric) && (instance.is_a?(Integer) || instance.floor == instance)
end

def error(formatted_instance_location:, **)
case value
when 'null'
Expand Down Expand Up @@ -45,7 +49,7 @@ def valid_type(type, instance)
when 'number'
instance.is_a?(Numeric)
when 'integer'
instance.is_a?(Numeric) && (instance.is_a?(Integer) || instance.floor == instance)
self.class.valid_integer?(instance)
when 'string'
instance.is_a?(String)
when 'array'
Expand Down
5 changes: 2 additions & 3 deletions lib/json_schemer/draft4/vocab/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ module Draft4
module Vocab
module Validation
class Type < Draft202012::Vocab::Validation::Type
private
def valid_type(type, instance)
type == 'integer' ? instance.is_a?(Integer) : super
def self.valid_integer?(instance)
instance.is_a?(Integer)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/json_schemer/openapi30/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module OpenAPI30
BASE_URI = URI('json-schemer://openapi30/schema')
# https://spec.openapis.org/oas/v3.0.3#data-types
FORMATS = OpenAPI31::FORMATS.merge(
'int32' => proc { |instance, _format| !instance.is_a?(Integer) || instance.floor.bit_length < 32 },
'int64' => proc { |instance, _format| !instance.is_a?(Integer) || instance.floor.bit_length < 64 },
'int32' => proc { |instance, _format| !Draft4::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 32 },
'int64' => proc { |instance, _format| !Draft4::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 64 },
'byte' => proc { |instance, _value| !instance.is_a?(String) || ContentEncoding::BASE64.call(instance).first },
'binary' => proc { |instance, _value| !instance.is_a?(String) || instance.encoding == Encoding::BINARY },
'date' => Format::DATE
Expand Down
10 changes: 2 additions & 8 deletions lib/json_schemer/openapi31/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ module OpenAPI31
BASE_URI = URI('https://spec.openapis.org/oas/3.1/dialect/base')
# https://spec.openapis.org/oas/v3.1.0#data-types
FORMATS = {
'int32' => proc do |instance, _format|
valid_type = instance.is_a?(Numeric) && (instance.is_a?(Integer) || instance.floor == instance)
!valid_type || instance.floor.bit_length < 32
end,
'int64' => proc do |instance, _format|
valid_type = instance.is_a?(Numeric) && (instance.is_a?(Integer) || instance.floor == instance)
!valid_type || instance.floor.bit_length < 64
end,
'int32' => proc { |instance, _format| !Draft202012::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 32 },
'int64' => proc { |instance, _format| !Draft202012::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 64 },
'float' => proc { |instance, _format| !instance.is_a?(Numeric) || instance.is_a?(Float) },
'double' => proc { |instance, _format| !instance.is_a?(Numeric) || instance.is_a?(Float) },
'password' => proc { |_instance, _format| true }
Expand Down

0 comments on commit 752818c

Please sign in to comment.