ajv-validator / ajv-validator/ajv

Fragments maintain cache-like behaviour, which is never cleared

Aperta
#1,293 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub
limitation
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.