feat: add new schema assertion - #3063
Conversation
🦋 Changeset detectedLatest commit: 6a386db The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Performance Benchmark (Lower is Faster)
|
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||
… broken, add strict schema
tatomyr
left a comment
There was a problem hiding this comment.
Didn't check the code yet.
| '@redocly/cli': minor | ||
| --- | ||
|
|
||
| Added the `schema` assertion for configurable rules. |
There was a problem hiding this comment.
| Added the `schema` assertion for configurable rules. | |
| Added new `schema` assertion for configurable rules to be able ... |
?
|
|
||
| The assertion reports one problem for each violation, at the location of the value that fails. | ||
|
|
||
| The assertion keeps standard JSON Schema behavior: |
There was a problem hiding this comment.
Do we need to explain how json schema works? Maybe yes... I often see people get confused with that.
There was a problem hiding this comment.
Added for two purposes:
- User could think that if he add property to the schema in assertion it is required
- Some users miss by default
additionalProperties: true
There was a problem hiding this comment.
I will change the behavior to additionalProperties: false
|
|
||
| Two limits apply to the schema itself: | ||
|
|
||
| - A `$ref` is resolved against `redocly.yaml` before the assertion runs, not against the schema. |
There was a problem hiding this comment.
The rule could reside in a file other than redocly.yaml (e.g. could be referenced from it).
There was a problem hiding this comment.
We support external refs.
e.g.
rules:
rule/audit:
subject:
type: Info
property: x-audit
assertions:
schema:
type: object
properties:
status:
$ref: './audit-status.yaml'There was a problem hiding this comment.
against the current file
| - A `$ref` is resolved against `redocly.yaml` before the assertion runs, not against the schema. | ||
| A pointer such as `#/$defs/Status` therefore looks inside the configuration file, not inside the assertion. | ||
| - A keyword that JSON Schema does not define fails the rule, so a typo such as `tpe` instead of `type` is reported instead of being ignored. | ||
| The OpenAPI annotations `example`, `nullable`, `externalDocs`, `discriminator`, and `xml` are accepted. |
There was a problem hiding this comment.
There's no nullable keyword. Moreover, none of that keys are useful for schema validating, so I wouldn't mention them.
There was a problem hiding this comment.
I've added those keywords, because i need to have option in ajv strictSchema: true to be able to throw a typo error. But ajv with strictSchema don't support those keywords and throws an error, so i needed to extend the ajv with addKeyword.
|
|
||
| - A `$ref` is resolved against `redocly.yaml` before the assertion runs, not against the schema. | ||
| A pointer such as `#/$defs/Status` therefore looks inside the configuration file, not inside the assertion. | ||
| - A keyword that JSON Schema does not define fails the rule, so a typo such as `tpe` instead of `type` is reported instead of being ignored. |
There was a problem hiding this comment.
It's just a regular behaviour when validating Redocly config, no need to mention it specificaly in this assertion.
There was a problem hiding this comment.
No, the error goes from the ajv validator, where i put strictSchema: true.
Co-authored-by: Andrew Tatomyr <andrew.tatomyr@redocly.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 57c7c5a. Configure here.

What/Why/How?
Added new
schemaassertion to be able to force specific schema for the property.Reference
Resolves #3060
Testing
Tests, ran locally and tested in VS code extension.
Screenshots (optional)
CLI:

VS Code:


Check yourself
Security
Note
Medium Risk
Touches core lint assertion plumbing and AJV validation defaults (
allowAdditionalProperties: false); behavior changes for rules usingschemaand for how multi-location assertion failures are grouped into messages.Overview
Adds a
schemaconfigurable-rule assertion so lint rules can validate a subject property’s value against JSON Schema (2020-12) via the existingAjvValidatorpath used for OpenAPI 3.1.Rule authors can embed a full schema in
redocly.yaml(for example onx-*extensions); violations emit one diagnostic per issue at the failing path.undefinedvalues are skipped; invalid assertion schemas throw at config/use time. When the subject is a$ref, problems are reported against the resolved document location.Config typing replaces the minimal
Schemastub in the Redocly config type tree withOas3_1Types.Schema(and related nodes), so IDE/schema validation accepts real JSON Schema keywords forassertions.schema.Assertion reporting groups sub-problems by source + pointer (
groupProblemsByLocation) instead of pointer alone, so multi-file/$refcases don’t merge unrelated issues.Docs, changeset (minor for
@redocly/openapi-coreand@redocly/cli), and unit/integration tests cover happy path, additional/unevaluated properties, refs, and end-to-end lint output.Reviewed by Cursor Bugbot for commit 6a386db. Bugbot is set up for automated code reviews on this repo. Configure here.