使用电子商店时来自 ajv 的严格模式警告

A strict mode warning from ajv when using electron-store

我想使用 electron-storeJSON Schema 保存一个 object[],在阅读 JSON Schema 的文档后,我得到代码可以成功执行但有警告:

strict mode: "items" is 1-tuple, but minItems or maxItems/additionalItems are not specified or different at path "#/properties/todo"

我的代码是:

const Store = require('electron-store')

/** @type import('json-schema-typed').JSONSchema */
const schema = {
  todo: {
    type: 'array',
    items: [true],
    minItems: 0,
    maxItems: 999,
    additionalItems: {
      type: 'object',
      properties: {
        id: {
          type: 'number'
        },
        name: {
          type: 'string'
        }
      }
    }
  }
}

const todoStore = new Store({ schema })

const todoItem = [{ id: 1, name: '11111' }]

todoStore.set('todo', todoItem)

console.log(todoStore.get('todo'))

const newTodo = [...todoStore.get('todo')]
newTodo.push({ id: 2, name: '22222' })

todoStore.set('todo', prev)

console.log(todoStore.get('todo'))

module.exports = todoStore

我添加了minItemsmaxItems,但警告仍然出现。我检查了几个小时,但无法工作。谁能帮帮我?

顺便问一下我使用JSON Schema是否正确?

您可以安装 electron-store 并直接使用 node ./xxx.js

执行它

谢谢你帮助我。

您的架构没有问题。

AJV 版本 8 引入了默认开启的“严格模式”。它旨在防止在编写模式时出错。

默认设置之一是在以元组形式使用 items 时防止出现不受约束的项。

Ajv also logs a warning if "items" is an array (for schema that defines a tuple) but neither "minItems" nor "additionalItems"/"maxItems" keyword is present (or have a wrong value):

https://ajv.js.org/strict-mode.html#unconstrained-tuples

我会争辩说,虽然您没有将 additionalItems 设置为 false,但是当您设置 maxItems.

时,您仍然限制了所有值

我会在您的代表上提出问题,并在评论中 link。

直到它被修复(如果它被修复),你可以在初始化 AJV 时使用配置禁用严格模式的这个元素(https://ajv.js.org/options.html#stricttuples

const ajv = new Ajv({ strictTuples: false });