如何使用对象数组指定eslint规则架构

huangapple go评论49阅读模式
英文:

How do I specify eslint rule schema with array of objects

问题

尝试为我的 eslint 规则设置模式。
预期的配置如下:

'my-rule': ['error', [
  {'any-key': ['array', 'of', 'strings']},
  {'some-other-key': ['array', 'of', 'strings']},
  ...等等
]]

我发现可以允许任何非枚举属性名称,但我不确定如何指定它们的值类型:

schema: [
    {
        type: 'array',
        items: {
            type: 'object',
            propertyNames: {
                type: 'string',
                // ??? - how do I specify array of strings here?
            },
        },
    },
],

这是否可能?

英文:

Trying to set a schema for my eslint rule.
The intended configuration is as follows:

'my-rule': ['error', [
  {'any-key': ['array', 'of', 'strings']},
  {'some-other-key': ['array', 'of', 'strings']},
  ...etc
]]

I found out that it is possible to allow any non-enum property names, but I'm not sure how to specify their value types:

    schema: [
        {
            type: 'array',
            items: {
                type: 'object',
                propertyNames: {
                    type: 'string',
                    // ??? - how do I specify array of strings here?
                },
            }
        }
    ],

Is this possible at all?

答案1

得分: 2

I don't think you can use propertyNames to specify the value types.

Wouldn't patternProperties work better for your purpose?

schema: [
    {
        type: 'array',
        items: {
            type: 'object',
            patternProperties: {
                ".": {
                    type: "array",
                    items: {type: "string"}
                }
            },
        }
    }
]
英文:

I don't think you can use propertyNames to specify the value types.

Wouldn't patternProperties work better for your purpose?

schema: [
    {
        type: 'array',
        items: {
            type: 'object',
            patternProperties: {
                ".": {
                    type: "array",
                    items: {type: "string"}
                }
            },
        }
    }
]

huangapple
  • 本文由 发表于 2023年2月19日 07:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定