-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Perform a strict check on object properties #33
Perform a strict check on object properties #33
Conversation
@@ -7,6 +7,9 @@ class AllOfValidator < Base | |||
def coerce_and_validate(value, schema) | |||
# if any schema return error, it's not valida all of value | |||
schema.all_of.each do |s| | |||
# We need to store the reference to all of, so we can perform strict check on allowed properties | |||
s.parent_all_of = schema.all_of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Schema object should be immutable.
And schema's parent isn't always the same (e.g, when we use reference, one object have many parent)
so it becomes a problem when multi-threading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotExistPropertyDefinition error have property name which not defined in s
.
I think this validator validate all schemas in all_of section and collect not defined keys.
And returns error if there is property name not included in all the definitions.
This is sample code
remain_keys = value.keys
schema.all_of.each do |s|
_coerced, errs = validatable.validate_schema(value, s, early_return=false)
errors_key = get_errors_key(errs)
remain_keys = remain_keys & errors_key
end
raise 'not exist key' if remain_keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this sample code dosen't work because validate_schema method return only one error.
So I think we should add early_return option which return all validate errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, doing it here with schema.all_of.each
would solve only a part of the issue. Because there is a normal object validation, which changes if the object is under allOf
https://github.com/ota42y/openapi_parser/pull/33/files#diff-0662b153c9d300d215ae97087f9fa776R18
So I can do the validation here, but I will still need to pass something to this nested validation, so we can skip it at least. So maybe passing another argument to validatable.validate_schema(value, s)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so I moved the parent_all_of to argument fde88c0 and now trying to figure out how to move it up and aggregate, but still it won;t be perfect, because it can throw also OpenAPIParser::NotExistRequiredKey and we can't aggregate 2 different errors into one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ota42y ok, it's done
We need to throw a validation error if property being send is not defined. If additionalProperties are defined, we skip the validation for now, with TODO added. In a case of composing schema using allOf, the property needs to be defined at least in 1 of te allOf items, as stated in: https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof
4851245
to
da388f8
Compare
Aggregating either for the whole allOf block or the object.
fde88c0
to
7615724
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much! I think almost good!
I have some questions, so I want to know more about it
Supporting attribute that can be nil or any of the refs and adding specs for the validation.
@ota42y last 2 commits implement the 2 requests you had :-) |
The past format of nullable was not validatable, changing according to new validations and specs in ota42y/openapi_parser#33
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost ok!
Just one simple fix please.
return [nil, OpenAPIParser::NotExistRequiredKey.new(required_set.to_a, schema.object_reference)] unless required_set.empty? | ||
|
||
value.merge!(coerced_values.to_h) if @coerce_value | ||
|
||
[value, nil] | ||
end | ||
|
||
def validate_using_additional_properties(schema, name, v) | ||
# TODO: we need to perform a validation based on schema.additional_properties here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need implement this method in this PR.
Because this PR is for adding strict check feature and we can add this feature later.
So I will merge this and release a new version.
(If you have a problem, please tell me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removing the method for now, but keeping the todo, so we know where to touch (and we can search for 'additionalProperties' string)
lib/openapi_parser/schemas/base.rb
Outdated
@@ -6,6 +6,9 @@ class Base | |||
|
|||
attr_reader :parent, :raw_schema, :object_reference, :root | |||
|
|||
# Parent allOf reference, if schema is nested in allOf | |||
attr_accessor :parent_all_of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code is unnecessary, so please delete it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, right, I forgot it there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for adding great features!!!
@@ -17,8 +17,9 @@ def coerce_and_validate(value, schema, parent_all_of: false) | |||
remaining_keys.delete(name) | |||
validatable.validate_schema(v, s) | |||
else | |||
# Property is not defined, try validate using additionalProperties | |||
validate_using_additional_properties(schema, name, v) | |||
# TODO: we need to perform a validation based on schema.additional_properties here, if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@ota42y thank you for the great reviews :-) |
I just released v.0.3.0 👍 |
Perform a strict check on properties
We need to throw a validation error if property being send is
not defined. If additionalProperties are defined, we skip the
validation for now, with TODO added.
In a case of composing schema using allOf, the property needs
to be defined at least in 1 of te allOf items, as stated in:
https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof
Depends on: