-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (142 loc) · 4.84 KB
/
index.js
File metadata and controls
173 lines (142 loc) · 4.84 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
var arrify = require('arrify')
var is = require('is')
/* eslint-disable no-undef, semi, indent */
var customTypes = {
Double: function (value) {
return new entity.Double(value);
}.toString(),
GeoPoint: function (coordindates) {
return new entity.GeoPoint(coordindates);
}.toString(),
Int: function (value) {
return new entity.Int(value);
}.toString()
}
/* eslint-enable no-undef, semi, indent */
var getKind = function (key) {
if (key.kind) return key.kind
var path = key.path
if (path.length % 2 === 0) {
return path[path.length - 2]
} else {
return path[path.length - 1]
}
}
var validateType = function (intendedType, prop, value) {
var errorMessages = []
// Is it a native type?
if (intendedType === global[intendedType.name]) {
if (!is.type(value, intendedType.name.toLowerCase())) {
// Some types don't exist on `is`, e.g. Buffer. One last check:
if (!(value instanceof global[intendedType.name])) {
errorMessages.push([
'Schema definition violated for property: "' + prop + '".',
'Expected type: ' + intendedType.name + ', received: ' + JSON.stringify(value)
].join(' '))
}
}
return errorMessages
}
// Is it a custom Datastore type?
for (var customTypeName in customTypes) {
var customTypeStringified = customTypes[customTypeName]
if (intendedType.toString() === customTypeStringified) {
if (value.constructor.name !== customTypeName) {
errorMessages.push([
'Schema definition violated for property: "' + prop + '".',
'Expected type: ' + customTypeName + ', received: ' + value.constructor.name
].join(' '))
}
continue
}
}
// Assume it's a custom intendedType function
if (!intendedType(value)) {
errorMessages.push('Schema definition violated for property: "' + prop + '"')
}
return errorMessages
}
var validateSchema = function (schema, data) {
var errorMessages = []
var dataProperties = Object.keys(data)
for (var prop in schema) {
var validator = schema[prop]
if (!validator) {
errorMessages.push('Schema definition not found for property: "' + prop + '"')
continue
}
if (!is.defined(data[prop])) {
errorMessages.push('Schema definition expected property: "' + prop + '"')
continue
}
var value = data[prop]
var nestedErrorMessages = []
if (dataProperties.indexOf(prop) > -1) {
dataProperties.splice(dataProperties.indexOf(prop), 1)
}
if (is.object(validator)) {
// Nested schema
nestedErrorMessages = validateSchema(validator, value)
} else if (is.array(validator)) {
// Array with a nested schema
var arraySchema = validator[0]
if (!is.array(value)) {
var nestedErrorMessage = [
'Schema definition violated for property: "',
'Expected type: Array, received: ' + value.constructor.name
].join(' ')
nestedErrorMessages.push(nestedErrorMessage)
} else {
value
.map(function (value) {
if (is.object(value)) return validateSchema(arraySchema, value)
if (is.array(value)) return value.map(validateType.bind(null, arraySchema, prop))
return validateType(arraySchema, prop, value)
})
.forEach(function (errors) {
if (errors.length > 0) nestedErrorMessages = errors
})
}
} else {
errorMessages = errorMessages.concat(validateType(validator, prop, value))
}
if (nestedErrorMessages.length > 0) {
nestedErrorMessages = nestedErrorMessages.map(function (errorMessage) {
var separator = is.object(validator) ? '.' : '[].'
return errorMessage.replace('"', '"' + prop + separator)
})
errorMessages = errorMessages.concat(nestedErrorMessages)
}
}
if (dataProperties.length > 0) {
errorMessages.push('Unexpected properties found: "' + dataProperties.join('", "') + '"')
}
return errorMessages
}
module.exports = function (datastore) {
var schemas = {}
datastore.register = function (kind, schema) {
schemas[kind] = schema
}
var save = datastore.save
datastore.save = function (entities, callback) {
var schemaValidationError = new Error('Schema validation failed')
schemaValidationError.code = 'ESCHEMAVIOLATION'
schemaValidationError.errors = []
arrify(entities).forEach(function (entity) {
var kind = getKind(entity.key)
if (!schemas[kind]) return
var validationFailureMessages = validateSchema(schemas[kind], entity.data)
if (validationFailureMessages.length > 0) {
schemaValidationError.errors.push({
kind: kind,
errors: validationFailureMessages
})
}
})
if (schemaValidationError.errors.length > 0) callback(schemaValidationError)
else save.apply(datastore, arguments)
}
return datastore
}