Sha256: d9df28ec3634b2de5ec7ebfab86f0c1e10cb7f81f2ee9b9d0a304ab37d3d50b3
Contents?: true
Size: 1.6 KB
Versions: 4
Compression:
Stored size: 1.6 KB
Contents
require 'jschematic/element' module Jschematic module Attributes class Type include Jschematic::Element attr_reader :type def initialize(type) @type = type end def accepts?(instance) return true unless type case type when /^object$/ assert_kind_of([Hash], instance) when /^number$/ assert_kind_of([Float, Integer], instance) when /^integer$/ assert_kind_of([Integer], instance) when /^boolean$/ assert_kind_of([TrueClass, FalseClass], instance) when /^null$/ assert_kind_of([NilClass], instance) when /^any$/ true when Array # union # TODO: this is gross. A specific Union type is likely called for. type.any? do |union_type| begin if String===union_type Type.new(union_type).accepts?(instance) elsif Hash===union_type Schema.new(union_type).accepts?(instance) end rescue ValidationError false end end else # TODO: probably worth just putting in explicit mapping for all # JSON schema types--there are only a few left assert_kind_of([constantize(type)], instance) end end private def assert_kind_of(klassen, instance) klassen.any?{ |klass| instance.kind_of?(klass) } || fail_validation!(klassen, instance) end def constantize(string) Kernel.const_get(string.capitalize) end end end end
Version data entries
4 entries across 4 versions & 1 rubygems