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

Add rule to validate all schemas and examples in schemas #386

Open
dlyz opened this issue Sep 29, 2021 · 5 comments
Open

Add rule to validate all schemas and examples in schemas #386

dlyz opened this issue Sep 29, 2021 · 5 comments
Labels
p2 question Further information is requested Type: Enhancement

Comments

@dlyz
Copy link

dlyz commented Sep 29, 2021

Is your feature request related to a problem? Please describe.

I made an OpenAPI document, that passes validation, but is not fully conformant with OpenAPI. In the doc there was a schema like:

nullable: true
allOf:
  - $ref: .\DateTime.yaml

According to OpenAPI spec:

nullable boolean: A true value adds "null" to the allowed type specified by the type keyword, only if type is explicitly defined within the same Schema Object. Other Schema Object constraints retain their defined behavior, and therefore may disallow the use of null as a value. A false value leaves the specified or default type unmodified. The default value is false.

So as I understand, nullable: true makes sense only with explicit type property in the same json-object.

I discovered the problem, when tried to use for my api-spec openapi-request-validator, that uses ajv, that fails with the exception just like in #319 ("nullable" cannot be used without "type").

For now, there is built-in 'no-invalid-media-type-examples', that only validates examples in media type objects. ajv validator implementation will check the schema itself for correctness on construction and will detect problems with the schema (like "nullable" cannot be used without "type"). But:

  1. If there is no examples, schema itself will not be validated.
  2. Other than schemas in media types (like schemas of parameters) will not be validated.
  3. Examples in schemas (example property in OpenAPI spec) will not be validated.
  4. json-schema format is not considered during validation (tried with date-time, others probably ignored too).

Describe the solution you'd like

It will be useful to have a built-in rule to validate all schemas itself. Or maybe all schemas should be validated by default regardless of any rules config.

Also it will be useful to have a rule to validate examples in all schemas (nested too).

And of course, format should be considered during examples validation (in 'no-invalid-media-type-examples' too).

@adamaltman
Copy link
Member

AJV validates JSON Schema. OAS 3.0 is not valid JSON Schema... consider some OpenAPI-specific features. OAS 3.0 is an extended subset of JSON Schema:

  • discriminator (not handled by JSON Schema)
  • nullable (JSON Schema addresses it by having an array of types which can include null as a type)
  • deprecated (was only defined in a newer version of JSON Schema)

Maybe you should consider updating to OAS 3.1.

@dlyz
Copy link
Author

dlyz commented Oct 6, 2021

OAS 3.0 is not valid JSON Schema... consider some OpenAPI-specific features.

Yes, i understand that, but how is that related to the problem and requested features? AJV extended with nullable support only helps to detect a problem with incompatibilities with OAS 3+.

Maybe you should consider updating to OAS 3.1.

Also don't understand how it helps here. How does this affect requested features?

@dlyz
Copy link
Author

dlyz commented Oct 6, 2021

Maybe you should consider updating to OAS 3.1.

Oh, I see, nullable was removed in 3.1 and schema now is the superset of json schema 2020-12. There is still a problem with 3.0, but probably it can be ignored, as this is not actual for 3.1.

Also the list of formats was shortened. But it's still will be useful to support common formats like date-time and uuid.

And anyway, it will be useful to have all examples validation.

@adamaltman
Copy link
Member

@lornajane
Copy link
Collaborator

Does the no-invalid-schema-examples help? I think we may have added this since you asked the question.

@lornajane lornajane added the question Further information is requested label Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2 question Further information is requested Type: Enhancement
Projects
None yet
Development

No branches or pull requests

5 participants