Skip to content

feat(rulesets): add object-with-properties-requires-type-object rule - #2961

Open
Sebastian Legarraga (slegarraga) wants to merge 1 commit into
stoplightio:developfrom
slegarraga:feat/object-with-properties-requires-type-object
Open

Sebastian Legarraga (slegarraga) wants to merge 1 commit into
stoplightio:developfrom
slegarraga:feat/object-with-properties-requires-type-object

Conversation

@slegarraga

@slegarraga Sebastian Legarraga (slegarraga) commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the rule proposed in #2203: a schema that defines properties must also declare type: object.

  • Rule id: object-with-properties-requires-type-object
  • recommended: false. It's opinionated, so it's opt-in like the other style rules.
  • resolved: false, because the type belongs on the schema node, not on $ref targets.
  • In OAS 3.1, a type array that includes object (for example [object, "null"]) passes.
  • The check uses if/then/else instead of anyOf, so a bad type is reported once instead of once per branch.
  • Documented in docs/reference/openapi-rules.md.

Why

Without type: object, JSON Schema validation doesn't restrict instances to objects, so strings and booleans pass when only properties is set.

Tests

7 cases covering OAS 2, OAS 3.0 and OAS 3.1, including type arrays with and without object.

  • yarn jest packages: 2029 passed, 1 skipped
  • Mutation check: going back to enum: ['object'], or dropping the then, the else or required, each makes at least one case fail.

Closes #2203

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

@slegarraga

This comment has been minimized.

Adds an opt-in rule for schemas that define properties without declaring
type: object. Without the type, JSON Schema does not restrict instances
to objects, so strings and other primitives pass validation.

In OAS 3.1 a type array that includes object, such as [object, 'null'],
also passes. The check uses if/then/else instead of anyOf so a bad type
is reported once rather than once per branch.

Closes stoplightio#2203
@slegarraga Sebastian Legarraga (slegarraga) changed the title feat(oas): add object-with-properties-requires-type-object rule feat(rulesets): add object-with-properties-requires-type-object rule Oct 22, 2026
@slegarraga
Sebastian Legarraga (slegarraga) force-pushed the feat/object-with-properties-requires-type-object branch from 21f68f5 to f0b8759 Compare October 22, 2026 15:56

This branch has not been deployed

No deployments
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.

Add "Objects with properties must have type: object" rule to builtin spectral:oas ruleset

1 participant