ajv-validator / ajv-validator/ajv

strictTuples warning when using 2020-12 draft prefixItems with optional

Open
#2,241 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
14.8k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

**What version of Ajv are you using? Does the issue happen if you use the latest version?**
I'm using latest Ajv version as of now (8.12.0)

**Ajv options object**

```javascript
const options = { allErrors: true };
```

**JSON Schema**

```json
{
"$id": "https://www.example.com/schemas/test",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A schema for unit testing",
"type": "object",
"properties": {
"foo": {
"type": "string",
"minLength": 2
},
"bar": {
"type": "number",
"minimum": 0
},
"baz": {
"type": "array",
"items": { "type": "string" },
"minItems": 2,
"prefixItems": [{ "const": "test" }]
}
},
"required": ["foo", "baz"]
}
```

**Sample data**

```json
{
"foo": "foo",
"bar": 1,
"baz": ["test", "ok"]
}
```

**Your code**

```javascript
import Ajv from 'ajv/dist/2020';
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data); // warn: strict mode: "prefixItems" is 2-tuple, but minItems or maxItems/items are not specified or different at path "#/properties/baz"
```

**Validation result, data AFTER validation, error messages**

```javascript
{
errors: null,
schema: {
'$id': 'https://www.example.com/schemas/test',
'$schema': 'https://json-schema.org/draft/2020-12/schema',
description: 'A schema for unit testing',
type: 'object',
properties: { foo: [Object], bar: [Object], baz: [Object] },
required: [ 'foo', 'baz' ]
},
schemaEnv: SchemaEnv {
refs: {},
dynamicAnchors: {},
schema: {
'$id': 'https://www.example.com/schemas/test',
'$schema': 'https://json-schema.org/draft/2020-12/schema',
description: 'A schema for unit testing',
type: 'object',
properties: [Object],
required: [Array]
},
schemaId: '$id',
root: [Circular *1],
baseId: 'https://www.example.com/schemas/test',
schemaPath: undefined,
localRefs: {},
meta: undefined,
'$async': undefined,
validateName: ValueScopeName {
str: 'validate20',
prefix: 'validate',
value: [Object],
scopePath: [_Code]
},
validate: [Circular *2]
},
evaluated: {
props: { foo: true, bar: true, baz: true },
items: undefined,
dynamicProps: false,
dynamicItems: false
}
}
```

**What results did you expect?**
No warning. The spec has specifically [changed the way to declare tuples and arrays](https://json-schema.org/draft/2020-12/release-notes.html#changes-to-items-and-additionalitems) to avoid this sort of confusion and I'm not sure the warning is still needed

**Are you going to resolve the issue?**
You mean through an PR? I can certainly do that if that's wanted

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.