@jsreport/jsreport-core 3.1.2-test.2 → 3.2.0
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/LICENSE +166 -166
- package/README.md +298 -298
- package/index.js +29 -29
- package/lib/main/blobStorage/blobStorage.js +52 -52
- package/lib/main/blobStorage/inMemoryProvider.js +27 -27
- package/lib/main/blobStorage/mainActions.js +24 -24
- package/lib/main/createDefaultLoggerFormat.js +17 -17
- package/lib/main/defaults.js +14 -14
- package/lib/main/extensions/discover.js +20 -20
- package/lib/main/extensions/extensionsManager.js +264 -264
- package/lib/main/extensions/fileUtils.js +56 -56
- package/lib/main/extensions/findVersion.js +49 -49
- package/lib/main/extensions/locationCache.js +103 -103
- package/lib/main/extensions/sorter.js +10 -10
- package/lib/main/extensions/validateMinimalVersion.js +50 -50
- package/lib/main/folders/cascadeFolderRemove.js +25 -25
- package/lib/main/folders/getEntitiesInFolder.js +53 -53
- package/lib/main/folders/index.js +42 -42
- package/lib/main/folders/moveBetweenFolders.js +354 -354
- package/lib/main/folders/validateDuplicatedName.js +107 -107
- package/lib/main/folders/validateReservedName.js +53 -53
- package/lib/main/logger.js +244 -244
- package/lib/main/migration/resourcesToAssets.js +230 -230
- package/lib/main/migration/xlsxTemplatesToAssets.js +128 -128
- package/lib/main/monitoring.js +91 -91
- package/lib/main/optionsLoad.js +237 -237
- package/lib/main/optionsSchema.js +237 -237
- package/lib/main/reporter.js +574 -579
- package/lib/main/schemaValidator.js +252 -252
- package/lib/main/settings.js +154 -154
- package/lib/main/store/checkDuplicatedId.js +27 -27
- package/lib/main/store/collection.js +329 -329
- package/lib/main/store/documentStore.js +469 -469
- package/lib/main/store/mainActions.js +28 -28
- package/lib/main/store/memoryStoreProvider.js +99 -99
- package/lib/main/store/queue.js +48 -48
- package/lib/main/store/referenceUtils.js +251 -251
- package/lib/main/store/setupValidateId.js +43 -43
- package/lib/main/store/setupValidateShortid.js +71 -71
- package/lib/main/store/transaction.js +69 -69
- package/lib/main/store/typeUtils.js +180 -180
- package/lib/main/templates.js +34 -34
- package/lib/main/validateEntityName.js +62 -62
- package/lib/shared/createError.js +36 -36
- package/lib/shared/encryption.js +114 -114
- package/lib/shared/folders/index.js +11 -11
- package/lib/shared/folders/normalizeEntityPath.js +15 -15
- package/lib/shared/folders/resolveEntityFromPath.js +88 -88
- package/lib/shared/folders/resolveEntityPath.js +46 -46
- package/lib/shared/folders/resolveFolderFromPath.js +38 -38
- package/lib/shared/generateRequestId.js +4 -4
- package/lib/shared/listenerCollection.js +169 -169
- package/lib/shared/normalizeMetaFromLogs.js +30 -30
- package/lib/shared/reporter.js +128 -123
- package/lib/shared/request.js +64 -64
- package/lib/shared/tempFilesHandler.js +81 -81
- package/lib/shared/templates.js +82 -82
- package/lib/static/helpers.js +33 -33
- package/lib/worker/blobStorage.js +34 -34
- package/lib/worker/defaultProxyExtend.js +46 -46
- package/lib/worker/documentStore.js +49 -49
- package/lib/worker/extensionsManager.js +17 -17
- package/lib/worker/logger.js +48 -48
- package/lib/worker/render/diff.js +138 -138
- package/lib/worker/render/executeEngine.js +239 -207
- package/lib/worker/render/htmlRecipe.js +10 -10
- package/lib/worker/render/moduleHelper.js +45 -43
- package/lib/worker/render/noneEngine.js +12 -12
- package/lib/worker/render/profiler.js +158 -158
- package/lib/worker/render/render.js +202 -205
- package/lib/worker/render/resolveReferences.js +60 -60
- package/lib/worker/reporter.js +192 -191
- package/lib/worker/sandbox/runInSandbox.js +16 -9
- package/lib/worker/sandbox/safeSandbox.js +828 -828
- package/lib/worker/templates.js +80 -78
- package/lib/worker/workerHandler.js +54 -54
- package/package.json +92 -92
- package/test/blobStorage/common.js +21 -21
- package/test/store/common.js +1449 -1449
|
@@ -1,252 +1,252 @@
|
|
|
1
|
-
const extend = require('node.extend.without.arrays')
|
|
2
|
-
const set = require('lodash.set')
|
|
3
|
-
const hasOwn = require('has-own-deep')
|
|
4
|
-
const unsetValue = require('unset-value')
|
|
5
|
-
const ms = require('ms')
|
|
6
|
-
const Ajv = require('ajv')
|
|
7
|
-
|
|
8
|
-
const validatorCollection = new WeakMap()
|
|
9
|
-
|
|
10
|
-
class SchemaValidator {
|
|
11
|
-
constructor (_options = {}) {
|
|
12
|
-
const options = Object.assign({
|
|
13
|
-
rootSchema: null
|
|
14
|
-
}, _options)
|
|
15
|
-
|
|
16
|
-
this.schemaVersion = 'http://json-schema.org/draft-07/schema#'
|
|
17
|
-
|
|
18
|
-
const validator = new Ajv({
|
|
19
|
-
useDefaults: true,
|
|
20
|
-
coerceTypes: true,
|
|
21
|
-
format: 'full'
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const stringDateFormatValidate = validator.compile({ type: 'string', format: 'date-time' })
|
|
25
|
-
|
|
26
|
-
validator.addKeyword('$jsreport-constantOrArray', {
|
|
27
|
-
type: 'string',
|
|
28
|
-
modifying: true,
|
|
29
|
-
compile: (sch) => {
|
|
30
|
-
if (!Array.isArray(sch)) {
|
|
31
|
-
throw new Error('$jsreport-constantOrArray json schema keyword should contain an array')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return (data, dataPath, parentData, parentDataProperty) => {
|
|
35
|
-
if (sch.indexOf(data) !== -1) {
|
|
36
|
-
parentData[parentDataProperty] = data
|
|
37
|
-
return true
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
parentData[parentDataProperty] = data.split(',')
|
|
41
|
-
return true
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
validator.addKeyword('$jsreport-stringToDate', {
|
|
47
|
-
modifying: true,
|
|
48
|
-
compile: (sch) => {
|
|
49
|
-
if (sch !== true) {
|
|
50
|
-
return () => true
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return (data, dataPath, parentData, parentDataProperty) => {
|
|
54
|
-
if (!stringDateFormatValidate(data)) {
|
|
55
|
-
return false
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const newData = new Date(data)
|
|
59
|
-
|
|
60
|
-
// tests that date instance is not invalid
|
|
61
|
-
if (Object.prototype.toString.call(newData) !== '[object Date]') {
|
|
62
|
-
return false
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
parentData[parentDataProperty] = newData
|
|
66
|
-
|
|
67
|
-
return true
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
validator.addKeyword('$jsreport-acceptsDate', {
|
|
73
|
-
compile: (sch) => {
|
|
74
|
-
if (sch !== true) {
|
|
75
|
-
return () => true
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return (data) => {
|
|
79
|
-
return Boolean(
|
|
80
|
-
data &&
|
|
81
|
-
Object.prototype.toString.call(data) === '[object Date]' &&
|
|
82
|
-
!isNaN(data)
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
validator.addKeyword('$jsreport-acceptsBuffer', {
|
|
89
|
-
compile: (sch) => {
|
|
90
|
-
if (sch !== true) {
|
|
91
|
-
return () => true
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return (data) => {
|
|
95
|
-
return Buffer.isBuffer(data)
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
validator.addKeyword('$jsreport-acceptsDuration', {
|
|
101
|
-
modifying: true,
|
|
102
|
-
compile: (sch) => {
|
|
103
|
-
if (sch !== true) {
|
|
104
|
-
return () => true
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return (data, dataPath, parentData, parentDataProperty) => {
|
|
108
|
-
if (typeof data !== 'string' && typeof data !== 'number') {
|
|
109
|
-
return false
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (typeof data === 'number') {
|
|
113
|
-
return true
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const newData = ms(data)
|
|
117
|
-
|
|
118
|
-
if (newData == null) {
|
|
119
|
-
return false
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
parentData[parentDataProperty] = newData
|
|
123
|
-
|
|
124
|
-
return true
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
let rootValidate
|
|
130
|
-
|
|
131
|
-
if (options.rootSchema != null) {
|
|
132
|
-
rootValidate = validator.compile(addDefaultsAndValidateRootSchema(
|
|
133
|
-
validator,
|
|
134
|
-
options.rootSchema,
|
|
135
|
-
this.schemaVersion
|
|
136
|
-
))
|
|
137
|
-
|
|
138
|
-
this.setRootSchema = (schema) => {
|
|
139
|
-
rootValidate = validator.compile(addDefaultsAndValidateRootSchema(
|
|
140
|
-
validator,
|
|
141
|
-
schema,
|
|
142
|
-
this.schemaVersion
|
|
143
|
-
))
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
this.getRootSchema = () => rootValidate && rootValidate.schema
|
|
147
|
-
|
|
148
|
-
this.validateRoot = (data, { rootPrefix = '', ignore = [] } = {}) => {
|
|
149
|
-
let validateFn
|
|
150
|
-
|
|
151
|
-
if (ignore.length > 0) {
|
|
152
|
-
const newRootSchema = extend(true, {}, this.getRootSchema())
|
|
153
|
-
|
|
154
|
-
ignore.forEach((prop) => {
|
|
155
|
-
if (hasOwn(newRootSchema, prop)) {
|
|
156
|
-
set(newRootSchema, prop, true)
|
|
157
|
-
unsetValue(newRootSchema, prop)
|
|
158
|
-
}
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
validateFn = validator.compile(newRootSchema)
|
|
162
|
-
} else {
|
|
163
|
-
validateFn = rootValidate
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return runSchemaValidation(validator, validateFn, data, rootPrefix)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
validatorCollection.set(this, validator)
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
addSchema (name, _schema, replace = false) {
|
|
174
|
-
const validator = validatorCollection.get(this)
|
|
175
|
-
let schema = _schema
|
|
176
|
-
|
|
177
|
-
if (typeof schema === 'object' && !Array.isArray(schema) && schema.$schema == null) {
|
|
178
|
-
schema = Object.assign({}, schema, { $schema: this.schemaVersion })
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (replace === true && validator.getSchema(name) != null) {
|
|
182
|
-
validator.removeSchema(name)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
validator.addSchema(schema, name)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// after validate, data will be coerced (modified) with value types
|
|
189
|
-
// that match the schema
|
|
190
|
-
validate (name, data, { rootPrefix = '' } = {}) {
|
|
191
|
-
const validator = validatorCollection.get(this)
|
|
192
|
-
const schemaValidate = validator.getSchema(name)
|
|
193
|
-
|
|
194
|
-
if (schemaValidate == null) {
|
|
195
|
-
throw new Error(`schema ${name} is not registered in validator`)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return runSchemaValidation(validator, schemaValidate, data, rootPrefix)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
getSchema (name) {
|
|
202
|
-
const validator = validatorCollection.get(this)
|
|
203
|
-
const schemaValidate = validator.getSchema(name)
|
|
204
|
-
return schemaValidate ? schemaValidate.schema : undefined
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function addDefaultsAndValidateRootSchema (validator, _rootSchema, schemaVersion) {
|
|
209
|
-
let rootSchema = _rootSchema
|
|
210
|
-
|
|
211
|
-
if (
|
|
212
|
-
typeof schema === 'object' &&
|
|
213
|
-
!Array.isArray(rootSchema) &&
|
|
214
|
-
rootSchema.$schema == null
|
|
215
|
-
) {
|
|
216
|
-
rootSchema = Object.assign({}, rootSchema, { $schema: schemaVersion })
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const schemaValid = validator.validateSchema(rootSchema)
|
|
220
|
-
|
|
221
|
-
if (!schemaValid) {
|
|
222
|
-
throw new Error(`root schema is not valid. errors: ${
|
|
223
|
-
validator.errorsText(validator.errors, { dataVar: 'rootSchema' })
|
|
224
|
-
}`)
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return rootSchema
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function runSchemaValidation (validator, schemaValidate, data, rootPrefix) {
|
|
231
|
-
const valid = schemaValidate(data)
|
|
232
|
-
const result = { valid }
|
|
233
|
-
|
|
234
|
-
if (!valid) {
|
|
235
|
-
result.errors = schemaValidate.errors.map((err) => {
|
|
236
|
-
if (err.keyword === 'enum' && err.params && Array.isArray(err.params.allowedValues)) {
|
|
237
|
-
// ajv errorsText does not put allowed values in message, we need to add it manually
|
|
238
|
-
err.message += `. allowed values: [${err.params.allowedValues.map(v => JSON.stringify(v)).join(', ')}]`
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return err
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
result.fullErrorMessage = `schema validation errors: ${
|
|
245
|
-
validator.errorsText(result.errors, { dataVar: rootPrefix })
|
|
246
|
-
}`
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return result
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
module.exports = SchemaValidator
|
|
1
|
+
const extend = require('node.extend.without.arrays')
|
|
2
|
+
const set = require('lodash.set')
|
|
3
|
+
const hasOwn = require('has-own-deep')
|
|
4
|
+
const unsetValue = require('unset-value')
|
|
5
|
+
const ms = require('ms')
|
|
6
|
+
const Ajv = require('ajv')
|
|
7
|
+
|
|
8
|
+
const validatorCollection = new WeakMap()
|
|
9
|
+
|
|
10
|
+
class SchemaValidator {
|
|
11
|
+
constructor (_options = {}) {
|
|
12
|
+
const options = Object.assign({
|
|
13
|
+
rootSchema: null
|
|
14
|
+
}, _options)
|
|
15
|
+
|
|
16
|
+
this.schemaVersion = 'http://json-schema.org/draft-07/schema#'
|
|
17
|
+
|
|
18
|
+
const validator = new Ajv({
|
|
19
|
+
useDefaults: true,
|
|
20
|
+
coerceTypes: true,
|
|
21
|
+
format: 'full'
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const stringDateFormatValidate = validator.compile({ type: 'string', format: 'date-time' })
|
|
25
|
+
|
|
26
|
+
validator.addKeyword('$jsreport-constantOrArray', {
|
|
27
|
+
type: 'string',
|
|
28
|
+
modifying: true,
|
|
29
|
+
compile: (sch) => {
|
|
30
|
+
if (!Array.isArray(sch)) {
|
|
31
|
+
throw new Error('$jsreport-constantOrArray json schema keyword should contain an array')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (data, dataPath, parentData, parentDataProperty) => {
|
|
35
|
+
if (sch.indexOf(data) !== -1) {
|
|
36
|
+
parentData[parentDataProperty] = data
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
parentData[parentDataProperty] = data.split(',')
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
validator.addKeyword('$jsreport-stringToDate', {
|
|
47
|
+
modifying: true,
|
|
48
|
+
compile: (sch) => {
|
|
49
|
+
if (sch !== true) {
|
|
50
|
+
return () => true
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (data, dataPath, parentData, parentDataProperty) => {
|
|
54
|
+
if (!stringDateFormatValidate(data)) {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const newData = new Date(data)
|
|
59
|
+
|
|
60
|
+
// tests that date instance is not invalid
|
|
61
|
+
if (Object.prototype.toString.call(newData) !== '[object Date]') {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
parentData[parentDataProperty] = newData
|
|
66
|
+
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
validator.addKeyword('$jsreport-acceptsDate', {
|
|
73
|
+
compile: (sch) => {
|
|
74
|
+
if (sch !== true) {
|
|
75
|
+
return () => true
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (data) => {
|
|
79
|
+
return Boolean(
|
|
80
|
+
data &&
|
|
81
|
+
Object.prototype.toString.call(data) === '[object Date]' &&
|
|
82
|
+
!isNaN(data)
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
validator.addKeyword('$jsreport-acceptsBuffer', {
|
|
89
|
+
compile: (sch) => {
|
|
90
|
+
if (sch !== true) {
|
|
91
|
+
return () => true
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (data) => {
|
|
95
|
+
return Buffer.isBuffer(data)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
validator.addKeyword('$jsreport-acceptsDuration', {
|
|
101
|
+
modifying: true,
|
|
102
|
+
compile: (sch) => {
|
|
103
|
+
if (sch !== true) {
|
|
104
|
+
return () => true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return (data, dataPath, parentData, parentDataProperty) => {
|
|
108
|
+
if (typeof data !== 'string' && typeof data !== 'number') {
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (typeof data === 'number') {
|
|
113
|
+
return true
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const newData = ms(data)
|
|
117
|
+
|
|
118
|
+
if (newData == null) {
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
parentData[parentDataProperty] = newData
|
|
123
|
+
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
let rootValidate
|
|
130
|
+
|
|
131
|
+
if (options.rootSchema != null) {
|
|
132
|
+
rootValidate = validator.compile(addDefaultsAndValidateRootSchema(
|
|
133
|
+
validator,
|
|
134
|
+
options.rootSchema,
|
|
135
|
+
this.schemaVersion
|
|
136
|
+
))
|
|
137
|
+
|
|
138
|
+
this.setRootSchema = (schema) => {
|
|
139
|
+
rootValidate = validator.compile(addDefaultsAndValidateRootSchema(
|
|
140
|
+
validator,
|
|
141
|
+
schema,
|
|
142
|
+
this.schemaVersion
|
|
143
|
+
))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.getRootSchema = () => rootValidate && rootValidate.schema
|
|
147
|
+
|
|
148
|
+
this.validateRoot = (data, { rootPrefix = '', ignore = [] } = {}) => {
|
|
149
|
+
let validateFn
|
|
150
|
+
|
|
151
|
+
if (ignore.length > 0) {
|
|
152
|
+
const newRootSchema = extend(true, {}, this.getRootSchema())
|
|
153
|
+
|
|
154
|
+
ignore.forEach((prop) => {
|
|
155
|
+
if (hasOwn(newRootSchema, prop)) {
|
|
156
|
+
set(newRootSchema, prop, true)
|
|
157
|
+
unsetValue(newRootSchema, prop)
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
validateFn = validator.compile(newRootSchema)
|
|
162
|
+
} else {
|
|
163
|
+
validateFn = rootValidate
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return runSchemaValidation(validator, validateFn, data, rootPrefix)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
validatorCollection.set(this, validator)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
addSchema (name, _schema, replace = false) {
|
|
174
|
+
const validator = validatorCollection.get(this)
|
|
175
|
+
let schema = _schema
|
|
176
|
+
|
|
177
|
+
if (typeof schema === 'object' && !Array.isArray(schema) && schema.$schema == null) {
|
|
178
|
+
schema = Object.assign({}, schema, { $schema: this.schemaVersion })
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (replace === true && validator.getSchema(name) != null) {
|
|
182
|
+
validator.removeSchema(name)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
validator.addSchema(schema, name)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// after validate, data will be coerced (modified) with value types
|
|
189
|
+
// that match the schema
|
|
190
|
+
validate (name, data, { rootPrefix = '' } = {}) {
|
|
191
|
+
const validator = validatorCollection.get(this)
|
|
192
|
+
const schemaValidate = validator.getSchema(name)
|
|
193
|
+
|
|
194
|
+
if (schemaValidate == null) {
|
|
195
|
+
throw new Error(`schema ${name} is not registered in validator`)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return runSchemaValidation(validator, schemaValidate, data, rootPrefix)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getSchema (name) {
|
|
202
|
+
const validator = validatorCollection.get(this)
|
|
203
|
+
const schemaValidate = validator.getSchema(name)
|
|
204
|
+
return schemaValidate ? schemaValidate.schema : undefined
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function addDefaultsAndValidateRootSchema (validator, _rootSchema, schemaVersion) {
|
|
209
|
+
let rootSchema = _rootSchema
|
|
210
|
+
|
|
211
|
+
if (
|
|
212
|
+
typeof schema === 'object' &&
|
|
213
|
+
!Array.isArray(rootSchema) &&
|
|
214
|
+
rootSchema.$schema == null
|
|
215
|
+
) {
|
|
216
|
+
rootSchema = Object.assign({}, rootSchema, { $schema: schemaVersion })
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const schemaValid = validator.validateSchema(rootSchema)
|
|
220
|
+
|
|
221
|
+
if (!schemaValid) {
|
|
222
|
+
throw new Error(`root schema is not valid. errors: ${
|
|
223
|
+
validator.errorsText(validator.errors, { dataVar: 'rootSchema' })
|
|
224
|
+
}`)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return rootSchema
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function runSchemaValidation (validator, schemaValidate, data, rootPrefix) {
|
|
231
|
+
const valid = schemaValidate(data)
|
|
232
|
+
const result = { valid }
|
|
233
|
+
|
|
234
|
+
if (!valid) {
|
|
235
|
+
result.errors = schemaValidate.errors.map((err) => {
|
|
236
|
+
if (err.keyword === 'enum' && err.params && Array.isArray(err.params.allowedValues)) {
|
|
237
|
+
// ajv errorsText does not put allowed values in message, we need to add it manually
|
|
238
|
+
err.message += `. allowed values: [${err.params.allowedValues.map(v => JSON.stringify(v)).join(', ')}]`
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return err
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
result.fullErrorMessage = `schema validation errors: ${
|
|
245
|
+
validator.errorsText(result.errors, { dataVar: rootPrefix })
|
|
246
|
+
}`
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return result
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
module.exports = SchemaValidator
|