5

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:

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. 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?

You could install electron-store and exec it directly with node ./xxx.js

Thx for helping me.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
GuangWu
  • 378
  • 4
  • 14

1 Answers1

4

There is nothing wrong with your schema.

AJV version 8 introduced "strict mode" which is on by default. It aims to prevent making mistakes when writing schemas.

One of the defaults is to prevent unconstrained items when using items in tuple form.

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

I would argue, that although you haven't set additionalItems to false, you have still constrained all values when you set maxItems.

I will raise an issue on your behlaf and link in the comments.

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)

const ajv = new Ajv({ strictTuples: false });
Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Thank you so much for the anwser, It works when i added `strictTuples: false`. But i must add this in `node_modules/conf/dist/source/index.js` and find no APIs or entries to config ajv with `electron-store`. Is there any other solutions or ways to saving a `object[]` structrue? – GuangWu Jul 21 '21 at 13:27
  • Ah. Well. If you want to be a good citizen, you have a few jumps to make here. It doesn't look like the `conf` module gives you access to modify ajv config. You would need to file an issue there, then get that patched and pushed downstream. Luckily, it's the same maintainer. If it helps, I'm happy to comment on any issues you raise – Relequestual Jul 21 '21 at 14:08
  • The bug should get fixed, but it's unliklely you'll get the option to pass through config to AJV form electron-store. It has been suggested you could use https://www.npmjs.com/package/patch-package, but I don't know how I feel about that solution. – Relequestual Jul 22 '21 at 11:58
  • Well, that's too much trouble, i've decide to remove schema and restrict data structure in renderer process. – GuangWu Jul 23 '21 at 08:21