-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfake-model.ts
302 lines (291 loc) · 9.28 KB
/
fake-model.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import Model, { attr, belongsTo, hasMany, type AsyncBelongsTo, type AsyncHasMany } from '@ember-data/model';
import { modelValidator, type ValidationsConfig, type ValidatedModel } from 'ember-model-validator';
import type AsyncModel from './async-model';
import type OtherModel from './other-model';
interface FakeModel extends ValidatedModel, Model {}
@modelValidator
class FakeModel extends Model {
@attr('string') declare name: string;
@attr('string') declare login: string;
@attr('string') declare secondName: string;
@attr('string') declare email: string;
@attr('string') declare password: string;
@attr('string') declare passwordConfirmation: string;
@attr('string', { defaultValue: '[email protected]' }) declare bussinessEmail: string;
@attr('string', { defaultValue: '423abb' }) declare favoriteColor: string;
@attr('string') declare legacyCode: string;
@attr('string', { defaultValue: 'hiphopBachatudo' }) declare mainstreamCode: string;
@attr('number') declare lotteryNumber: number;
@attr('number') declare alibabaNumber: number;
@attr('number', { defaultValue: 111 }) declare anInteger: number;
@attr('number', { defaultValue: 5 }) declare anIntegerGreaterThan4: number;
@attr('number', { defaultValue: 3 }) declare anIntegerLessThan4: number;
@attr('number', { defaultValue: 7 }) declare anIntegerGreaterThanOrEqual7: number;
@attr('number', { defaultValue: 6 }) declare anIntegerLessThanOrEqual6: number;
@attr('number', { defaultValue: 10 }) declare aTenNumber: number;
@attr('number', { defaultValue: 3 }) declare anOddNumber: number;
@attr('number', { defaultValue: 2 }) declare anEvenNumber: number;
@attr('number', { defaultValue: undefined }) declare anOptionalNumber: number | null;
@attr('boolean', { defaultValue: true }) declare acceptConditions: boolean;
@attr('number', { defaultValue: 12345 }) declare socialSecurity: number;
@attr('number', { defaultValue: 1234 }) declare nsaNumber: number;
@attr('number', { defaultValue: 1234567891 }) declare chuncaluchoNumber: number;
@attr('number', { defaultValue: 3223 }) declare theMinimunmTwoNumber: number;
@attr('number', { defaultValue: 3223222222 }) declare theMinimunmInterpolatedTenNumber: number;
@attr('number', { defaultValue: 12345 }) declare hugeName: number;
@attr('string', { defaultValue: '09011' }) declare postalCodeUS: string;
@attr('string', { defaultValue: 'KY16 8BP' }) declare postalCodeUK: string;
@attr('string', { defaultValue: 'T2A2V8' }) declare postalCodeCA: string;
@attr('string', { defaultValue: '09011' }) declare postalCodeZZ: string;
@attr('string', { defaultValue: 'fake_subdomain' }) declare mySubdomain: string;
@attr('string', { defaultValue: 'http://esbanarango.com' }) declare myBlog: string;
@attr() declare thing: any;
@attr('number', { defaultValue: 12345 }) declare otherCustomValidation: number;
@attr('number', { defaultValue: 12345 }) declare otherCustomValidationBadMessageFunction: number;
@attr() declare images: any;
@attr('string') declare condType: string;
@hasMany('other-model', { async: true, inverse: 'fakeModel' }) declare otherFakes: AsyncHasMany<OtherModel>;
@belongsTo('other-model', { async: true, inverse: null }) declare otherFake: AsyncBelongsTo<OtherModel>;
@belongsTo('async-model', { async: true, inverse: 'fakeModel' }) declare asyncModel: AsyncModel;
@belongsTo('other-model', { async: true, inverse: null }) declare ifOtherFake: AsyncBelongsTo<OtherModel>;
@hasMany('other-model', { async: true, inverse: null }) declare ifOtherFakes: AsyncHasMany<OtherModel>;
@attr('date', {
defaultValue() {
return new Date();
},
})
declare date: Date;
@attr('string', { defaultValue: '2015-01-01' }) declare stringDate: string;
@attr('date', {
defaultValue() {
return new Date(2014, 7, 1);
},
})
declare dateBefore2015: Date;
@attr('date', {
defaultValue() {
return new Date(2015, 5, 3);
},
})
declare dateAfter2014: Date;
validations: ValidationsConfig = {
asyncModel: {
presence: true,
},
name: {
presence: { errorAs: 'profile.name' },
inclusion: { in: ['Jose Rene', 'Aristi Gol', 'Armani'], message: 'Solo verde a morir' },
},
images: {
presence: {
if: function (key: string, value: any, _this: FakeModel) {
return 'gallery' === _this.get('condType');
},
},
},
login: {
absence: true,
},
secondName: {
exclusion: { in: ['Gionvany Hernandez', 'Wilder Medina'], message: 'Que iNrresponsabilidad' },
},
socialSecurity: {
length: 5,
},
nsaNumber: {
length: [3, 5],
},
chuncaluchoNumber: {
length: { is: 10, message: 'this is not the length of a chuncalucho' },
},
theMinimunmTwoNumber: {
length: {
minimum: {
value: 2,
message: 'please it has to be minimum 2 come on man!!',
},
},
},
theMinimunmInterpolatedTenNumber: {
length: {
minimum: {
value: 10,
message: 'eeeche {value}',
},
},
},
hugeName: {
length: {
minimum: 3,
maximum: 5,
},
},
bussinessEmail: {
presence: { message: "sup dude, where's da email" },
email: { message: 'Be professional ma men' },
},
favoriteColor: {
color: { message: 'not a hex color' },
},
email: {
presence: true,
email: true,
},
password: {
custom: function (key: string, value: any, _this: FakeModel) {
return String(value) === String(_this.get('socialSecurity')) ? false : true;
},
match: 'passwordConfirmation',
mustContainCapital: true,
mustContainLower: true,
mustContainNumber: true,
mustContainSpecial: true,
},
thing: {
custom: [
{
validation: function (key: string, value: any) {
return value !== 'blahblahblahblahbthishouldneverfaillahblahblah';
},
},
{
validation: function (key: string, value: any) {
return value !== 'fail';
},
},
],
},
mySubdomain: {
subdomain: { reserved: ['admin', 'blog'], message: 'this subdomain is super invalid' },
},
myBlog: {
URL: true,
},
mainstreamCode: {
format: { with: /^[a-zA-Z]+$/, message: "nu nu, that's not the format" },
},
legacyCode: {
format: { with: /^[a-zA-Z]+$/ },
},
anInteger: {
numericality: { onlyInteger: true },
},
anIntegerLessThan4: {
numericality: { lessThan: 4 },
},
anIntegerGreaterThan4: {
numericality: { greaterThan: 4 },
},
anIntegerGreaterThanOrEqual7: {
numericality: { greaterThanOrEqualTo: 7 },
},
anIntegerLessThanOrEqual6: {
numericality: { lessThanOrEqualTo: 6 },
},
aTenNumber: {
numericality: { equalTo: 10 },
},
anOddNumber: {
numericality: { odd: true },
},
anEvenNumber: {
numericality: { even: true },
},
anOptionalNumber: {
numericality: { onlyInteger: true, allowBlank: true },
},
alibabaNumber: {
numericality: { message: 'is not abracadabra' },
},
lotteryNumber: {
numericality: true,
custom: {
validation: function (key: string, value: any, _this: FakeModel) {
const favColor = _this.get('favoriteColor');
return !!favColor;
},
message: 'must have a favorite color to play the lotto, duh',
},
},
acceptConditions: {
acceptance: true,
},
postalCodeUS: {
zipCode: true,
},
postalCodeUK: {
zipCode: { countryCode: 'UK' },
},
postalCodeCA: {
zipCode: { countryCode: 'CA' },
},
postalCodeZZ: {
zipCode: { countryCode: 'ZZ' },
},
otherFakes: {
relations: ['hasMany'],
},
otherFake: {
presence: true,
relations: ['belongsTo'],
},
ifOtherFake: {
relations: {
if: function (key: string, value: any, _this: FakeModel) {
return 'gallery' === _this.get('condType');
},
value: ['belongsTo'],
},
},
ifOtherFakes: {
relations: {
if: function (key: string, value: any, _this: FakeModel) {
return 'gallery' === _this.get('condType');
},
value: ['hasMany'],
},
},
otherCustomValidation: {
custom: {
validation: function (key: string, value: any) {
return value.toString().length === 5 ? true : false;
},
message: function (key: string, value: any) {
return `${key} must have exactly 5 digits, value ${value} does not`;
},
},
},
otherCustomValidationBadMessageFunction: {
custom: {
validation: function (key: string, value: any) {
return value.toString().length === 5 ? true : false;
},
message: function () {
return 12345;
},
},
},
date: {
date: true,
},
stringDate: {
date: true,
},
dateBefore2015: {
date: {
before: new Date(2015, 1, 1),
},
},
dateAfter2014: {
date: {
after: new Date(2014, 12, 31),
},
},
};
}
declare module 'ember-data/types/registries/model' {
export default interface ModelRegistry {
'fake-model': FakeModel;
}
}
export default FakeModel;