-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for readOnly & writeOnly keywords
- Loading branch information
Showing
11 changed files
with
726 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lib/skooma/keywords/oas_3_1/dialect/additional_properties.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module Skooma | ||
module Keywords | ||
module OAS31 | ||
module Dialect | ||
class AdditionalProperties < JSONSkooma::Keywords::Applicator::AdditionalProperties | ||
self.key = "additionalProperties" | ||
self.instance_types = "object" | ||
self.value_schema = :schema | ||
self.depends_on = %w[properties patternProperties] | ||
|
||
def evaluate(instance, result) | ||
known_property_names = result.sibling(instance, "properties")&.schema_node&.keys || [] | ||
known_property_patterns = (result.sibling(instance, "patternProperties")&.schema_node&.keys || []) | ||
.map { |pattern| Regexp.new(pattern) } | ||
|
||
forbidden = [] | ||
|
||
if json.root.enforce_access_modes? | ||
only_key = result.path.include?("responses") ? "writeOnly" : "readOnly" | ||
properties_result = result.sibling(instance, "properties") | ||
instance.each_key do |name| | ||
res = properties_result&.children&.[](instance[name]&.path)&.[]name | ||
forbidden << name.tap { puts "adding #{name}" } if annotation_exists?(res, key: only_key) | ||
end | ||
end | ||
|
||
annotation = [] | ||
error = [] | ||
|
||
instance.each do |name, item| | ||
if forbidden.include?(name) || !known_property_names.include?(name) && known_property_patterns.none? { |pattern| pattern.match?(name) } | ||
if json.evaluate(item, result).passed? | ||
annotation << name | ||
else | ||
error << name | ||
# reset to success for the next iteration | ||
result.success | ||
end | ||
end | ||
end | ||
return result.annotate(annotation) if error.empty? | ||
|
||
result.failure(error) | ||
end | ||
|
||
private | ||
|
||
def annotation_exists?(result, key:) | ||
return result if result.key == key && result.annotation | ||
|
||
result.each_children do |child| | ||
return child if annotation_exists?(child, key: key) | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module Skooma | ||
module Keywords | ||
module OAS31 | ||
module Dialect | ||
class Properties < JSONSkooma::Keywords::Applicator::Properties | ||
self.key = "properties" | ||
self.instance_types = "object" | ||
self.value_schema = :object_of_schemas | ||
|
||
def evaluate(instance, result) | ||
annotation = [] | ||
err_names = [] | ||
instance.each do |name, item| | ||
next unless json.value.key?(name) | ||
|
||
result.call(item, name) do |subresult| | ||
json[name].evaluate(item, subresult) | ||
if ignored_with_only_key?(subresult) | ||
subresult.discard | ||
elsif subresult.passed? | ||
annotation << name | ||
else | ||
err_names << name | ||
end | ||
end | ||
end | ||
|
||
return result.annotate(annotation) if err_names.empty? | ||
|
||
result.failure("Properties #{err_names.join(", ")} are invalid") | ||
end | ||
|
||
private | ||
|
||
def ignored_with_only_key?(subresult) | ||
return false unless json.root.enforce_access_modes? | ||
|
||
if subresult.parent.path.include?("responses") | ||
subresult.children["readOnly"]&.value == true | ||
else | ||
subresult.children["writeOnly"]&.value == true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Skooma | ||
module Keywords | ||
module OAS31 | ||
module Dialect | ||
class Required < JSONSkooma::Keywords::Validation::Required | ||
self.key = "required" | ||
self.instance_types = "object" | ||
self.depends_on = %w[properties] | ||
|
||
def evaluate(instance, result) | ||
missing = required_keys.reject { |key| instance.key?(key) } | ||
return if missing.none? | ||
|
||
if json.root.enforce_access_modes? | ||
properties_schema = result.sibling(instance, "properties")&.schema_node || {} | ||
only_key = result.path.include?("responses") ? "writeOnly" : "readOnly" | ||
ignore = [] | ||
missing.each do |name| | ||
next unless properties_schema.key?(name) | ||
|
||
result.call(nil, name) do |subresult| | ||
properties_schema[name].evaluate(nil, subresult) | ||
ignore << name if annotation_exists?(subresult, key: only_key) | ||
subresult.discard | ||
end | ||
end | ||
|
||
return if (missing - ignore).none? | ||
end | ||
|
||
result.failure(missing_keys_message(missing)) | ||
end | ||
|
||
private | ||
|
||
def annotation_exists?(result, key:) | ||
return result if result.key == key && result.annotation | ||
|
||
result.each_children do |child| | ||
return child if annotation_exists?(child, key: key) | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.