Skip to content

Handle anyOf, allOf, oneOf and not schema keywords #42

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

Closed
wants to merge 9 commits into from

Conversation

fenollp
Copy link
Contributor

@fenollp fenollp commented Aug 27, 2018

Fixes 80% of #23

BTW testing needs a great overall. I'll do it using https://github.com/alfert/propcheck when I find the time.
Also, validation is far from correct in many places (this is hard to get right!). See for example the few clauses I had to patch even though they are not related to this PR.

Thanks a lot for creating this package however! :)

@fenollp
Copy link
Contributor Author

fenollp commented Aug 28, 2018

Note WRT enums validation: cast does not "atomify" keys in maps of enum payloads. In other words, the following code does not crash:

schema = %Schema{enum: [%{id: 42}]}
spec = ...
# string keys
{:ok, data} = OpenApiSpex.cast(spec, schema, %{"id" => 42})
{:error, _} = OpenApiSpex.validate(spec, schema, data)
# atom keys
{:ok, data} = OpenApiSpex.cast(spec, schema, %{id: 42})
:ok = OpenApiSpex.validate(spec, schema, data)

I think it would be very dangerous (OOM) to atomify enum keys anyway!
My current workaround is to specify enum schemas with string keys instead, which can be easily missed by anyone.

So how about throwing an error when creating a schema that has atom keys in enum: ...?

@mbuhot
Copy link
Collaborator

mbuhot commented Aug 28, 2018

Thanks for this! Looking pretty good. I'll take a closer read through the code this week.

@@ -470,6 +502,15 @@ defmodule OpenApiSpex.Schema do
:ok
end
end
# Note: OpenAPI3's `{"nullable": true}` really means JSON schema's `{}` (i.e. anything)
Copy link
Collaborator

@mbuhot mbuhot Sep 9, 2018

Choose a reason for hiding this comment

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

Can you clarify this comment?

From https://swagger.io/docs/specification/data-models/data-types/:

Null
OpenAPI 3.0 does not have an explicit null type as in JSON Schema, but you can use nullable: true to specify that the value may be null.
The example above may be mapped to the nullable types int? in C# and java.lang.Integer in Java.

My interpretation is that the value must match the schema or be null, not anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is also my interpretation.
However the schema in {"nullable": true} is {} which is the schema that matches anything. Do you want me to remove that comment maybe?

I'd be much happier if we'd use ex_json_schema instead of rolling out our own schema handling...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I get it now after reading https://swagger.io/docs/specification/data-models/data-types/#any. Any type is allowed when no type is specified, and nullable: true additionally allows null.

mbuhot added a commit that referenced this pull request Oct 28, 2018
 - Validate enums of non-string types
 - Validate anyOf/oneOf/allOf/not before type testing
 - Improve error message for regex validations
mbuhot added a commit that referenced this pull request Oct 28, 2018
 - Validate enums of non-string types
 - Validate anyOf/oneOf/allOf/not before type testing
 - Improve error message for regex validations
mbuhot added a commit that referenced this pull request Oct 28, 2018
@mbuhot
Copy link
Collaborator

mbuhot commented Oct 28, 2018

I've manually incorporated most of the changes from this PR, and captured the Enum issue in #60.

@mbuhot mbuhot closed this Oct 28, 2018
@fenollp fenollp deleted the validate-Of branch October 29, 2018 08:46
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.

2 participants