Skip to content

Commit

Permalink
check the type
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jan 23, 2025
1 parent de89966 commit 110aa81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions eslint/src/rules/hasType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRule } from 'eslint-plugin-yml/lib/utils';

import { isPairWithKey } from '../utils.js';
import { isPairWithKey, isPairWithValue } from '../utils.js';

export const hasType = createRule('hasType', {
meta: {
Expand All @@ -27,15 +27,15 @@ export const hasType = createRule('hasType', {
return; // allow everything in properties
}

const hasType = !!node.parent.pairs.find((pair) => isPairWithKey(pair, 'type'));
if (isPairWithKey(node, 'properties') && !hasType) {
const type = node.parent.pairs.find((pair) => isPairWithKey(pair, 'type'));
if (isPairWithKey(node, 'properties') && (!type || !isPairWithValue(type, 'object'))) {
return context.report({
node: node as any,
messageId: 'hasType',
});
}

if (isPairWithKey(node, 'items') && !hasType) {
if (isPairWithKey(node, 'items') && (!type || !isPairWithValue(type, 'array'))) {
return context.report({
node: node as any,
messageId: 'hasType',
Expand Down
7 changes: 7 additions & 0 deletions eslint/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export function isPairWithKey(node: AST.YAMLNode | null, key: string): node is A
return isScalar(node.key) && node.key.value === key;
}

export function isPairWithValue(node: AST.YAMLNode | null, value: string): node is AST.YAMLPair {
if (node === null || node.type !== 'YAMLPair' || node.value === null) {
return false;
}
return isScalar(node.value) && node.value.value === value;
}

export function isNullable(node: AST.YAMLNode | null): boolean {
return (
isSequence(node) &&
Expand Down
10 changes: 10 additions & 0 deletions eslint/tests/hasType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ simple:
},
{
code: `
wrongType:
type: string
properties:
noType:
type: string
`,
errors: [{ messageId: 'hasType' }],
},
{
code: `
array:
items:
type: string
Expand Down

0 comments on commit 110aa81

Please sign in to comment.