ajv-validator / ajv-validator/ajv

Fragments maintain cache-like behaviour, which is never cleared

未关闭
#1,293 2 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
limitation
主要语言
TypeScript
星标
14.8k
派生
1k
PR 合并指标
30 天内没有已合并 PR

描述

**What version of Ajv are you using? Does the issue happen if you use the latest version?**
6.12.4

**Ajv options object**

```javascript
{useDefaults: true}
```

**Your code**

```javascript
test.only('Should allow removing and adding "nested" schemas', t => {

const SOME_TYPE = 'SomeType'
const SOME_STRING = 'some string'
const SOME_NUMBER = 5

const ajv = aJV({useDefaults: true})

// Add initial schema (supports a string)
ajv.addSchema({[SOME_TYPE]: STRING_SCHEMA}, 'data')
const validate = ajv.getSchema(`data#/${SOME_TYPE}`)
t.deepEqual(validate(SOME_STRING), true) // Works of course

// Replace schema with new one (supports a number)
ajv.removeSchema('data')
ajv.addSchema({[SOME_TYPE]: NUMBER_SCHEMA}, 'data')

// Returns a cached validator due to fragments
const validate2 = ajv.getSchema(`data#/${SOME_TYPE}`)
t.deepEqual(validate2(SOME_NUMBER), true) // Breaks - expects a string
t.deepEqual(validate2(SOME_STRING), false)
})

```

**Why does this happen ?
In some cases a fragments object is populated -
https://github.com/ajv-validator/ajv/blob/master/lib/ajv.js#L218

This object is not cleared when calling `removeSchema` (unlike `_refs` & `_schemas `)
https://github.com/ajv-validator/ajv/blob/master/lib/ajv.js#L262

**What results did you expect?**
- Removing and adding a schema under a certain namespace should be supported, and validation should run for updated schema.

**Are you going to resolve the issue?**

- This can be resolved by clearing all keys matching schema key provided in removeSchema.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。