-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmongoosify.js
More file actions
180 lines (144 loc) · 3.92 KB
/
mongoosify.js
File metadata and controls
180 lines (144 loc) · 3.92 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
174
175
176
177
178
179
180
"use strict";
var _ = require('lodash');
var fs = require("fs");
/**
*
* @param key
* @returns {boolean}
*/
var hasSchemaKey = function (schemaObject, key) {
return _.has(schemaObject, key);
};
/**
*
* @param idVal a value of a $schema property
* @returns true if a value of a $schema property is valid, else false
*/
var hasValidSchemaVal = function (schemaObject, idVal) {
return true;
//TODO
};
var hasValidTypeVal = function (typeVal) {
return _.includes(["array", "boolean", "integer", "number", "object", "string"], typeVal);
};
var hasValidPropertiesVal = function(propertiesVal){
return !_.isEmpty(propertiesVal);
};
var validateSchema = function (schemaObject) {
var schema = "$schema";
if(hasSchemaKey(schemaObject, schema)) {
var schemaVal = _.get(schemaObject, schema);
if(!hasValidSchemaVal(schemaVal)){
throw "the value of a '$schema' property is not valid.\nvalue:"+schemaObject;
}
}
var id = "id";
if(hasSchemaKey(schemaObject, id)) {
var idVal = _.get(schemaObject, id);
if(!hasValidSchemaVal(idVal)){
throw "the value of a 'id' property is not valid\nvalue:"+idVal;
}
}
var type = "type";
if(hasSchemaKey(schemaObject, type)) {
var typeVal = _.get(schemaObject, type);
if(!hasValidTypeVal(typeVal)){
throw "the value of a 'type' property is not valid.\nvalue:"+typeVal;
}
}
var properties = "properties";
var propertiesVal;
if(hasSchemaKey(schemaObject, properties)) {
propertiesVal = _.get(schemaObject, properties);
if(!hasValidPropertiesVal(propertiesVal)){
throw "the value of a 'properties' property is not valid.\nvalue:"+propertiesVal;
}
}
var additionalProperties = "additionalProperties";
if(hasSchemaKey(schemaObject, additionalProperties)) {
var additionalPropertiesVal = _.get(schemaObject, additionalProperties);
}
var required = "required";
if(hasSchemaKey(schemaObject, required)) {
var requiredVal = _.get(schemaObject, required);
}
};
var mapPrimitiveTypes = function (primitiveType) {
if (primitiveType.match(/^string|integer|array|object|boolean|number$/g)) {
if (primitiveType.indexOf('integer') === 0) {
primitiveType = "number";
}
return eval(primitiveType.charAt(0).toUpperCase() + primitiveType.substr(1));
}
};
var mapComplexTypes = function(value, strType) {
var tmp = null;
var type = strType || value['type'];
switch(type) {
case 'array':
tmp = [];
if(value['items']['$ref'] != null) {
tmp.push({
type: Schema.ObjectId,
ref: value['items']['$ref']
});
} else {
var originalType = value['items']['type'];
value['items']['type'] = mapPrimitiveTypes(value['items']['type']);
tmp.push(mapComplexTypes(value['items'], originalType));
}
break;
case 'object':
tmp = {};
var props = value['properties'];
_.each(props, function(data, k) {
if(data['$ref'] != null) {
tmp[k] = {
type: Schema.ObjectId,
ref: data['$ref']
};
} else {
tmp[k] = mapPrimitiveTypes(data['type'])
}
});
break;
default:
tmp = value;
tmp['type'] = mapPrimitiveTypes(type);
break;
}
return tmp
};
/**
*
* @param data
* @returns {{}}
*/
var mapSchema = function(data){
var tmp = null;
var mongooseSchema = {};
_.each(data, function (value, key) {
if(value['$ref'] != null) {
tmp = {
type: Schema.ObjectId,
ref: value['$ref']
}
}else {
tmp = mapComplexTypes(value);
}
mongooseSchema[key] = tmp;
});
return mongooseSchema;
};
/**
*
* @param schemaObject a json schema object
* @return a mongoose schema of a given json schema
*/
var mongoosify = function (schemaObject) {
validateSchema(schemaObject);
var data = schemaObject["properties"];
var schema = mapSchema(data);
return schema;
};
module.exports = mongoosify;