I expect the ajv keyword a unevaluatedProperties
to be defined and working in ajv version 7.2.4, however I get the error message in the title.
Here is the npm version listing:
$ npm list ajv
[email protected] /mnt/common/github/tscapi
├── [email protected]
└─┬ [email protected]
├─┬ @eslint/[email protected]
│ └── [email protected]
├── [email protected]
└─┬ [email protected]
└── [email protected]
Here is the code that gives the error:
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');
}
In the file node_modules/ajv/README.md
the following text is found, which shows that unevaluatedProperties
should not be unknown in this version of 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).
...
I've also tried changing one line in the code
const validators = new ajv.default({schemas: schemas});
to
const opts = {next:true,unevaluated:true}
const validators = new ajv.default(opts).addSchema(schemas);
but still no luck.
What do I need to do to enable unevaluatedProperties
?
It requires explicitly loading the newer specs via
import Ajv2019 from "ajv/dist/2019"
const ajv = new Ajv2019()
as stated on either of these man pages: