-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (36 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const validateModel = require('./lib/model')
const validateReferences = require('./lib/refs')
const validateProperty = require('./lib/property')
const utils = require('./lib/utils')
const { updateErrorWithMessage, handleError } = utils
exports = module.exports = validate
exports.model = validateModel
exports.refs = validateReferences
exports.property = validateProperty
exports.utils = utils
exports.StubModel = require('./lib/stub-model')
exports.ObjectModel = require('./lib/object-model')
function validate (models) {
if (!Array.isArray(models)) {
if (isModel(models)) {
return validateModel(models)
}
// convert to array
models = Object.keys(models).map(id => models[id])
}
models.forEach(model => {
try {
validateModel(model)
} catch (err) {
handleError(
updateErrorWithMessage(err, `invalid model "${model.id}": ${err.message}`)
)
}
})
validateReferences({ models })
}
function isModel (obj) {
return obj.type === 'tradle.Model' &&
(obj.properties && typeof obj.properties === 'object') &&
typeof obj.id === 'string'
}