Skip to content
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

Merged

Conversation

Ladas
Copy link
Contributor

@Ladas Ladas commented May 17, 2019

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:

@ota42y ota42y self-requested a review May 20, 2019 06:33
@@ -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
Copy link
Owner

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.

Copy link
Owner

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

Copy link
Owner

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.

Copy link
Contributor Author

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) ?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Ladas added 3 commits May 20, 2019 15:56
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
@Ladas Ladas force-pushed the perform_a_strict_check_on_object_properties branch from 4851245 to da388f8 Compare May 20, 2019 13:56
@Ladas Ladas force-pushed the perform_a_strict_check_on_object_properties branch from fde88c0 to 7615724 Compare May 21, 2019 17:39
@Ladas Ladas changed the title [WIP] Perform a strict check on object properties Perform a strict check on object properties May 21, 2019
Copy link
Owner

@ota42y ota42y left a 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

Ladas added 2 commits May 27, 2019 15:52
Supporting attribute that can be nil or any of the refs and adding
specs for the validation.
@Ladas
Copy link
Contributor Author

Ladas commented May 27, 2019

@ota42y last 2 commits implement the 2 requests you had :-)

Ladas added a commit to Ladas/topological_inventory-ingress_api that referenced this pull request May 28, 2019
The past format of nullable was not validatable, changing
according to new validations and specs in
ota42y/openapi_parser#33
Copy link
Owner

@ota42y ota42y left a 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
Copy link
Owner

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)

Copy link
Contributor Author

@Ladas Ladas May 28, 2019

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)

@@ -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
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok removed

Copy link
Owner

@ota42y ota42y left a 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ota42y ota42y merged commit d210f77 into ota42y:master May 28, 2019
@Ladas
Copy link
Contributor Author

Ladas commented May 28, 2019

@ota42y thank you for the great reviews :-)

@ota42y
Copy link
Owner

ota42y commented Jun 1, 2019

I just released v.0.3.0 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants