adapt-authoring-jsonschema 1.0.0 → 1.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.
- package/lib/JsonSchema.js +9 -4
- package/lib/JsonSchemaModule.js +20 -5
- package/package.json +1 -1
package/lib/JsonSchema.js
CHANGED
|
@@ -135,7 +135,7 @@ class JsonSchema {
|
|
|
135
135
|
let parent = await this.getParent()
|
|
136
136
|
|
|
137
137
|
while (parent) {
|
|
138
|
-
const parentBuilt = _.cloneDeep((await parent.build(options)).built)
|
|
138
|
+
const parentBuilt = _.cloneDeep((await parent.build({ ...options, compile: false })).built)
|
|
139
139
|
built = await this.patch(parentBuilt, built, { strict: !parent.name === BASE_SCHEMA_NAME })
|
|
140
140
|
parent = await parent.getParent()
|
|
141
141
|
}
|
|
@@ -149,7 +149,9 @@ class JsonSchema {
|
|
|
149
149
|
}))
|
|
150
150
|
}
|
|
151
151
|
this.built = built
|
|
152
|
-
|
|
152
|
+
if(options.compile !== false) { // don't compile when option present (e.g. when running build recursively)
|
|
153
|
+
this.compiled = await this.validator.compileAsync(built)
|
|
154
|
+
}
|
|
153
155
|
this.isBuilding = false
|
|
154
156
|
this.lastBuildTime = Date.now()
|
|
155
157
|
|
|
@@ -200,8 +202,11 @@ class JsonSchema {
|
|
|
200
202
|
*/
|
|
201
203
|
validate (dataToValidate, options) {
|
|
202
204
|
const opts = _.defaults(options, { useDefaults: true, ignoreRequired: false })
|
|
203
|
-
const data = _.
|
|
204
|
-
|
|
205
|
+
const data = _.defaults(_.cloneDeep(dataToValidate), opts.useDefaults ? this.getObjectDefaults() : {})
|
|
206
|
+
if(!this.compiled) { // fallback in the case that the compiled function is missing
|
|
207
|
+
this.log('warn', 'NO_COMPILED_FUNC', this.name)
|
|
208
|
+
this.validator.compile(this.built)
|
|
209
|
+
}
|
|
205
210
|
this.compiled(data)
|
|
206
211
|
|
|
207
212
|
const errors = this.compiled.errors && this.compiled.errors
|
package/lib/JsonSchemaModule.js
CHANGED
|
@@ -67,8 +67,11 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
67
67
|
this.getConfig('xssWhitelist'))
|
|
68
68
|
})
|
|
69
69
|
.then(() => this.addStringFormats(this.getConfig('formatOverrides')))
|
|
70
|
-
.then(() => this.registerSchemas())
|
|
70
|
+
.then(() => this.registerSchemas({ quiet: true })) // note: supress logging here as other schemas will likely be added
|
|
71
71
|
.catch(e => this.log('error', e))
|
|
72
|
+
|
|
73
|
+
this.app.onReady()
|
|
74
|
+
.then(() => this.logSchemas())
|
|
72
75
|
}
|
|
73
76
|
|
|
74
77
|
/**
|
|
@@ -108,7 +111,7 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
108
111
|
* Searches all Adapt dependencies for any local JSON schemas and registers them for use in the app. Schemas must be located in in a `/schema` folder, and be named appropriately: `*.schema.json`.
|
|
109
112
|
* @return {Promise}
|
|
110
113
|
*/
|
|
111
|
-
async registerSchemas () {
|
|
114
|
+
async registerSchemas ({ quiet }) {
|
|
112
115
|
await this.resetSchemaRegistry()
|
|
113
116
|
await Promise.all(Object.values(this.app.dependencies).map(async d => {
|
|
114
117
|
if(d.name === this.name) return
|
|
@@ -117,7 +120,8 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
117
120
|
.filter(r => r.status === 'rejected')
|
|
118
121
|
.forEach(r => this.log('warn', r.reason))
|
|
119
122
|
}))
|
|
120
|
-
this.registerSchemasHook.invoke()
|
|
123
|
+
await this.registerSchemasHook.invoke()
|
|
124
|
+
if(quiet !== true) this.logSchemas()
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
/**
|
|
@@ -140,7 +144,7 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
140
144
|
this.schemaExtensions?.[schema.name]?.forEach(s => schema.addExtension(s))
|
|
141
145
|
if (schema.raw.$patch) this.extendSchema(schema.raw.$patch?.source?.$ref, schema.name)
|
|
142
146
|
|
|
143
|
-
this.log('
|
|
147
|
+
this.log('verbose', 'REGISTER_SCHEMA', schema.name, filePath)
|
|
144
148
|
}
|
|
145
149
|
|
|
146
150
|
/**
|
|
@@ -189,7 +193,7 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
189
193
|
if (!this.schemaExtensions[baseSchemaName]) this.schemaExtensions[baseSchemaName] = []
|
|
190
194
|
this.schemaExtensions[baseSchemaName].push(extSchemaName)
|
|
191
195
|
}
|
|
192
|
-
this.log('
|
|
196
|
+
this.log('verbose', 'EXTEND_SCHEMA', baseSchemaName, extSchemaName)
|
|
193
197
|
}
|
|
194
198
|
|
|
195
199
|
/**
|
|
@@ -204,6 +208,17 @@ class JsonSchemaModule extends AbstractModule {
|
|
|
204
208
|
if (!schema) throw this.app.errors.MISSING_SCHEMA.setData({ schemaName })
|
|
205
209
|
return schema.build(options)
|
|
206
210
|
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Logs all registered schemas & schema extensions
|
|
214
|
+
*/
|
|
215
|
+
logSchemas () {
|
|
216
|
+
this.log('debug', 'SCHEMAS', Object.keys(this.schemas))
|
|
217
|
+
this.log('debug', 'SCHEMA_EXTENSIONS', Object.entries(this.schemas).reduce((m, [k, v]) => {
|
|
218
|
+
if(v.extensions.length) m[k] = v.extensions
|
|
219
|
+
return m
|
|
220
|
+
}, {}))
|
|
221
|
+
}
|
|
207
222
|
}
|
|
208
223
|
|
|
209
224
|
export default JsonSchemaModule
|
package/package.json
CHANGED