Skip to content

Commit

Permalink
dev Schema#match_to_instance, #object_subschemas, #array_subschemas i…
Browse files Browse the repository at this point in the history
…mplemented as Schema instead of Pointer
  • Loading branch information
notEthan committed Dec 9, 2020
1 parent 92f7c19 commit 9b4bd0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 48 deletions.
44 changes: 20 additions & 24 deletions lib/jsi/schema/application/child_application.rb
Expand Up @@ -8,26 +8,24 @@ module Schema::Application::ChildApplication
# @param property_name [String] the property name for which to find subschemas
# @return [Set<JSI::Schema>] subschemas of this schema for the given property_name
def subschemas_for_property_name(property_name)
begin
ptr = self
schema = ptr.evaluate(document)
Set.new.tap do |ptrs|
if schema.respond_to?(:to_hash)
jsi_memoize(__method__, property_name) do |property_name|
Set.new.tap do |subschemas|
if schema_content.respond_to?(:to_hash)
apply_additional = true
if schema.key?('properties') && schema['properties'].respond_to?(:to_hash) && schema['properties'].key?(property_name)
if schema_content.key?('properties') && schema_content['properties'].respond_to?(:to_hash) && schema_content['properties'].key?(property_name)
apply_additional = false
ptrs << ptr['properties'][property_name]
subschemas << subschema(['properties', property_name])
end
if schema['patternProperties'].respond_to?(:to_hash)
schema['patternProperties'].each_key do |pattern|
if schema_content['patternProperties'].respond_to?(:to_hash)
schema_content['patternProperties'].each_key do |pattern|
if property_name.to_s =~ Regexp.new(pattern) # TODO map pattern to ruby syntax
apply_additional = false
ptrs << ptr['patternProperties'][pattern]
subschemas << subschema(['patternProperties', pattern])
end
end
end
if apply_additional && schema.key?('additionalProperties')
ptrs << ptr['additionalProperties']
if apply_additional && schema_content.key?('additionalProperties')
subschemas << subschema(['additionalProperties'])
end
end
end
Expand All @@ -40,19 +38,17 @@ def subschemas_for_property_name(property_name)
# @param index [Integer] the array index for which to find subschemas
# @return [Set<JSI::Schema>] subschemas of this schema for the given array index
def subschemas_for_index(index)
begin
ptr = self
schema = ptr.evaluate(document)
Set.new.tap do |ptrs|
if schema.respond_to?(:to_hash)
if schema['items'].respond_to?(:to_ary)
if schema['items'].each_index.to_a.include?(idx)
ptrs << ptr['items'][idx]
elsif schema.key?('additionalItems')
ptrs << ptr['additionalItems']
jsi_memoize(__method__, index) do |idx|
Set.new.tap do |subschemas|
if schema_content.respond_to?(:to_hash)
if schema_content['items'].respond_to?(:to_ary)
if schema_content['items'].each_index.to_a.include?(idx)
subschemas << subschema(['items', idx])
elsif schema_content.key?('additionalItems')
subschemas << subschema(['additionalItems'])
end
elsif schema.key?('items')
ptrs << ptr['items']
elsif schema_content.key?('items')
subschemas << subschema(['items'])
end
end
end
Expand Down
44 changes: 20 additions & 24 deletions lib/jsi/schema/application/inplace_application.rb
Expand Up @@ -7,45 +7,41 @@ module Schema::Application::InplaceApplication
#
# the returned set will contain this schema itself, unless this schema contains a $ref keyword.
#
# @param other_instance [Object] the instance to check any applicators against
# @param instance [Object] the instance to check any applicators against
# @return [Set<JSI::Schema>] matched applicator schemas
def match_to_instance(other_instance)
ptr = self
schema = ptr.evaluate(document)

Set.new.tap do |ptrs|
if schema.respond_to?(:to_hash)
if schema['$ref'].respond_to?(:to_str)
ptr.deref(document) do |deref_ptr|
ptrs.merge(deref_ptr.schema_match_ptrs_to_instance(document, instance))
def match_to_instance(instance)
Set.new.tap do |schemas|
if schema_content.respond_to?(:to_hash)
if schema_content['$ref'].respond_to?(:to_str)
jsi_ptr.deref(jsi_document) do |deref_ptr|
schemas.merge((resource_root_subschema(deref_ptr)).match_to_instance(instance))
end
else
ptrs << ptr
schemas << self
end
if schema['allOf'].respond_to?(:to_ary)
schema['allOf'].each_index do |i|
ptrs.merge(ptr['allOf'][i].schema_match_ptrs_to_instance(document, instance))
if schema_content['allOf'].respond_to?(:to_ary)
schema_content['allOf'].each_index do |i|
schemas.merge(subschema(['allOf', i]).match_to_instance(instance))
end
end
if schema['anyOf'].respond_to?(:to_ary)
schema['anyOf'].each_index do |i|
valid = ::JSON::Validator.validate(JSI::Typelike.as_json(document), JSI::Typelike.as_json(instance), fragment: ptr['anyOf'][i].fragment)
if valid
ptrs.merge(ptr['anyOf'][i].schema_match_ptrs_to_instance(document, instance))
if schema_content['anyOf'].respond_to?(:to_ary)
schema_content['anyOf'].each_index do |i|
if subschema(['anyOf', i]).validate_instance(instance)
schemas.merge(subschema(['anyOf', i]).match_to_instance(instance))
end
end
end
if schema['oneOf'].respond_to?(:to_ary)
one_i = schema['oneOf'].each_index.detect do |i|
::JSON::Validator.validate(JSI::Typelike.as_json(document), JSI::Typelike.as_json(instance), fragment: ptr['oneOf'][i].fragment)
if schema_content['oneOf'].respond_to?(:to_ary)
one_i = schema_content['oneOf'].each_index.detect do |i|
subschema(['oneOf', i]).validate_instance(instance)
end
if one_i
ptrs.merge(ptr['oneOf'][one_i].schema_match_ptrs_to_instance(document, instance))
schemas.merge(subschema(['oneOf', one_i]).match_to_instance(instance))
end
end
# TODO dependencies
else
ptrs << ptr
schemas << self
end
end
end
Expand Down

0 comments on commit 9b4bd0f

Please sign in to comment.