Raise Warning/Error for required but undefined properties
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'
- Given the two documents above...
- Run the CLI command
spectral lint api.yml - No error or warning is thrown that
ExampleSchema.prop2is 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
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.
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