@orion-js/schema 3.11.8 → 4.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3635 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +3607 -0
- package/package.json +19 -13
- package/LICENSE +0 -21
- package/lib/Errors.d.ts +0 -21
- package/lib/Errors.js +0 -22
- package/lib/ValidationError.d.ts +0 -14
- package/lib/ValidationError.js +0 -45
- package/lib/ValidationError.test.d.ts +0 -1
- package/lib/ValidationError.test.js +0 -35
- package/lib/clean/cleanType.d.ts +0 -3
- package/lib/clean/cleanType.js +0 -49
- package/lib/clean/getObjectNode.d.ts +0 -2
- package/lib/clean/getObjectNode.js +0 -14
- package/lib/clean/index.d.ts +0 -2
- package/lib/clean/index.js +0 -30
- package/lib/clean/index.test.d.ts +0 -1
- package/lib/clean/index.test.js +0 -482
- package/lib/clean/recursiveClean.d.ts +0 -3
- package/lib/clean/recursiveClean.js +0 -89
- package/lib/cleanKey.d.ts +0 -1
- package/lib/cleanKey.js +0 -25
- package/lib/cleanKey.test.d.ts +0 -1
- package/lib/cleanKey.test.js +0 -115
- package/lib/dotGetSchema.d.ts +0 -2
- package/lib/dotGetSchema.js +0 -7
- package/lib/fieldType.d.ts +0 -17
- package/lib/fieldType.js +0 -28
- package/lib/fieldTypes/ID.d.ts +0 -2
- package/lib/fieldTypes/ID.js +0 -29
- package/lib/fieldTypes/ID.test.d.ts +0 -1
- package/lib/fieldTypes/ID.test.js +0 -25
- package/lib/fieldTypes/any.d.ts +0 -2
- package/lib/fieldTypes/any.js +0 -10
- package/lib/fieldTypes/any.test.d.ts +0 -1
- package/lib/fieldTypes/any.test.js +0 -13
- package/lib/fieldTypes/array.d.ts +0 -2
- package/lib/fieldTypes/array.js +0 -23
- package/lib/fieldTypes/array.test.d.ts +0 -1
- package/lib/fieldTypes/array.test.js +0 -22
- package/lib/fieldTypes/blackbox.d.ts +0 -2
- package/lib/fieldTypes/blackbox.js +0 -15
- package/lib/fieldTypes/blackbox.test.d.ts +0 -1
- package/lib/fieldTypes/blackbox.test.js +0 -16
- package/lib/fieldTypes/boolean.d.ts +0 -2
- package/lib/fieldTypes/boolean.js +0 -36
- package/lib/fieldTypes/boolean.test.d.ts +0 -1
- package/lib/fieldTypes/boolean.test.js +0 -27
- package/lib/fieldTypes/date.d.ts +0 -2
- package/lib/fieldTypes/date.js +0 -36
- package/lib/fieldTypes/date.test.d.ts +0 -1
- package/lib/fieldTypes/date.test.js +0 -37
- package/lib/fieldTypes/email.d.ts +0 -2
- package/lib/fieldTypes/email.js +0 -29
- package/lib/fieldTypes/email.test.d.ts +0 -1
- package/lib/fieldTypes/email.test.js +0 -20
- package/lib/fieldTypes/enum.d.ts +0 -4
- package/lib/fieldTypes/enum.js +0 -57
- package/lib/fieldTypes/enum.test.d.ts +0 -1
- package/lib/fieldTypes/enum.test.js +0 -41
- package/lib/fieldTypes/index.d.ts +0 -14
- package/lib/fieldTypes/index.js +0 -29
- package/lib/fieldTypes/integer.d.ts +0 -2
- package/lib/fieldTypes/integer.js +0 -17
- package/lib/fieldTypes/integer.test.d.ts +0 -1
- package/lib/fieldTypes/integer.test.js +0 -19
- package/lib/fieldTypes/number.d.ts +0 -2
- package/lib/fieldTypes/number.js +0 -32
- package/lib/fieldTypes/number.test.d.ts +0 -1
- package/lib/fieldTypes/number.test.js +0 -28
- package/lib/fieldTypes/plainObject.d.ts +0 -2
- package/lib/fieldTypes/plainObject.js +0 -29
- package/lib/fieldTypes/plainObject.test.d.ts +0 -1
- package/lib/fieldTypes/plainObject.test.js +0 -42
- package/lib/fieldTypes/string.d.ts +0 -2
- package/lib/fieldTypes/string.js +0 -47
- package/lib/fieldTypes/string.test.d.ts +0 -1
- package/lib/fieldTypes/string.test.js +0 -28
- package/lib/getSchemaFromTypedModel.d.ts +0 -2
- package/lib/getSchemaFromTypedModel.js +0 -15
- package/lib/getValidationErrors/convertTypedModel.d.ts +0 -3
- package/lib/getValidationErrors/convertTypedModel.js +0 -33
- package/lib/getValidationErrors/doValidation.d.ts +0 -2
- package/lib/getValidationErrors/doValidation.js +0 -78
- package/lib/getValidationErrors/getError/getFieldType.d.ts +0 -4
- package/lib/getValidationErrors/getError/getFieldType.js +0 -13
- package/lib/getValidationErrors/getError/getFieldValidator.d.ts +0 -2
- package/lib/getValidationErrors/getError/getFieldValidator.js +0 -38
- package/lib/getValidationErrors/getError/getFieldValidator.test.d.ts +0 -1
- package/lib/getValidationErrors/getError/getFieldValidator.test.js +0 -72
- package/lib/getValidationErrors/getError/index.d.ts +0 -2
- package/lib/getValidationErrors/getError/index.js +0 -44
- package/lib/getValidationErrors/getError/index.test.d.ts +0 -1
- package/lib/getValidationErrors/getError/index.test.js +0 -52
- package/lib/getValidationErrors/getValidationErrorsObject.d.ts +0 -4
- package/lib/getValidationErrors/getValidationErrorsObject.js +0 -17
- package/lib/getValidationErrors/index.d.ts +0 -2
- package/lib/getValidationErrors/index.js +0 -34
- package/lib/getValidationErrors/index.test.d.ts +0 -1
- package/lib/getValidationErrors/index.test.js +0 -416
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -42
- package/lib/isValid.d.ts +0 -2
- package/lib/isValid.js +0 -11
- package/lib/isValid.test.d.ts +0 -1
- package/lib/isValid.test.js +0 -25
- package/lib/types/fieldValidators.d.ts +0 -2
- package/lib/types/fieldValidators.js +0 -2
- package/lib/types/index.d.ts +0 -2
- package/lib/types/index.js +0 -18
- package/lib/types/schema.d.ts +0 -118
- package/lib/types/schema.js +0 -3
- package/lib/types/types.test.d.ts +0 -1
- package/lib/types/types.test.js +0 -17
- package/lib/validate.d.ts +0 -2
- package/lib/validate.js +0 -14
- package/lib/validate.test.d.ts +0 -1
- package/lib/validate.test.js +0 -29
- package/lib/validateKey/dotGetSchema.d.ts +0 -2
- package/lib/validateKey/dotGetSchema.js +0 -32
- package/lib/validateKey/dotGetSchema.test.d.ts +0 -1
- package/lib/validateKey/dotGetSchema.test.js +0 -65
- package/lib/validateKey/index.d.ts +0 -2
- package/lib/validateKey/index.js +0 -31
- package/lib/validateKey/index.test.d.ts +0 -1
- package/lib/validateKey/index.test.js +0 -84
package/lib/clean/index.test.js
DELETED
|
@@ -1,482 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const index_1 = __importDefault(require("./index"));
|
|
7
|
-
test('autoconverts values', async () => {
|
|
8
|
-
const schema = {
|
|
9
|
-
number: {
|
|
10
|
-
type: Number
|
|
11
|
-
},
|
|
12
|
-
string: {
|
|
13
|
-
type: String
|
|
14
|
-
},
|
|
15
|
-
string2: {
|
|
16
|
-
type: String
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
const doc = {
|
|
20
|
-
number: '12',
|
|
21
|
-
string: 12,
|
|
22
|
-
string2: 'string '
|
|
23
|
-
};
|
|
24
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
25
|
-
expect(cleaned).toEqual({
|
|
26
|
-
number: 12,
|
|
27
|
-
string: '12',
|
|
28
|
-
string2: 'string'
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
test('dont autoConvert values', async () => {
|
|
32
|
-
const schema = {
|
|
33
|
-
number: {
|
|
34
|
-
type: 'number'
|
|
35
|
-
},
|
|
36
|
-
string: {
|
|
37
|
-
type: String
|
|
38
|
-
},
|
|
39
|
-
string2: {
|
|
40
|
-
type: String
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const doc = {
|
|
44
|
-
number: '12',
|
|
45
|
-
string: 12,
|
|
46
|
-
string2: 'string '
|
|
47
|
-
};
|
|
48
|
-
const cleaned = await (0, index_1.default)(schema, doc, { autoConvert: false, trimStrings: false });
|
|
49
|
-
expect(cleaned).toEqual({
|
|
50
|
-
number: '12',
|
|
51
|
-
string: 12,
|
|
52
|
-
string2: 'string '
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
test('dont remove null values', async () => {
|
|
56
|
-
const schema = {
|
|
57
|
-
string: {
|
|
58
|
-
type: String
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
const doc = {
|
|
62
|
-
string: null
|
|
63
|
-
};
|
|
64
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
65
|
-
expect(cleaned).toEqual({
|
|
66
|
-
string: null
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
test('cleans boolean correctly', async () => {
|
|
70
|
-
const type = { type: Boolean };
|
|
71
|
-
const schema = {
|
|
72
|
-
a: type,
|
|
73
|
-
b: type,
|
|
74
|
-
c: type,
|
|
75
|
-
d: type,
|
|
76
|
-
e: type
|
|
77
|
-
};
|
|
78
|
-
const doc = {
|
|
79
|
-
a: 1,
|
|
80
|
-
b: 'true',
|
|
81
|
-
c: 0,
|
|
82
|
-
d: true,
|
|
83
|
-
e: 'false',
|
|
84
|
-
f: true
|
|
85
|
-
};
|
|
86
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
87
|
-
expect(cleaned).toEqual({
|
|
88
|
-
a: true,
|
|
89
|
-
b: true,
|
|
90
|
-
c: false,
|
|
91
|
-
d: true,
|
|
92
|
-
e: false
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
test('trims strings and filter them', async () => {
|
|
96
|
-
const type = { type: String };
|
|
97
|
-
const schema = {
|
|
98
|
-
aString: type,
|
|
99
|
-
otherString: type
|
|
100
|
-
};
|
|
101
|
-
const doc = {
|
|
102
|
-
aString: ' 123 321 \n 123 \n\t d\t',
|
|
103
|
-
otherString: ' '
|
|
104
|
-
};
|
|
105
|
-
const cleaned = await (0, index_1.default)(schema, doc, { trimStrings: true, removeEmptyStrings: true });
|
|
106
|
-
expect(cleaned).toEqual({
|
|
107
|
-
aString: '123 321 \n 123 \n\t d'
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
test('filter fields not in schema', async () => {
|
|
111
|
-
const type = { type: String };
|
|
112
|
-
const schema = {
|
|
113
|
-
a: type,
|
|
114
|
-
b: type
|
|
115
|
-
};
|
|
116
|
-
const doc = {
|
|
117
|
-
b: 'hi',
|
|
118
|
-
c: 'hello'
|
|
119
|
-
};
|
|
120
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
121
|
-
expect(cleaned).toEqual({
|
|
122
|
-
b: 'hi'
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
test('runs autovalues with arrays', async () => {
|
|
126
|
-
const schema = {
|
|
127
|
-
texts: {
|
|
128
|
-
type: [String],
|
|
129
|
-
autoValue(values) {
|
|
130
|
-
return values.map(val => val + ' world');
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
const doc = {
|
|
135
|
-
texts: ['hello', 'good bye']
|
|
136
|
-
};
|
|
137
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
138
|
-
expect(cleaned).toEqual({
|
|
139
|
-
texts: ['hello world', 'good bye world']
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
test('run autovalue when field is not present', async () => {
|
|
143
|
-
const schema = {
|
|
144
|
-
text: {
|
|
145
|
-
type: String,
|
|
146
|
-
autoValue() {
|
|
147
|
-
return 'a value';
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
const doc = {};
|
|
152
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
153
|
-
expect(cleaned).toEqual({ text: 'a value' });
|
|
154
|
-
});
|
|
155
|
-
test('returns the default values', async () => {
|
|
156
|
-
const schema = {
|
|
157
|
-
text: {
|
|
158
|
-
type: String,
|
|
159
|
-
defaultValue: 'hello',
|
|
160
|
-
autoValue(value) {
|
|
161
|
-
return value + ' world';
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
text1: {
|
|
165
|
-
type: String,
|
|
166
|
-
defaultValue: 'text1'
|
|
167
|
-
},
|
|
168
|
-
text2: {
|
|
169
|
-
type: String,
|
|
170
|
-
defaultValue: 'text2'
|
|
171
|
-
},
|
|
172
|
-
text3: {
|
|
173
|
-
type: String
|
|
174
|
-
},
|
|
175
|
-
text4: {
|
|
176
|
-
type: String,
|
|
177
|
-
defaultValue() {
|
|
178
|
-
return 'afunc';
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
const doc = { text1: 'pass' };
|
|
183
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
184
|
-
expect(cleaned).toEqual({
|
|
185
|
-
text: 'hello world',
|
|
186
|
-
text1: 'pass',
|
|
187
|
-
text2: 'text2',
|
|
188
|
-
text4: 'afunc'
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
test('run deep autovalues', async () => {
|
|
192
|
-
const deep = {
|
|
193
|
-
s: {
|
|
194
|
-
type: String,
|
|
195
|
-
autoValue() {
|
|
196
|
-
return 'no';
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
const schema = {
|
|
201
|
-
text: {
|
|
202
|
-
type: deep,
|
|
203
|
-
autoValue(text) {
|
|
204
|
-
return { ...text, type: 'text' };
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
texts: {
|
|
208
|
-
type: [deep],
|
|
209
|
-
autoValue(texts) {
|
|
210
|
-
return [texts[0], 'yes'];
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
const doc = {
|
|
215
|
-
text: { s: 'objecttext' },
|
|
216
|
-
texts: [{ s: 'arraytext' }, { s: 'noa' }]
|
|
217
|
-
};
|
|
218
|
-
const cleaned = await (0, index_1.default)(schema, doc);
|
|
219
|
-
expect(cleaned).toEqual({
|
|
220
|
-
text: { s: 'no' },
|
|
221
|
-
texts: [{ s: 'no' }, 'yes']
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
test('perform custom cleaning from clean option in field', async () => {
|
|
225
|
-
const person = {
|
|
226
|
-
name: {
|
|
227
|
-
type: String,
|
|
228
|
-
async clean(value) {
|
|
229
|
-
if (value === 'Joaquin') {
|
|
230
|
-
return 'Roberto';
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
return value;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
const schema = {
|
|
239
|
-
persons: {
|
|
240
|
-
type: [person]
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
const cleaned = await (0, index_1.default)(schema, {
|
|
244
|
-
persons: [{ name: 'Nicolás' }, { name: 'Joaquin' }]
|
|
245
|
-
});
|
|
246
|
-
expect(cleaned).toEqual({
|
|
247
|
-
persons: [{ name: 'Nicolás' }, { name: 'Roberto' }]
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
test('perform custom cleaning', async () => {
|
|
251
|
-
const person = {
|
|
252
|
-
name: {
|
|
253
|
-
type: String
|
|
254
|
-
},
|
|
255
|
-
async __clean(value) {
|
|
256
|
-
if (value.name === 'Joaquin') {
|
|
257
|
-
return { name: 'Roberto' };
|
|
258
|
-
}
|
|
259
|
-
else {
|
|
260
|
-
return value;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
const schema = {
|
|
265
|
-
persons: {
|
|
266
|
-
type: [person]
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
// TODO: Check why the __clean method is being used here instead of clean
|
|
270
|
-
const cleaned = await (0, index_1.default)(schema, {
|
|
271
|
-
persons: [{ name: 'Nicolás' }, { name: 'Joaquin' }]
|
|
272
|
-
});
|
|
273
|
-
expect(cleaned).toEqual({
|
|
274
|
-
persons: [{ name: 'Nicolás' }, { name: 'Roberto' }]
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
test('perform non deep custom cleaning', async () => {
|
|
278
|
-
const schema = {
|
|
279
|
-
name: {
|
|
280
|
-
type: String
|
|
281
|
-
},
|
|
282
|
-
async __clean(value) {
|
|
283
|
-
if (value.name === 'Joaquin') {
|
|
284
|
-
return { name: 'Roberto' };
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
287
|
-
return value;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
// TODO: Check why the __clean method is being used here instead of clean
|
|
292
|
-
const cleaned = await (0, index_1.default)(schema, { name: 'Joaquin' });
|
|
293
|
-
expect(cleaned).toEqual({ name: 'Roberto' });
|
|
294
|
-
});
|
|
295
|
-
test('Handle errors while cleaning', async () => {
|
|
296
|
-
const schema = {
|
|
297
|
-
name: {
|
|
298
|
-
type: String,
|
|
299
|
-
async autoValue() {
|
|
300
|
-
throw new Error('an error');
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
try {
|
|
305
|
-
await (0, index_1.default)(schema, { name: 'Joaquin' });
|
|
306
|
-
}
|
|
307
|
-
catch (error) {
|
|
308
|
-
expect(error.message).toBe('Error cleaning field name, error: an error');
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
test('pass currentDoc cleaning arrays', async () => {
|
|
312
|
-
const aItem = { name: 'Nicolás' };
|
|
313
|
-
const doc = { items: [aItem] };
|
|
314
|
-
const item = {
|
|
315
|
-
name: {
|
|
316
|
-
type: String,
|
|
317
|
-
async autoValue(name, { currentDoc }) {
|
|
318
|
-
expect(currentDoc).toBe(aItem);
|
|
319
|
-
return name;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
const schema = {
|
|
324
|
-
items: {
|
|
325
|
-
type: [item],
|
|
326
|
-
async autoValue(items, { currentDoc }) {
|
|
327
|
-
expect(currentDoc).toBe(doc);
|
|
328
|
-
return items;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
expect.assertions(2);
|
|
333
|
-
await (0, index_1.default)(schema, doc);
|
|
334
|
-
});
|
|
335
|
-
test('omit undefined items in array', async () => {
|
|
336
|
-
const doc = { items: [{ name: 'Nicolás' }] };
|
|
337
|
-
const item = {
|
|
338
|
-
name: {
|
|
339
|
-
type: String
|
|
340
|
-
},
|
|
341
|
-
__clean() {
|
|
342
|
-
return undefined;
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
const schema = {
|
|
346
|
-
items: {
|
|
347
|
-
type: [item]
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
// TODO: Check why the __clean method is being used here instead of clean
|
|
351
|
-
const result = await (0, index_1.default)(schema, doc);
|
|
352
|
-
expect(result).toEqual({ items: [] });
|
|
353
|
-
});
|
|
354
|
-
test('passes extra arguments to clean', async () => {
|
|
355
|
-
const doc = {
|
|
356
|
-
name: 'Nicolás'
|
|
357
|
-
};
|
|
358
|
-
const schema = {
|
|
359
|
-
name: {
|
|
360
|
-
type: String,
|
|
361
|
-
autoValue(name, info, arg1, arg2) {
|
|
362
|
-
expect(arg1).toBe(1);
|
|
363
|
-
expect(arg2).toBe(2);
|
|
364
|
-
return name;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
};
|
|
368
|
-
expect.assertions(2);
|
|
369
|
-
await (0, index_1.default)(schema, doc, null, 1, 2);
|
|
370
|
-
});
|
|
371
|
-
test('throws error when cleaning field with no type', async () => {
|
|
372
|
-
const schema = {
|
|
373
|
-
name: {
|
|
374
|
-
type: null,
|
|
375
|
-
autoValue() {
|
|
376
|
-
return 'Nicolás';
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
expect.assertions(1);
|
|
381
|
-
try {
|
|
382
|
-
await (0, index_1.default)(schema, {});
|
|
383
|
-
}
|
|
384
|
-
catch (error) {
|
|
385
|
-
expect(error.message).toBe('Error cleaning field name, error: Cleaning field with no type');
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
test('cleans when no argument is passed', async () => {
|
|
389
|
-
const schema = {
|
|
390
|
-
name: {
|
|
391
|
-
type: String,
|
|
392
|
-
autoValue() {
|
|
393
|
-
return 'Nicolás';
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
const result = await (0, index_1.default)(schema, {});
|
|
398
|
-
expect(result).toEqual({ name: 'Nicolás' });
|
|
399
|
-
});
|
|
400
|
-
test('pass currentDoc cleaning complex schemas', async () => {
|
|
401
|
-
const aCar = { brand: 'Jeep' };
|
|
402
|
-
const aMom = { name: 'Paula', car: aCar };
|
|
403
|
-
const aItem = { name: 'Nicolás', mom: aMom };
|
|
404
|
-
const doc = { items: [aItem] };
|
|
405
|
-
const car = {
|
|
406
|
-
brand: {
|
|
407
|
-
type: String,
|
|
408
|
-
async autoValue(value, { currentDoc }) {
|
|
409
|
-
expect(value).toEqual(aCar.brand);
|
|
410
|
-
expect(currentDoc).toEqual(aCar);
|
|
411
|
-
return value;
|
|
412
|
-
}
|
|
413
|
-
},
|
|
414
|
-
async __clean(value, info) {
|
|
415
|
-
expect(value).toEqual(aMom.car);
|
|
416
|
-
expect(info.currentDoc).toEqual(aMom);
|
|
417
|
-
return value;
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
const mom = {
|
|
421
|
-
name: {
|
|
422
|
-
type: String,
|
|
423
|
-
async autoValue(value, { currentDoc }) {
|
|
424
|
-
expect(value).toEqual(aMom.name);
|
|
425
|
-
expect(currentDoc).toEqual(aMom);
|
|
426
|
-
return value;
|
|
427
|
-
}
|
|
428
|
-
},
|
|
429
|
-
car: {
|
|
430
|
-
type: car,
|
|
431
|
-
async autoValue(value, { currentDoc }) {
|
|
432
|
-
expect(value).toEqual(aMom.car);
|
|
433
|
-
expect(currentDoc).toEqual(aMom);
|
|
434
|
-
return value;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
const item = {
|
|
439
|
-
name: {
|
|
440
|
-
type: String,
|
|
441
|
-
async autoValue(value, { currentDoc }) {
|
|
442
|
-
expect(value).toEqual(aItem.name);
|
|
443
|
-
expect(currentDoc).toEqual(aItem);
|
|
444
|
-
return value;
|
|
445
|
-
}
|
|
446
|
-
},
|
|
447
|
-
mom: {
|
|
448
|
-
type: mom,
|
|
449
|
-
async autoValue(value, { currentDoc }) {
|
|
450
|
-
expect(value).toEqual(aItem.mom);
|
|
451
|
-
expect(currentDoc).toEqual(aItem);
|
|
452
|
-
return value;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
const schema = {
|
|
457
|
-
items: {
|
|
458
|
-
type: [item],
|
|
459
|
-
async autoValue(value, { currentDoc }) {
|
|
460
|
-
expect(value).toEqual(doc.items);
|
|
461
|
-
expect(currentDoc).toEqual(doc);
|
|
462
|
-
return value;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
};
|
|
466
|
-
expect.assertions(14);
|
|
467
|
-
// TODO: Check why the __clean method is being used here instead of clean
|
|
468
|
-
await (0, index_1.default)(schema, doc);
|
|
469
|
-
});
|
|
470
|
-
test('On blackbox allow use of custom clean', async () => {
|
|
471
|
-
const schema = {
|
|
472
|
-
info: {
|
|
473
|
-
type: 'blackbox',
|
|
474
|
-
optional: true,
|
|
475
|
-
async clean() {
|
|
476
|
-
return { hello: 'world' };
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
const result = await (0, index_1.default)(schema, { info: { hello: 'night' } });
|
|
481
|
-
expect(result).toEqual({ info: { hello: 'world' } });
|
|
482
|
-
});
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const isUndefined_1 = __importDefault(require("lodash/isUndefined"));
|
|
7
|
-
const isArray_1 = __importDefault(require("lodash/isArray"));
|
|
8
|
-
const cleanType_1 = __importDefault(require("./cleanType"));
|
|
9
|
-
const isNil_1 = __importDefault(require("lodash/isNil"));
|
|
10
|
-
const getObjectNode_1 = __importDefault(require("./getObjectNode"));
|
|
11
|
-
const convertTypedModel_1 = require("../getValidationErrors/convertTypedModel");
|
|
12
|
-
const cleanObjectFields = async function ({ schema, value, ...other }) {
|
|
13
|
-
const keys = Object.keys(schema.type).filter(key => !key.startsWith('__'));
|
|
14
|
-
const newDoc = {};
|
|
15
|
-
for (const key of keys) {
|
|
16
|
-
try {
|
|
17
|
-
const cleanOptions = {
|
|
18
|
-
...other,
|
|
19
|
-
schema: schema.type[key],
|
|
20
|
-
value: value[key],
|
|
21
|
-
currentDoc: value
|
|
22
|
-
};
|
|
23
|
-
const newValue = await clean(cleanOptions);
|
|
24
|
-
if (!(0, isUndefined_1.default)(newValue)) {
|
|
25
|
-
newDoc[key] = newValue;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
throw new Error(`Error cleaning field ${key}, error: ${error.message}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return newDoc;
|
|
33
|
-
};
|
|
34
|
-
const cleanArrayItems = async function ({ schema, value, ...other }) {
|
|
35
|
-
// clean array items
|
|
36
|
-
const schemaType = schema.type[0];
|
|
37
|
-
const promises = value.map(async (item, index) => {
|
|
38
|
-
const newValue = await clean({
|
|
39
|
-
...other,
|
|
40
|
-
schema: {
|
|
41
|
-
type: schemaType
|
|
42
|
-
},
|
|
43
|
-
value: item,
|
|
44
|
-
currentDoc: value
|
|
45
|
-
});
|
|
46
|
-
return newValue;
|
|
47
|
-
});
|
|
48
|
-
const result = await Promise.all(promises);
|
|
49
|
-
return result.filter(value => !(0, isUndefined_1.default)(value));
|
|
50
|
-
};
|
|
51
|
-
function getArrayNode(schema, value) {
|
|
52
|
-
if ((0, isArray_1.default)(schema.type) && !(0, isNil_1.default)(value)) {
|
|
53
|
-
const result = schema;
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
const clean = async function (info) {
|
|
59
|
-
(0, convertTypedModel_1.convertTypedModel)(info);
|
|
60
|
-
let { schema, args = [], value } = info;
|
|
61
|
-
const currSchema = schema.type === undefined ? { type: schema } : schema;
|
|
62
|
-
const objectSchema = (0, getObjectNode_1.default)(currSchema, value);
|
|
63
|
-
if (objectSchema) {
|
|
64
|
-
const newDoc = await cleanObjectFields({
|
|
65
|
-
...info,
|
|
66
|
-
schema: objectSchema,
|
|
67
|
-
value: value
|
|
68
|
-
});
|
|
69
|
-
const result = await (0, cleanType_1.default)('plainObject', objectSchema, newDoc, info, ...args);
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
const arraySchema = getArrayNode(currSchema, value);
|
|
73
|
-
if (arraySchema) {
|
|
74
|
-
let updatedValue = value;
|
|
75
|
-
if (!(0, isArray_1.default)(value) && !Array.isArray(value)) {
|
|
76
|
-
updatedValue = [value];
|
|
77
|
-
}
|
|
78
|
-
const newDoc = await cleanArrayItems({
|
|
79
|
-
...info,
|
|
80
|
-
schema: arraySchema,
|
|
81
|
-
value: updatedValue
|
|
82
|
-
});
|
|
83
|
-
const result = await (0, cleanType_1.default)('array', arraySchema, newDoc, info, ...args);
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
const result = await (0, cleanType_1.default)(currSchema.type, currSchema, value, info, ...args);
|
|
87
|
-
return result;
|
|
88
|
-
};
|
|
89
|
-
exports.default = clean;
|
package/lib/cleanKey.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function (schema: any, key: any, value: any, passedOptions?: {}, ...args: any[]): Promise<any>;
|
package/lib/cleanKey.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const clean_1 = __importDefault(require("./clean"));
|
|
7
|
-
const dotGetSchema_1 = __importDefault(require("./dotGetSchema"));
|
|
8
|
-
const defaultOptions = {
|
|
9
|
-
filter: true
|
|
10
|
-
};
|
|
11
|
-
async function default_1(schema, key, value, passedOptions = {}, ...args) {
|
|
12
|
-
const options = { ...defaultOptions, ...passedOptions };
|
|
13
|
-
const keySchema = (0, dotGetSchema_1.default)(schema, key);
|
|
14
|
-
if (!keySchema) {
|
|
15
|
-
if (options.filter) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const result = await (0, clean_1.default)({ clean: keySchema }, { clean: value }, options, ...args);
|
|
23
|
-
return result.clean;
|
|
24
|
-
}
|
|
25
|
-
exports.default = default_1;
|
package/lib/cleanKey.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|