-
Notifications
You must be signed in to change notification settings - Fork 0
/
astronomy-simple-schema.js
122 lines (109 loc) · 3.46 KB
/
astronomy-simple-schema.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
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
var constructorTypeMap = {
StringField: String,
DateField: Date,
BooleanField: Boolean,
ObjectField: Object,
NumberField: Number,
ArrayField: []
// Sorry, no NullField in SimpleSchema -_-
};
var determineFieldType = function(field) {
var constructorName = field.constructor.name;
var fieldType = constructorTypeMap[constructorName];
if (constructorName === 'ArrayField') {
// XXX Need to account for nested SimpleSchemas
// Currently only plain nested types are allowed
var arrayElementType = constructorTypeMap[field.field.constructor.name];
if (!_.isUndefined(arrayElementType)) {
fieldType.push(arrayElementType);
} else {
// XXX Need to think this more:
// What do we do about NullFields? SimpleSchema doesn't
// support them
// Assume an array of objects
// And flag the field as `blackbox` true for now
fieldType.push(Object);
}
}
return fieldType;
};
var validatorMap = {
minLength: 'min',
maxLength: 'max',
gte: 'min',
lte: 'max',
gt: 'exclusiveMin',
lt: 'exclusiveMax',
email: 'regEx',
choice: 'allowedValues',
unique: 'unique',
regexp: 'regEx'
};
applySingleFieldValidator = function(fieldDef, validatorDef) {
var validatorSimpleSchemaObject = validatorMap[validatorDef.validator.name];
switch (validatorDef.validator.name) {
case 'minLength':
case 'gte':
case 'maxLength':
case 'lte':
case 'gt':
case 'lt':
case 'choice':
case 'unique':
case 'regexp':
fieldDef[validatorSimpleSchemaObject] = validatorDef.param;
break;
case 'email':
fieldDef[validatorSimpleSchemaObject] = SimpleSchema.RegEx.Email;
break;
default:
console.log('Currently there is no translation of', validatorDef.validator.name);
break;
}
};
Astro.eventManager.on('initClass', function() {
var Class = this;
Class.getSimpleSchema = function(manualFieldDefs) {
var fieldDefs = {};
_.each(Class.getFields(), function(field, fieldName) {
var fieldDef = {};
// Check if we have a manual field definition
// This is used when Astronomy's schema definitions
// are way too complex to be translated into a SimpleSchema definition
if (manualFieldDefs && manualFieldDefs[fieldName]) {
fieldDefs[fieldName] = manualFieldDefs[fieldName];
// Continue
return;
}
// The field type is required
fieldDef.type = determineFieldType(field);
// XXX for now any object field or array of Objects will be a blackbox
// until I get a chance to figure it out, if possible
if (fieldDef.type === Object || (_.isArray(fieldDef.type) && fieldDef.type[0] === Object)) {
fieldDef.blackbox = true;
}
// defaultValue
if (field.default) {
fieldDef.defaultValue = _.isFunction(field.default) ? field.default() : field.default;
}
// denyUpdate
if (field.immutable) {
fieldDef.denyUpdate = true;
}
// optional
if (field.optional) {
fieldDef.optional = true;
}
var fieldValidator = Class.getValidator(fieldName);
if (fieldValidator) {
// XXX Currently only supports non-array Validators
// Use manual field def approach to define your simple schema field
if (!_.isArray(fieldValidator)) {
applySingleFieldValidator(fieldDef, fieldValidator);
}
}
fieldDefs[fieldName] = fieldDef;
});
return new SimpleSchema(fieldDefs);
};
});