adapt-schemas 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/Schemas.js +20 -0
  2. package/package.json +1 -1
package/lib/Schemas.js CHANGED
@@ -110,6 +110,12 @@ class Schemas extends EventEmitter {
110
110
  * Empties the schema registry (with the exception of the base schema)
111
111
  */
112
112
  resetSchemaRegistry () {
113
+ // Remove previously registered schemas from AJV instances
114
+ for (const name of Object.keys(this.schemas)) {
115
+ for (const v of [this.validator, this.validatorWithDefaults]) {
116
+ if (v.getSchema(name)) v.removeSchema(name)
117
+ }
118
+ }
113
119
  this.schemas = {
114
120
  base: this.createSchema(path.resolve(__dirname, BASE_SCHEMA_PATH), { enableCache: true })
115
121
  }
@@ -224,6 +230,16 @@ class Schemas extends EventEmitter {
224
230
  }
225
231
 
226
232
  this.schemas[schema.name] = schema
233
+
234
+ // AJV's sync compile() can only resolve $refs against schemas in its internal store to allow `$ref: "schemaName`"
235
+ // Note we only add simple schemas (no $merge/$patch)
236
+ if (!schema.raw.$merge && !schema.raw.$patch) {
237
+ for (const v of [this.validator, this.validatorWithDefaults]) {
238
+ if (v.getSchema(schema.name)) v.removeSchema(schema.name)
239
+ v.addSchema(schema.raw, schema.name)
240
+ }
241
+ }
242
+
227
243
  this.schemaExtensions?.[schema.name]?.forEach(s => schema.addExtension(s))
228
244
 
229
245
  if (schema.raw.$patch) {
@@ -242,6 +258,10 @@ class Schemas extends EventEmitter {
242
258
  if (this.schemas[name]) {
243
259
  delete this.schemas[name]
244
260
  }
261
+ // Remove from AJV instances
262
+ for (const v of [this.validator, this.validatorWithDefaults]) {
263
+ if (v.getSchema(name)) v.removeSchema(name)
264
+ }
245
265
  // Remove schema from any extensions lists
246
266
  Object.entries(this.schemaExtensions).forEach(([base, extensions]) => {
247
267
  this.schemaExtensions[base] = extensions.filter(s => s !== name)
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/cgkineo/adapt-schemas"
6
6
  },
7
- "version": "3.1.0",
7
+ "version": "3.1.1",
8
8
  "description": "Standalone JSON Schema library for the Adapt framework",
9
9
  "type": "module",
10
10
  "main": "index.js",