-
Notifications
You must be signed in to change notification settings - Fork 198
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
Conversation
Note WRT enums validation: 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! So how about throwing an error when creating a schema that has atom keys in |
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) |
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.
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.
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.
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...
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 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
.
- Validate enums of non-string types - Validate anyOf/oneOf/allOf/not before type testing - Improve error message for regex validations
- Validate enums of non-string types - Validate anyOf/oneOf/allOf/not before type testing - Improve error message for regex validations
Incorporate improvements from #42
I've manually incorporated most of the changes from this PR, and captured the Enum issue in #60. |
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! :)