forked from colinskow/sofa-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (159 loc) · 4.12 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
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
const validate = require('validate.js'),
merge = require('deepmerge'),
sanitize = require('./sanitize'),
utils = require('./utils');
module.exports = function (options) {
const whitelist = (doc, list) => {
const results = {};
for (const item in list) {
const selected = utils.getObjectRef(doc, list[item]);
if (typeof selected !== 'undefined') {
utils.setObjectRef(results, list[item], selected);
}
}
return results;
};
const blacklist = (doc, list) => {
for (const item in list) {
const selected = utils.getObjectRef(doc, item);
if (typeof selected !== 'undefined') {
utils.delObjectRef(doc, item);
}
}
return doc;
};
const rename = (doc, list) => {
for (const item in list) {
const selected = utils.getObjectRef(doc, item);
if (typeof selected !== 'undefined') {
utils.setObjectRef(doc, list[item], selected);
utils.delObjectRef(doc, item);
}
}
return doc;
};
const deepClone = obj => {
return JSON.parse(JSON.stringify(obj));
};
class SofaModel {
constructor(record) {
this.results = deepClone(record);
this.validator = validate;
this.errors = null;
const customValidators = options.customValidators || {};
for (const validator in customValidators) {
if (
Object.prototype.hasOwnProperty.call(customValidators, validator) &&
typeof customValidators[validator] === 'function'
) {
this.validator.validators[validator] = customValidators[validator];
}
}
}
validate() {
if (typeof options.validate !== 'object') {
return this;
}
if (options.async) {
return new Promise((resolve, reject) => {
validate
.async(this.results, options.validate, options.valOptions || {})
.then(
() => {
resolve(this.results);
},
err => {
this.errors = err;
reject(err);
}
);
});
} else {
this.errors =
validate(this.results, options.validate, options.valOptions || {}) ||
null;
return this;
}
}
sanitize() {
if (options.sanitize) {
this.results = sanitize(
this.results,
options.sanitize || {},
options.customSanitizers || {}
);
}
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
whitelist() {
if (options.whitelist) {
this.results = whitelist(this.results, options.whitelist);
}
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
blacklist() {
if (options.blacklist) {
this.results = blacklist(this.results, options.blacklist);
}
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
rename() {
if (options.rename) {
this.results = rename(this.results, options.rename);
}
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
static() {
if (options.static) {
this.results = merge(this.results, options.static);
}
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
merge(doc) {
this.results = merge(doc, this.results);
if (options.async) {
return Promise.resolve(this.results);
} else {
return this;
}
}
process() {
if (options.async) {
return this.whitelist()
.then(() => this.blacklist())
.then(() => this.sanitize())
.then(() => this.validate())
.then(() => this.rename())
.then(() => this.static());
} else {
return this.whitelist()
.blacklist()
.sanitize()
.validate()
.rename()
.static();
}
}
}
return SofaModel;
};