Skip to content

Commit

Permalink
Add node-param-default-missing exception for modes (ivov#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Sep 14, 2022
1 parent 51e6795 commit 80ddf07
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ In the `community` ruleset, the five `*-still-default` rules allow you to define
| [node-param-collection-type-item-required](docs/rules/node-param-collection-type-item-required.md) | Items in collection-type node parameter must not have a `required` property. | Yes |
| [node-param-collection-type-unsorted-items](docs/rules/node-param-collection-type-unsorted-items.md) | Items in collection-type node parameter must be alphabetized by `name` if five or more than five. | Yes |
| [node-param-color-type-unused](docs/rules/node-param-color-type-unused.md) | `string`-type color-related node parameter must be `color`-type. | Yes |
| [node-param-default-missing](docs/rules/node-param-default-missing.md) | `default` must be present in a node parameter. | Yes |
| [node-param-default-missing](docs/rules/node-param-default-missing.md) | `default` must be present in a node parameter, except in node parameters under `modes`. | Yes |
| [node-param-default-wrong-for-boolean](docs/rules/node-param-default-wrong-for-boolean.md) | `default` for boolean-type node parameter must be a boolean. | Yes |
| [node-param-default-wrong-for-collection](docs/rules/node-param-default-wrong-for-collection.md) | `default` for collection-type node parameter must be an object. | Yes |
| [node-param-default-wrong-for-fixed-collection](docs/rules/node-param-default-wrong-for-fixed-collection.md) | `default` for fixed-collection-type node parameter must be an object. | Yes |
Expand Down
71 changes: 70 additions & 1 deletion docs/rules/node-param-default-missing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# node-param-default-missing

`default` must be present in a node parameter.
`default` must be present in a node parameter, except in node parameters under `modes`.

📋 This rule is part of the `plugin:n8n-nodes-base/nodes` config.

Expand Down Expand Up @@ -76,6 +76,75 @@ const test = {
type: "number",
default: -1,
};

const test = {
displayName: "Board",
name: "boardIdRLC",
type: "resourceLocator",
default: { mode: "list", value: "" },
required: true,
displayOptions: {
show: {
operation: ["get", "delete", "update"],
resource: ["board"],
"@version": [2],
},
},
description: "The ID of the board",
modes: [
{
displayName: "From List",
name: "list",
type: "list",
hint: "Select a board from the list",
placeholder: "Choose...",
initType: "board",
typeOptions: {
searchListMethod: "searchBoards",
searchFilterRequired: true,
searchable: true,
},
},
{
displayName: "ID",
name: "id",
type: "string",
hint: "Enter Board Id",
validation: [
{
type: "regex",
properties: {
regex: "[a-zA-Z0-9]+",
errorMessage: "ID value cannot be empty",
},
},
],
placeholder: "KdEAAdde",
url: "=https://trello.com/b/{{$value}}",
},
{
displayName: "By URL",
name: "url",
type: "string",
hint: "Enter board URL",
placeholder: "https://trello.com/b/e123456/board-name",
validation: [
{
type: "regex",
properties: {
regex: "http(s)?://trello.com/b/([a-zA-Z0-9]+)/[a-zA-Z0-9]+",
errorMessage:
"URL has to be in the format: http(s)://trello.com/b/<board ID>/<board name>",
},
},
],
extractValue: {
type: "regex",
regex: "https://trello.com/b/([a-zA-Z0-9]+)",
},
},
],
};
```

## Links
Expand Down
14 changes: 12 additions & 2 deletions lib/rules/node-param-default-missing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESTree } from "@typescript-eslint/utils";
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
Expand All @@ -8,7 +8,7 @@ export default utils.createRule({
meta: {
type: "problem",
docs: {
description: "`default` must be present in a node parameter.",
description: "`default` must be present in a node parameter, except in node parameters under `modes`.",
recommended: "error",
},
fixable: "code",
Expand All @@ -23,6 +23,16 @@ export default utils.createRule({
ObjectExpression(node) {
if (!id.isNodeParameter(node, { skipKeys: ["default"] })) return;

if (node.parent?.parent) {
if (
node.parent.parent.type === AST_NODE_TYPES.Property &&
node.parent.parent.key.type === AST_NODE_TYPES.Identifier &&
node.parent.parent.key.name === "modes"
) {
return;
}
}

const type = getters.nodeParam.getType(node); // insertion point

if (!type) return;
Expand Down
71 changes: 71 additions & 0 deletions tests/node-param-default-missing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,77 @@ ruleTester().run(getRuleName(module), rule, {
default: -1,
};`,
},
{
code: outdent`
const test = {
displayName: 'Board',
name: 'boardIdRLC',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
required: true,
displayOptions: {
show: {
operation: ['get', 'delete', 'update'],
resource: ['board'],
'@version': [2],
},
},
description: 'The ID of the board',
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
hint: 'Select a board from the list',
placeholder: 'Choose...',
initType: 'board',
typeOptions: {
searchListMethod: 'searchBoards',
searchFilterRequired: true,
searchable: true,
},
},
{
displayName: 'ID',
name: 'id',
type: 'string',
hint: 'Enter Board Id',
validation: [
{
type: 'regex',
properties: {
regex: '[a-zA-Z0-9]+',
errorMessage: 'ID value cannot be empty',
},
},
],
placeholder: 'KdEAAdde',
url: '=https://trello.com/b/{{$value}}',
},
{
displayName: 'By URL',
name: 'url',
type: 'string',
hint: 'Enter board URL',
placeholder: 'https://trello.com/b/e123456/board-name',
validation: [
{
type: 'regex',
properties: {
regex: 'http(s)?://trello.com/b/([a-zA-Z0-9]+)/[a-zA-Z0-9]+',
errorMessage:
'URL has to be in the format: http(s)://trello.com/b/<board ID>/<board name>',
},
},
],
extractValue: {
type: 'regex',
regex: 'https://trello.com/b/([a-zA-Z0-9]+)',
},
},
],
};`,
}
],
invalid: [
{
Expand Down

0 comments on commit 80ddf07

Please sign in to comment.