简体   繁体   English

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

[英]A strict mode warning from ajv when using electron-store

I want to save a object[] using electron-store with JSON Schema , after reading the doc of JSON Schema i get the code can be exec successfully but with warning:我想使用带有JSON Schema electron-store保存object[] ,在阅读了JSON Schema的文档后,我得到代码可以成功执行但有警告:

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

And my code is :我的代码是:

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

i add minItems and maxItems , but the warning is still appear.我添加了minItemsmaxItems ,但仍然出现警告。 I checkout it out for a few hours but can't work.我结帐了几个小时,但无法工作。 Can anyone help me?谁能帮我?

By the way, i want to ask if i use JSON Schema in right way?顺便问一下,我是否以正确的方式使用JSON Schema

You could install electron-store and exec it directly with node ./xxx.js您可以安装electron-store并直接使用node ./xxx.js执行它

Thx for helping me.感谢帮助我。

There is nothing wrong with your schema.您的架构没有任何问题。

AJV version 8 introduced "strict mode" which is on by default. AJV 版本 8 引入了默认开启的“严格模式”。 It aims to prevent making mistakes when writing schemas.它旨在防止在编写模式时出错。

One of the defaults is to prevent unconstrained items when using items in tuple form.其中默认的是使用时要防止不受约束项目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):如果“items”是一个数组(对于定义元组的模式)但既不存在“minItems”也不存在“additionalItems”/“maxItems”关键字(或具有错误的值),Ajv也会记录警告:

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

I would argue, that although you haven't set additionalItems to false, you have still constrained all values when you set maxItems .我认为,虽然您没有将additionalItems设置为 false,但在设置maxItems时您仍然限制了所有值。

I will raise an issue on your behlaf and link in the comments.我会在你的 behlaf 上提出一个问题,并在评论中提供链接。

Until it gets fixed (if it gets fixed), you can disable this element of strict mode by using a config when you initialise AJV ( https://ajv.js.org/options.html#stricttuples )在它被修复之前(如果它被修复),你可以在初始化 AJV 时通过使用配置禁用这个严格模式元素( https://ajv.js.org/options.html#stricttuples

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM