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

feat: no-scalar-refs rule #137

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions RULES.md
Expand Up @@ -17,6 +17,9 @@ License, if provided within the `info` section, must provide the `url` field.
### no-unused-schemas
Unused schemas defined in `components` may indicate a mistake. This rule checks for that scenario.

### no-scalar-ref
Scalar values should not be linked via `$ref`s.

### operation-2xx-response
When designing an API it's usually expected to do something successfully, although it might fail. So, this rule validates, that there is at least one response in the operation with a 2xx status code.

Expand Down
1 change: 1 addition & 0 deletions src/.redocly.yaml
Expand Up @@ -7,6 +7,7 @@ lint:

no-extra-fields: warning
no-$ref-siblings: warning
no-scalar-ref: warning

suggest-possible-refs: on
oas3-schema: on
Expand Down
27 changes: 27 additions & 0 deletions src/visitors/rules/semantic/no-scalar-ref.js
@@ -0,0 +1,27 @@
class NoScalarRef {
static get rule() {
return 'no-scalar-ref';
}

any() {
return {
onExit: (node, definition, ctx) => {
const errors = [];
for (const property of Object.keys(definition.properties)) {
if (definition.properties[property] === null && Object.keys(node[property] || {}).includes('$ref')) {
ctx.path.push(property);
ctx.path.push('$ref');
errors.push(ctx.createError(
'$ref reference is used for a scalar value.', 'key',
));
ctx.path.pop();
ctx.path.pop();
}
}
return errors;
},
};
}
}

module.exports = NoScalarRef;