spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Raise Warning/Error for required but undefined properties

Open MalteEbner opened this issue 4 years ago • 1 comments

Describe the bug Inside a schema: If a property is required, but not defined, no error or warning is thrown.

To Reproduce schema.yml:

ExampleSchema:
  type: object
  required:
    - prop1
    - prop2
  properties:
    prop1:
      type: string

api.yml:

openapi: 3.0.3
info:
  title: 'Example API'
  description: 'blub'
  version: 1.0.0

paths:
  /example:
    get:
      description: example
      operationId: exampleGet
      responses:
        '200':
          description: Get successful
          content:
            application/json:
              schema:
                $ref: 'schema.yml#/ExampleSchema'
  1. Given the two documents above...
  2. Run the CLI command spectral lint api.yml
  3. No error or warning is thrown that ExampleSchema.prop2 is undefined.

Expected behavior There is a warning or error that ExampleSchema.prop2 is undefined.

Environment:

  • Spectral version: 5.8.0
  • OS: MacOS BigSur 11.1

MalteEbner avatar Mar 19 '21 10:03 MalteEbner

Hey @MalteEbner! Thanks for pointing this out. While we don’t cover all cases with our OAS/AsyncAPI ruleset. It covers the most common ones, and then anybody can define custom rules extending the existing ruleset.

This is certainly something we should add to our OAS ruleset as it's a pretty common validation use case. We'll probably take this up in the next few weeks. I’d really appreciate if you could create a pull request for this though or add issues for other cases that you expect in these rulesets. We love all kinds of contribution from community.

mnaumanali94 avatar Jun 15 '21 13:06 mnaumanali94

I'm also looking foward to a rule for OAS 2/3 (or more correctly, JSON schema) that detects undefined required properties. Minimal example that should produce an error:

openapi: 3.0.3
components:
  schemas:
    foo:
      type: object
      required: [baz] # error: property "baz" is not defined
      properties:
        bar:
          type: string

silverwind avatar Nov 07 '22 13:11 silverwind