class JSchema::Validator::Type

Private Instance Methods

error_message(instance) click to toggle source
# File lib/jschema/validator/type.rb, line 42
def error_message(instance)
  types =
    case @json_types.size
    when 1
      @json_types.first
    when 2
      @json_types.join(' or ')
    when 3..Float::INFINITY
      @json_types[0..-2].join(', ') << ", or #{@json_types.last}"
    end

  "#{instance.inspect} must be a #{types}"
end
json_type_to_ruby_class(json_type) click to toggle source
# File lib/jschema/validator/type.rb, line 29
def json_type_to_ruby_class(json_type)
  case json_type
  when 'object'  then Hash
  when 'null'    then NilClass
  when 'string'  then String
  when 'integer' then Integer
  when 'array'   then Array
  when 'boolean' then Boolean
  when 'number'  then Numeric
  else invalid_schema('type', json_type)
  end
end
post_initialize(type) click to toggle source
# File lib/jschema/validator/type.rb, line 16
def post_initialize(type)
  @json_types = Array(type)
  @ruby_classes = @json_types.map do |json_type|
    json_type_to_ruby_class(json_type)
  end
end
validate_args(type) click to toggle source
# File lib/jschema/validator/type.rb, line 8
def validate_args(type)
  if type.is_a?(String) || non_empty_array?(type)
    true
  else
    invalid_schema 'type', type
  end
end
validate_instance(instance) click to toggle source
# File lib/jschema/validator/type.rb, line 23
def validate_instance(instance)
  unless @ruby_classes.one? { |type| type === instance }
    error_message(instance)
  end
end