ajv@7.2.4 - Error: strict mode: unknown keyword: "unevaluatedProperties" -

ajv@7.2.4 - Error: strict mode: unknown keyword: "unevaluatedProperties" -

我希望在 ajv 版本 7.2.4 中定义和工作 ajv 关键字 a unevaluatedProperties,但是我在标题中收到错误消息。

这里是 npm 版本列表:

$ npm list ajv
tscapi@0.0.1 /mnt/common/github/tscapi
├── ajv@7.2.4 
└─┬ eslint@7.24.0
  ├─┬ @eslint/eslintrc@0.4.0
  │ └── ajv@6.12.6 
  ├── ajv@6.12.6 
  └─┬ table@6.2.0
    └── ajv@8.1.0 

这是给出错误的代码:

import * as ajv from 'ajv';

const schemas = [
  {
    $id: 'schemas.json#/1',
    anyOf: [{$ref: 'schemas.json#/2'}, {$ref: 'schemas.json#/3'}],
  },
  {
    $id: 'schemas.json#/2',
    type: 'object',
    properties: {
      a: {type: 'string'},
    },
    required: ['a'],
    unevaluatedProperties: true,
  },
  {
    $id: 'schemas.json#/3',
    type: 'object',
    properties: {
      b: {type: 'string'},
    },
    required: ['b'],
    unevaluatedProperties: true,
  },
  {
    $id: 'schemas.json#/4',
    type: 'object',
    allOf: [{$ref: 'schemas.json#/2'}, {$ref: 'schemas.json#/3'}],
    unevaluatedProperties: false,
  },
];

const validators = new ajv.default({schemas: schemas});
{
  const validate = validators.getSchema('schemas.json#/1')!;
  validate({a: 'hello'});
  if (validate.errors) throw new Error(validate.errors[0].message);
  console.log('ok 1');
}
{
  const validate = validators.getSchema('schemas.json#/4')!;
  validate({a: 'hello', b: 'hello'});
  if (validate.errors) throw new Error(validate.errors[0].message);
  console.log('ok 2');
}

在文件node_modules/ajv/README.md中发现了如下文字,说明unevaluatedProperties在这个版本的ajv中应该是未知的。

## Using version 7

Ajv version 7 has these new features:
...
- support of JSON Schema draft-2019-09 features: [`unevaluatedProperties`](./docs/json-schema.md#unevaluatedproperties) and [`unevaluatedItems`](./docs/json-schema.md#unevaluateditems), [dynamic recursive references](./docs/guide/combining-schemas.md#extending-recursive-schemas) and other [additional keywords](./docs/json-schema.md#json-schema-draft-2019-09).
...

我也试过更改代码中的一行

const validators = new ajv.default({schemas: schemas});

const opts = {next:true,unevaluated:true}
const validators = new ajv.default(opts).addSchema(schemas);

但还是没有运气。

我需要做什么才能启用 unevaluatedProperties

它需要通过

显式加载更新的规范
import Ajv2019 from "ajv/dist/2019"
const ajv = new Ajv2019()

如以下任一手册页所述: