-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema-fields.js
70 lines (51 loc) · 1.41 KB
/
schema-fields.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
import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions';
// checkNpmVersions({
// 'simpl-schema': '0.0.x'
// }, 'wesleyfsmith:schema-fields');
//TODO for when we switch to new simpl schema version
const useProto = false;
// SimpleSchema = require('simpl-schema');
// let SimpleSchema = SimpleSchema;
//
// if (!SimpleSchema) { //for backwards compatibility
// SimpleSchema = require('simpl-schema');
// } else {
// useProto = true;
// }
// SimpleSchema = require('simpl-schema');
// console.log(SimpleSchema.prototype);
export const getFields = (schema, isObject) => {
schema = schema._schema;
//TODO:
let obj = {};
let arr = [];
_.each(schema, function(value, key) {
//skip if key is part of sub Array
//TODO: check if parent has specific rules for children on sub schema
if (key.includes('$') || key.includes('.') ) {
return;
}
if (isObject) {
obj[key] = 1;
} else {
arr.push(key);
}
});
if (isObject) {
return obj;
} else {
return arr;
}
}
Meteor.Collection.prototype.getFields = function() {
return getFields(this.simpleSchema(), false);
}
Meteor.Collection.prototype.getFieldsAsObject = function() {
return getFields(this.simpleSchema(), true);
}
SimpleSchema.prototype.getFields = function() {
return getFields(this, false);
}
SimpleSchema.prototype.getFieldsAsObject = function() {
return getFields(this, true);
}