@bedrockio/model 0.1.0 → 0.1.1

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.
Files changed (73) hide show
  1. package/README.md +24 -0
  2. package/dist/cjs/access.js +4 -0
  3. package/dist/cjs/assign.js +2 -3
  4. package/dist/cjs/disallowed.js +40 -0
  5. package/dist/cjs/include.js +3 -2
  6. package/dist/cjs/load.js +15 -3
  7. package/dist/cjs/schema.js +29 -12
  8. package/dist/cjs/search.js +16 -16
  9. package/dist/cjs/serialization.js +2 -3
  10. package/dist/cjs/soft-delete.js +234 -55
  11. package/dist/cjs/testing.js +8 -0
  12. package/dist/cjs/validation.js +109 -43
  13. package/package.json +9 -7
  14. package/src/access.js +3 -0
  15. package/src/disallowed.js +63 -0
  16. package/src/include.js +1 -0
  17. package/src/load.js +12 -1
  18. package/src/schema.js +25 -5
  19. package/src/search.js +21 -10
  20. package/src/soft-delete.js +238 -85
  21. package/src/testing.js +7 -0
  22. package/src/validation.js +134 -43
  23. package/types/access.d.ts +7 -0
  24. package/types/access.d.ts.map +1 -0
  25. package/types/assign.d.ts +2 -0
  26. package/types/assign.d.ts.map +1 -0
  27. package/types/const.d.ts +9 -0
  28. package/types/const.d.ts.map +1 -0
  29. package/types/disallowed.d.ts +2 -0
  30. package/types/disallowed.d.ts.map +1 -0
  31. package/types/errors.d.ts +9 -0
  32. package/types/errors.d.ts.map +1 -0
  33. package/types/include.d.ts +4 -0
  34. package/types/include.d.ts.map +1 -0
  35. package/types/index.d.ts +6 -0
  36. package/types/index.d.ts.map +1 -0
  37. package/types/load.d.ts +15 -0
  38. package/types/load.d.ts.map +1 -0
  39. package/types/references.d.ts +2 -0
  40. package/types/references.d.ts.map +1 -0
  41. package/types/schema.d.ts +71 -0
  42. package/types/schema.d.ts.map +1 -0
  43. package/types/search.d.ts +303 -0
  44. package/types/search.d.ts.map +1 -0
  45. package/types/serialization.d.ts +6 -0
  46. package/types/serialization.d.ts.map +1 -0
  47. package/types/slug.d.ts +2 -0
  48. package/types/slug.d.ts.map +1 -0
  49. package/types/soft-delete.d.ts +4 -0
  50. package/types/soft-delete.d.ts.map +1 -0
  51. package/types/testing.d.ts +11 -0
  52. package/types/testing.d.ts.map +1 -0
  53. package/types/utils.d.ts +8 -0
  54. package/types/utils.d.ts.map +1 -0
  55. package/types/validation.d.ts +13 -0
  56. package/types/validation.d.ts.map +1 -0
  57. package/types/warn.d.ts +2 -0
  58. package/types/warn.d.ts.map +1 -0
  59. package/babel.config.cjs +0 -41
  60. package/jest.config.js +0 -8
  61. package/test/assign.test.js +0 -225
  62. package/test/definitions/custom-model.json +0 -9
  63. package/test/definitions/special-category.json +0 -18
  64. package/test/include.test.js +0 -896
  65. package/test/load.test.js +0 -47
  66. package/test/references.test.js +0 -71
  67. package/test/schema.test.js +0 -919
  68. package/test/search.test.js +0 -652
  69. package/test/serialization.test.js +0 -748
  70. package/test/setup.js +0 -27
  71. package/test/slug.test.js +0 -112
  72. package/test/soft-delete.test.js +0 -333
  73. package/test/validation.test.js +0 -1925
