gpt4 book ai didi

typescript - ajv@7.2.4 - 错误 : strict mode: unknown keyword: "unevaluatedProperties" -

转载 作者:行者123 更新时间:2023-12-05 03:42:40 26 4
gpt4 key购买 nike

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

这是 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()

如这些手册页中的任一个所述:

关于typescript - ajv@7.2.4 - 错误 : strict mode: unknown keyword: "unevaluatedProperties" -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67156641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com