@@ -0,0 +1,303 @@
1
+ export function applySearch(schema: any, definition: any): void;
2
+ export function searchValidation(definition: any, options?: {}): {
3
+ ids: {
4
+ setup: any;
5
+ length(length: any): any;
6
+ min(length: any): any;
7
+ max(length: any): any;
8
+ latlng(): any;
9
+ assertions: any[];
10
+ meta: {};
11
+ required(): any;
12
+ default(value: any): any;
13
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
14
+ strip(strip: any): any;
15
+ allow(...set: any[]): any;
16
+ reject(...set: any[]): any;
17
+ message(message: any): any;
18
+ tag(tags: any): any;
19
+ description(description: any): any;
20
+ options(options: any): any;
21
+ validate(value: any, options?: {}): Promise<any>;
22
+ clone(meta: any): any;
23
+ append(schema: any): any;
24
+ toOpenApi(extra: any): any;
25
+ assertEnum(set: any, allow: any): any;
26
+ assert(type: any, fn: any): any;
27
+ pushAssertion(assertion: any): void;
28
+ transform(fn: any): any;
29
+ getSortIndex(type: any): number;
30
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
31
+ enumToOpenApi(): {
32
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
33
+ enum: any;
34
+ oneOf?: undefined;
35
+ } | {
36
+ oneOf: any[];
37
+ type?: undefined;
38
+ enum?: undefined;
39
+ };
40
+ };
41
+ keyword: {
42
+ min(length: number): any;
43
+ max(length: number): any;
44
+ trim(): any;
45
+ lowercase(assert?: boolean): any;
46
+ uppercase(assert?: boolean): any;
47
+ match(reg: RegExp): any;
48
+ email(): any;
49
+ phone(): any;
50
+ hex(): any;
51
+ md5(): any;
52
+ sha1(): any;
53
+ ascii(): any;
54
+ base64(options?: {
55
+ urlSafe?: boolean;
56
+ }): any;
57
+ creditCard(): any;
58
+ ip(): any;
59
+ country(): any;
60
+ locale(): any;
61
+ jwt(): any;
62
+ slug(): any;
63
+ latlng(): any;
64
+ postalCode(locale?: string): any;
65
+ password(options?: {
66
+ minLength?: number;
67
+ minNumbers?: number;
68
+ minSymbols?: number;
69
+ minLowercase?: number;
70
+ minUppercase?: number;
71
+ }): any;
72
+ url(options?: {
73
+ require_protocol?: boolean;
74
+ require_valid_protocol?: boolean;
75
+ require_host?: boolean;
76
+ require_port?: boolean;
77
+ allow_protocol_relative_urls?: boolean;
78
+ allow_fragments?: boolean;
79
+ allow_query_components?: boolean;
80
+ validate_length?: boolean;
81
+ protocols?: string[];
82
+ }): any;
83
+ domain(options?: {
84
+ require_tld?: boolean;
85
+ allow_underscores?: boolean;
86
+ allow_trailing_dot?: boolean;
87
+ allow_numeric_tld?: boolean;
88
+ allow_wildcard?: boolean;
89
+ ignore_max_length?: boolean;
90
+ }): any;
91
+ uuid(version?: 2 | 1 | 5 | 3 | 4): any;
92
+ btc(): any;
93
+ eth(): any;
94
+ swift(): any;
95
+ mongo(): any;
96
+ format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
97
+ toString(): any;
98
+ assertions: any[];
99
+ meta: {};
100
+ required(): any;
101
+ default(value: any): any;
102
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
103
+ strip(strip: any): any;
104
+ allow(...set: any[]): any;
105
+ reject(...set: any[]): any;
106
+ message(message: any): any;
107
+ tag(tags: any): any;
108
+ description(description: any): any;
109
+ options(options: any): any;
110
+ validate(value: any, options?: {}): Promise<any>;
111
+ clone(meta: any): any;
112
+ append(schema: any): any;
113
+ toOpenApi(extra: any): any;
114
+ assertEnum(set: any, allow: any): any;
115
+ assert(type: any, fn: any): any;
116
+ pushAssertion(assertion: any): void;
117
+ transform(fn: any): any;
118
+ getSortIndex(type: any): number;
119
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
120
+ enumToOpenApi(): {
121
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
122
+ enum: any;
123
+ oneOf?: undefined;
124
+ } | {
125
+ oneOf: any[];
126
+ type?: undefined;
127
+ enum?: undefined;
128
+ };
129
+ };
130
+ include: {
131
+ min(length: number): any;
132
+ max(length: number): any;
133
+ trim(): any;
134
+ lowercase(assert?: boolean): any;
135
+ uppercase(assert?: boolean): any;
136
+ match(reg: RegExp): any;
137
+ email(): any;
138
+ phone(): any;
139
+ hex(): any;
140
+ md5(): any;
141
+ sha1(): any;
142
+ ascii(): any;
143
+ base64(options?: {
144
+ urlSafe?: boolean;
145
+ }): any;
146
+ creditCard(): any;
147
+ ip(): any;
148
+ country(): any;
149
+ locale(): any;
150
+ jwt(): any;
151
+ slug(): any;
152
+ latlng(): any;
153
+ postalCode(locale?: string): any;
154
+ password(options?: {
155
+ minLength?: number;
156
+ minNumbers?: number;
157
+ minSymbols?: number;
158
+ minLowercase?: number;
159
+ minUppercase?: number;
160
+ }): any;
161
+ url(options?: {
162
+ require_protocol?: boolean;
163
+ require_valid_protocol?: boolean;
164
+ require_host?: boolean;
165
+ require_port?: boolean;
166
+ allow_protocol_relative_urls?: boolean;
167
+ allow_fragments?: boolean;
168
+ allow_query_components?: boolean;
169
+ validate_length?: boolean;
170
+ protocols?: string[];
171
+ }): any;
172
+ domain(options?: {
173
+ require_tld?: boolean;
174
+ allow_underscores?: boolean;
175
+ allow_trailing_dot?: boolean;
176
+ allow_numeric_tld?: boolean;
177
+ allow_wildcard?: boolean;
178
+ ignore_max_length?: boolean;
179
+ }): any;
180
+ uuid(version?: 2 | 1 | 5 | 3 | 4): any;
181
+ btc(): any;
182
+ eth(): any;
183
+ swift(): any;
184
+ mongo(): any;
185
+ format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
186
+ toString(): any;
187
+ assertions: any[];
188
+ meta: {};
189
+ required(): any;
190
+ default(value: any): any;
191
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
192
+ strip(strip: any): any;
193
+ allow(...set: any[]): any;
194
+ reject(...set: any[]): any;
195
+ message(message: any): any;
196
+ tag(tags: any): any;
197
+ description(description: any): any;
198
+ options(options: any): any;
199
+ validate(value: any, options?: {}): Promise<any>;
200
+ clone(meta: any): any;
201
+ append(schema: any): any;
202
+ toOpenApi(extra: any): any;
203
+ assertEnum(set: any, allow: any): any;
204
+ assert(type: any, fn: any): any;
205
+ pushAssertion(assertion: any): void;
206
+ transform(fn: any): any;
207
+ getSortIndex(type: any): number;
208
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
209
+ enumToOpenApi(): {
210
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
211
+ enum: any;
212
+ oneOf?: undefined;
213
+ } | {
214
+ oneOf: any[];
215
+ type?: undefined;
216
+ enum?: undefined;
217
+ };
218
+ };
219
+ skip: {
220
+ min(min: number, message?: string): any;
221
+ max(max: number, message?: string): any;
222
+ negative(): any;
223
+ positive(): any;
224
+ integer(): any;
225
+ multiple(multiple: any): any;
226
+ format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
227
+ toString(): any;
228
+ assertions: any[];
229
+ meta: {};
230
+ required(): any;
231
+ default(value: any): any;
232
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
233
+ strip(strip: any): any;
234
+ allow(...set: any[]): any;
235
+ reject(...set: any[]): any;
236
+ message(message: any): any;
237
+ tag(tags: any): any;
238
+ description(description: any): any;
239
+ options(options: any): any;
240
+ validate(value: any, options?: {}): Promise<any>;
241
+ clone(meta: any): any;
242
+ append(schema: any): any;
243
+ toOpenApi(extra: any): any;
244
+ assertEnum(set: any, allow: any): any;
245
+ assert(type: any, fn: any): any;
246
+ pushAssertion(assertion: any): void;
247
+ transform(fn: any): any;
248
+ getSortIndex(type: any): number;
249
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
250
+ enumToOpenApi(): {
251
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
252
+ enum: any;
253
+ oneOf?: undefined;
254
+ } | {
255
+ oneOf: any[];
256
+ type?: undefined;
257
+ enum?: undefined;
258
+ };
259
+ };
260
+ sort: import("@bedrockio/yada/types/Schema").default;
261
+ limit: {
262
+ min(min: number, message?: string): any;
263
+ max(max: number, message?: string): any;
264
+ negative(): any;
265
+ positive(): any;
266
+ integer(): any;
267
+ multiple(multiple: any): any;
268
+ format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
269
+ toString(): any;
270
+ assertions: any[];
271
+ meta: {};
272
+ required(): any;
273
+ default(value: any): any;
274
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
275
+ strip(strip: any): any;
276
+ allow(...set: any[]): any;
277
+ reject(...set: any[]): any;
278
+ message(message: any): any;
279
+ tag(tags: any): any;
280
+ description(description: any): any;
281
+ options(options: any): any;
282
+ validate(value: any, options?: {}): Promise<any>;
283
+ clone(meta: any): any;
284
+ append(schema: any): any;
285
+ toOpenApi(extra: any): any;
286
+ assertEnum(set: any, allow: any): any;
287
+ assert(type: any, fn: any): any;
288
+ pushAssertion(assertion: any): void;
289
+ transform(fn: any): any;
290
+ getSortIndex(type: any): number;
291
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
292
+ enumToOpenApi(): {
293
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
294
+ enum: any;
295
+ oneOf?: undefined;
296
+ } | {
297
+ oneOf: any[];
298
+ type?: undefined;
299
+ enum?: undefined;
300
+ };
301
+ };
302
+ };
303
+ //# sourceMappingURL=search.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAmBA,gEAyDC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwBC"}
@@ -0,0 +1,6 @@
1
+ export namespace serializeOptions {
2
+ const getters: boolean;
3
+ const versionKey: boolean;
4
+ function transform(doc: any, ret: any, options: any): void;
5
+ }
6
+ //# sourceMappingURL=serialization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../src/serialization.js"],"names":[],"mappings":";;;IASa,2DAIV"}
@@ -0,0 +1,2 @@
1
+ export function applySlug(schema: any): void;
2
+ //# sourceMappingURL=slug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slug.d.ts","sourceRoot":"","sources":["../src/slug.js"],"names":[],"mappings":"AAIA,6CAsBC"}
@@ -0,0 +1,4 @@
1
+ export function applySoftDelete(schema: any): void;
2
+ export function assertUnique(obj: any, options: any): Promise<void>;
3
+ export function hasUniqueConstraints(schema: any): boolean;
4
+ //# sourceMappingURL=soft-delete.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../src/soft-delete.js"],"names":[],"mappings":"AAAA,mDAGC;AA4PD,oEAsBC;AAgDD,2DAKC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Helper to quickly create models for testing.
3
+ * Accepts a definition's `attributes` object and
4
+ * an optional model name as the first argument.
5
+ * [Link](https://github.com/bedrockio/model#testing)
6
+ * @returns mongoose.Model
7
+ */
8
+ export function createTestModel(...args: any[]): mongoose.Model<any, unknown, unknown, unknown, any>;
9
+ export function getTestModelName(): string;
10
+ import mongoose from "mongoose";
11
+ //# sourceMappingURL=testing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.js"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,qGAiBC;AAED,2CAEC"}
@@ -0,0 +1,8 @@
1
+ export function isMongooseSchema(obj: any): boolean;
2
+ export function isReferenceField(obj: any, path: any): boolean;
3
+ export function isDateField(obj: any, path: any): boolean;
4
+ export function isNumberField(obj: any, path: any): boolean;
5
+ export function isSchemaTypedef(arg: any): boolean;
6
+ export function resolveField(obj: any, path: any): any;
7
+ export function resolveInnerField(obj: any, path: any): any;
8
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAEA,oDAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAOD,mDAGC;AAKD,uDAMC;AAID,4DAMC"}
@@ -0,0 +1,13 @@
1
+ export function addValidators(schemas: any): void;
2
+ export function applyValidation(schema: any, definition: any): void;
3
+ export function getValidationSchema(attributes: any, options?: {}): any;
4
+ export function getNamedValidator(name: any): {
5
+ (val: any): Promise<void>;
6
+ schema: any;
7
+ };
8
+ export function getTupleValidator(types: any): {
9
+ (val: any): Promise<void>;
10
+ schema: any;
11
+ };
12
+ export const OBJECT_ID_SCHEMA: import("@bedrockio/yada/types/Schema").default;
13
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA0DC;AAaD,wEAeC;AAyND;;;EAEC;AAED;;;EAKC;AAtWD,8EAUK"}
@@ -0,0 +1,2 @@
1
+ export default function warn(...lines: any[]): void;
2
+ //# sourceMappingURL=warn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"warn.d.ts","sourceRoot":"","sources":["../src/warn.js"],"names":[],"mappings":"AAEA,oDAIC"}
package/babel.config.cjs DELETED
@@ -1,41 +0,0 @@
1
- const path = require('path');
2
-
3
- const { BUILD_DIR } = process.env;
4
-
5
- module.exports = {
6
- presets: [
7
- [
8
- '@babel/preset-env',
9
- {
10
- targets: {
11
- node: 'current',
12
- },
13
- },
14
- ],
15
- ],
16
- plugins: [
17
- 'lodash',
18
- ...(BUILD_DIR
19
- ? [
20
- [
21
- 'import-replacement',
22
- {
23
- rules: [
24
- {
25
- match: 'mongoose',
26
- replacement: path.resolve(BUILD_DIR, 'node_modules/mongoose'),
27
- },
28
- {
29
- match: '@bedrockio/yada',
30
- replacement: path.resolve(
31
- BUILD_DIR,
32
- 'node_modules/@bedrockio/yada'
33
- ),
34
- },
35
- ],
36
- },
37
- ],
38
- ]
39
- : []),
40
- ],
41
- };
package/jest.config.js DELETED
@@ -1,8 +0,0 @@
1
- process.env.ENV_NAME = 'test';
2
-
3
- export default {
4
- preset: '@shelf/jest-mongodb',
5
- setupFilesAfterEnv: ['<rootDir>/test/setup'],
6
- // https://github.com/shelfio/jest-mongodb#6-jest-watch-mode-gotcha
7
- watchPathIgnorePatterns: ['globalConfig'],
8
- };
@@ -1,225 +0,0 @@
1
- import mongoose from 'mongoose';
2
-
3
- import { createTestModel } from '../src/testing';
4
-
5
- describe('assign', () => {
6
- it('should allow assignment of fields', async () => {
7
- const User = createTestModel({
8
- date: 'Date',
9
- name: 'String',
10
- number: 'Number',
11
- });
12
- const user = new User();
13
- const now = Date.now();
14
- user.assign({
15
- name: 'fake name',
16
- number: 5,
17
- date: new Date(now),
18
- });
19
- expect(user.name).toBe('fake name');
20
- expect(user.number).toBe(5);
21
- expect(user.date.getTime()).toBe(now);
22
- });
23
-
24
- it('should delete falsy values for reference fields', async () => {
25
- const User = createTestModel();
26
- const Shop = createTestModel({
27
- user: {
28
- ref: User.modelName,
29
- type: mongoose.Schema.Types.ObjectId,
30
- },
31
- nested: {
32
- name: 'String',
33
- user: {
34
- ref: User.modelName,
35
- type: mongoose.Schema.Types.ObjectId,
36
- },
37
- },
38
- });
39
- const id = mongoose.Types.ObjectId().toString();
40
- const shop = new Shop({
41
- user: id,
42
- nested: {
43
- name: 'fake',
44
- user: id,
45
- },
46
- });
47
- await shop.save();
48
-
49
- let data = JSON.parse(JSON.stringify(shop));
50
- expect(data).toMatchObject({
51
- user: id,
52
- nested: {
53
- name: 'fake',
54
- user: id,
55
- },
56
- });
57
- shop.assign({
58
- user: '',
59
- nested: {
60
- user: '',
61
- },
62
- });
63
- await shop.save();
64
-
65
- data = JSON.parse(JSON.stringify(shop));
66
- expect(data.user).toBeUndefined();
67
- expect(data.nested).toEqual({
68
- name: 'fake',
69
- });
70
- });
71
-
72
- it('should still allow assignment of empty arrays for multi-reference fields', async () => {
73
- const User = createTestModel();
74
- const Shop = createTestModel({
75
- users: [
76
- {
77
- ref: User.modelName,
78
- type: mongoose.Schema.Types.ObjectId,
79
- },
80
- ],
81
- });
82
- const shop = new Shop({
83
- users: ['5f63b1b88f09266f237e9d29', '5f63b1b88f09266f237e9d29'],
84
- });
85
- await shop.save();
86
-
87
- let data = JSON.parse(JSON.stringify(shop));
88
- expect(data.users).toEqual([
89
- '5f63b1b88f09266f237e9d29',
90
- '5f63b1b88f09266f237e9d29',
91
- ]);
92
- shop.assign({
93
- users: [],
94
- });
95
- await shop.save();
96
- data = JSON.parse(JSON.stringify(shop));
97
- expect(data.users).toEqual([]);
98
- });
99
-
100
- it('should allow partial assignment of nested fields', async () => {
101
- const User = createTestModel({
102
- profile: {
103
- firstName: 'String',
104
- lastName: 'String',
105
- },
106
- });
107
-
108
- const user = await User.create({
109
- profile: {
110
- firstName: 'John',
111
- lastName: 'Doe',
112
- },
113
- });
114
-
115
- user.assign({
116
- profile: {
117
- firstName: 'Jane',
118
- },
119
- });
120
- await user.save();
121
-
122
- expect(user.profile.firstName).toEqual('Jane');
123
- expect(user.profile.lastName).toEqual('Doe');
124
- });
125
-
126
- it('should naively set nested array fields', async () => {
127
- const Shop = createTestModel({
128
- products: [
129
- {
130
- name: 'String',
131
- },
132
- ],
133
- });
134
- const shop = await Shop.create({
135
- products: [
136
- {
137
- name: 'shampoo',
138
- },
139
- ],
140
- });
141
-
142
- shop.assign({
143
- products: [
144
- {
145
- name: 'conditioner',
146
- },
147
- {
148
- name: 'body wash',
149
- },
150
- ],
151
- });
152
- await shop.save();
153
-
154
- expect(shop.products[0].name).toBe('conditioner');
155
- expect(shop.products[1].name).toBe('body wash');
156
- });
157
-
158
- it('should not overwrite mixed content fields', async () => {
159
- const User = createTestModel({
160
- profile: 'Object',
161
- });
162
-
163
- const user = await User.create({
164
- profile: {
165
- foo: 'foo',
166
- },
167
- });
168
-
169
- user.assign({
170
- profile: {
171
- bar: 'bar',
172
- },
173
- });
174
- await user.save();
175
-
176
- expect(user.profile).toEqual({
177
- foo: 'foo',
178
- bar: 'bar',
179
- });
180
- });
181
-
182
- it('should delete mixed content fields with null', async () => {
183
- const User = createTestModel({
184
- profile: 'Object',
185
- });
186
-
187
- let user = await User.create({
188
- profile: {
189
- name: 'Bob',
190
- age: 30,
191
- },
192
- });
193
-
194
- user.assign({
195
- profile: {
196
- age: null,
197
- },
198
- });
199
- await user.save();
200
-
201
- user = await User.findById(user.id);
202
-
203
- expect(user.profile.name).toBe('Bob');
204
- expect('age' in user.profile).toBe(false);
205
- });
206
-
207
- it('should allow 0 to be set on number fields', async () => {
208
- const User = createTestModel({
209
- age: 'Number',
210
- });
211
-
212
- let user = await User.create({
213
- age: 30,
214
- });
215
-
216
- user.assign({
217
- age: 0,
218
- });
219
- await user.save();
220
-
221
- user = await User.findById(user.id);
222
-
223
- expect(user.age).toBe(0);
224
- });
225
- });
@@ -1,9 +0,0 @@
1
- {
2
- "attributes": {
3
- "name": {
4
- "type": "String",
5
- "trim": true,
6
- "required": true
7
- }
8
- }
9
- }
@@ -1,18 +0,0 @@
1
- {
2
- "attributes": {
3
- "name": {
4
- "type": "String",
5
- "trim": true,
6
- "required": true
7
- },
8
- "count": {
9
- "type": "Number",
10
- "required": true
11
- },
12
- "someRef": {
13
- "type": "ObjectId",
14
- "ref": "User",
15
- "required": true
16
- }
17
- }
18
- }