-
Notifications
You must be signed in to change notification settings - Fork 8
/
Field.spec.js
2445 lines (2186 loc) · 74.7 KB
/
Field.spec.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const uuid = require('uuid');
const Knorm = require('../lib/Knorm');
const sinon = require('sinon');
const expect = require('unexpected')
.clone()
.use(require('unexpected-sinon'))
.use(require('./lib/unexpected-workaround'));
describe('Field', function() {
let Model;
let Query;
let Field;
let sql;
before(function() {
const orm = new Knorm();
Model = orm.Model;
Query = orm.Query;
Field = orm.Field;
sql = Query.prototype.sql;
});
describe('constructor', function() {
it('throws an error if the field name is not provided', function() {
expect(() => new Field(), 'to throw', new Error('Field requires a name'));
});
it('throws an error if the model is not provided', function() {
expect(
() => new Field({ name: 'foo' }),
'to throw',
new Error('Field `foo` requires a subclass of `Model`')
);
});
it('throws an error if the field type is not provided', function() {
class Foo extends Model {}
expect(
() =>
new Field({
name: 'bar',
model: Foo
}),
'to throw',
new Error('Field `Foo.bar` has no type configured')
);
});
it('throws an error if the field type is not supported', function() {
class Foo extends Model {}
expect(
() =>
new Field({
name: 'bar',
model: Foo,
type: 'bar'
}),
'to throw',
new Error('Field `Foo.bar` has an invalid type `bar`')
);
});
it("throws an error if 'validate' is provided and is not a function", function() {
class Foo extends Model {}
expect(
() =>
new Field({
name: 'bar',
model: Foo,
type: 'string',
validate: {
oneOf: ['foo', 'bar']
}
}),
'to throw',
new Error('`validate` option for field `Foo.bar` should be a function')
);
});
describe('with a column name configured', function() {
it("sets the field's column name from configured value", function() {
class Foo extends Model {}
const field = new Field({
name: 'bar',
model: Foo,
type: 'string',
column: 'the column name'
});
expect(field.column, 'to be', 'the column name');
});
it('does not call getColumnName', function() {
class Foo extends Model {}
const spy = sinon.spy(Field.prototype, 'getColumnName');
// eslint-disable-next-line no-unused-vars
const field = new Field({
name: 'bar',
model: Foo,
type: 'string',
column: 'bar'
});
expect(spy, 'was not called');
spy.restore();
});
});
describe('without a column name configured', function() {
it("calls getColumnName to set the field's column name", function() {
class Foo extends Model {}
const stub = sinon
.stub(Field.prototype, 'getColumnName')
.returns('the column name');
const field = new Field({
name: 'bar',
model: Foo,
type: 'string'
});
expect(stub, 'to have calls satisfying', () => {
stub('bar');
});
expect(field.column, 'to be', 'the column name');
stub.restore();
});
});
describe('with `cast` options', function() {
it('throws if `cast.forSave` is not a function', function() {
class Foo extends Model {}
expect(
() =>
new Field({
name: 'bar',
model: Foo,
type: 'string',
cast: {
forSave: 'foo'
}
}),
'to throw',
new Error(
'`cast.forSave` option for field `Foo.bar` should be a function'
)
);
});
it('throws if `cast.forFetch` is not a function', function() {
class Foo extends Model {}
expect(
() =>
new Field({
name: 'bar',
model: Foo,
type: 'string',
cast: {
forFetch: 'foo'
}
}),
'to throw',
new Error(
'`cast.forFetch` option for field `Foo.bar` should be a function'
)
);
});
});
describe('for `json` and `jsonb` fields with a `shape` config', function() {
let Foo;
before(function() {
Foo = class extends Model {};
});
describe('with a root-level `shape`', function() {
it('throws the correct error if the shape config has no `type`', function() {
expect(
() =>
new Field({
name: 'json',
model: Foo,
type: 'json',
shape: { required: true }
}),
'to throw',
new Error('Field `Foo.json` has no type configured')
);
});
it('throws the correct error if a shape config has an invalid `type`', function() {
expect(
() =>
new Field({
name: 'json',
model: Foo,
type: 'json',
shape: { type: 'foo', required: true }
}),
'to throw',
new Error('Field `Foo.json` has an invalid type `foo`')
);
});
it("allows adding shape validators with the `shape: 'type'` shorthand", function() {
expect(
new Field({
name: 'json',
model: Foo,
type: 'array',
shape: 'string'
}),
'to satisfy',
{
validators: {
shape: expect.it(
'to be field',
new Field({
name: 'json',
path: 'json',
model: Foo,
type: 'string'
})
)
}
}
);
});
});
describe('with a shape with nested fields', function() {
it('throws the correct error if a nested field has no `type`', function() {
expect(
() =>
new Field({
name: 'json',
model: Foo,
type: 'json',
shape: { foo: { required: true } }
}),
'to throw',
new Error('Field `Foo.json.foo` has no type configured')
);
});
it('throws the correct error if a nested field has an invalid `type`', function() {
expect(
() =>
new Field({
name: 'json',
model: Foo,
type: 'json',
shape: { foo: { type: 'foo', required: true } }
}),
'to throw',
new Error('Field `Foo.json.foo` has an invalid type `foo`')
);
});
it("allows adding shape validators with the `shape: 'type'` shorthand", function() {
expect(
new Field({
name: 'json',
model: Foo,
type: 'json',
shape: { foo: 'string' }
}),
'to satisfy',
{
validators: {
shape: {
foo: expect.it(
'to be field',
new Field({
name: 'foo',
path: 'json.foo',
model: Foo,
type: 'string'
})
)
}
}
}
);
});
});
});
});
describe('Field.prototype.getColumnName', function() {
it('returns the field name passed as is', function() {
class Foo extends Model {}
const field = new Field({
name: 'firstName',
model: Foo,
type: 'string'
});
expect(field.getColumnName('firstName'), 'to be', 'firstName');
});
});
describe('Field.prototype.throwValidationError', function() {
it('throws a `ValidationError`', function() {
class Foo extends Model {}
const field = new Field({
name: 'firstName',
model: Foo,
type: 'string'
});
expect(() => field.throwValidationError(), 'to throw', {
name: 'ValidationError'
});
});
it('throws a validation error from `Field.ValidationError`', function() {
// allows configuring the ValidationError class entirely
class CustomValidationError {}
const ValidationError = Field.ValidationError;
Field.ValidationError = CustomValidationError;
class Foo extends Model {}
const field = new Field({
name: 'firstName',
model: Foo,
type: 'string'
});
expect(
() => field.throwValidationError(),
'to throw',
new CustomValidationError()
);
Field.ValidationError = ValidationError;
});
});
describe('Field.prototype.cast', function() {
let User;
before(function() {
User = class extends Model {};
});
describe('with no cast functions defined', function() {
it('returns undefined', function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string'
});
expect(
field.cast('bar value', 'a model instance', { forSave: true }),
'to be undefined'
);
});
});
describe('with a `forSave` cast function and the `forSave` option set to `true`', function() {
it('calls the function with the value and the model instance', function() {
const forSave = sinon.spy();
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
cast: { forSave }
});
field.cast('bar value', 'a model instance', { forSave: true });
expect(forSave, 'to have calls satisfying', () =>
forSave('bar value', 'a model instance')
);
});
it('calls the function with raw sql values', function() {
const forSave = sinon.spy();
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
cast: { forSave }
});
field.cast(sql('bar value'), 'a model instance', { forSave: true });
expect(forSave, 'was called with', sql('bar value'));
});
});
describe('with a `forFetch` cast function and the `forFetch` option set to `true`', function() {
it('calls the function with the value and the model instance', function() {
const forFetch = sinon.spy();
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
cast: { forFetch }
});
field.cast('bar value', 'a model instance', { forFetch: true });
expect(forFetch, 'to have calls satisfying', () =>
forFetch('bar value', 'a model instance')
);
});
});
describe('with both `forFetch` and `forSave` cast functions', function() {
it('calls only the `forFetch` cast function if the `forFetch` option is enabled', function() {
const forSave = sinon.spy();
const forFetch = sinon.spy();
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
cast: { forSave, forFetch }
});
field.cast('bar value', 'a model instance', { forFetch: true });
expect(forFetch, 'was called');
expect(forSave, 'was not called');
});
it('calls only the `forSave` cast function if the `forSave` option is enabled', function() {
const forSave = sinon.spy();
const forFetch = sinon.spy();
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
cast: { forSave, forFetch }
});
field.cast('bar value', 'a model instance', { forSave: true });
expect(forSave, 'was called');
expect(forFetch, 'was not called');
});
});
});
describe('Field.prototype.getDefault', function() {
let User;
before(function() {
User = class extends Model {};
});
it('returns the default value configured', function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
default: 'foo'
});
expect(field.getDefault(), 'to be', 'foo');
});
it('returns `undefined` if there is no the default value configured', function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string'
});
expect(field.getDefault(), 'to be undefined');
});
describe('when the default value is a function', function() {
it('returns the return value of the function', function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
default: () => 'bar'
});
expect(field.getDefault(), 'to be', 'bar');
});
it('calls the function with the mode instance as a parameter', function() {
const getDefault = sinon.stub().returns('bar');
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
default: getDefault
});
field.getDefault('a model instance');
expect(getDefault, 'to have calls satisfying', () => {
getDefault('a model instance');
});
});
});
});
describe('Field.prototype.validate', function() {
let User;
before(function() {
User = class extends Model {};
});
it('returns a Promise', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string'
});
await expect(field.validate(), 'to be fulfilled');
});
describe('required', function() {
it('rejects with a RequiredError if no value is passed', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
required: true
});
await expect(field.validate(), 'to be rejected with', {
name: 'ValidationError',
type: 'RequiredError'
});
});
it("rejects if the value is 'undefined'", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
required: true
});
await expect(field.validate(undefined), 'to be rejected with', {
name: 'ValidationError',
type: 'RequiredError'
});
});
it("rejects if the value is 'null'", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
required: true
});
await expect(field.validate(null), 'to be rejected with', {
name: 'ValidationError',
type: 'RequiredError'
});
});
it('resolves if the value is set', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
required: true
});
await expect(field.validate('foo'), 'to be fulfilled');
});
});
describe('type', function() {
it('rejects with a TypeError if an invalid value is set', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'text'
});
await expect(field.validate({}), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it('does not type-validate if the value is `undefined`', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'text'
});
await expect(field.validate(undefined), 'to be fulfilled');
});
it('does not type-validate if the value is `null`', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'text'
});
await expect(field.validate(null), 'to be fulfilled');
});
describe('resolves for valid types', function() {
it("any value type against the 'any' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'any'
});
await expect(field.validate('foo'), 'to be fulfilled');
await expect(field.validate(1), 'to be fulfilled');
await expect(field.validate(true), 'to be fulfilled');
await expect(field.validate(0.1), 'to be fulfilled');
await expect(field.validate(new Date()), 'to be fulfilled');
});
it("integers against the 'number' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'number'
});
await expect(field.validate(1), 'to be fulfilled');
});
it("decimals against the 'number' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'number'
});
await expect(field.validate(1.2), 'to be fulfilled');
});
it("strings against the 'string' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string'
});
await expect(field.validate('foo'), 'to be fulfilled');
});
it("strings against the 'text' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'text'
});
await expect(field.validate('foo'), 'to be fulfilled');
});
it("integers against the 'integer' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'integer'
});
await expect(field.validate(1), 'to be fulfilled');
});
it("dates against the 'dateTime' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'dateTime'
});
await expect(field.validate(new Date()), 'to be fulfilled');
});
it("dates against the 'date' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'date'
});
await expect(field.validate(new Date()), 'to be fulfilled');
});
it("true against the 'boolean' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'boolean'
});
await expect(field.validate(true), 'to be fulfilled');
});
it("false against the 'boolean' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'boolean'
});
await expect(field.validate(false), 'to be fulfilled');
});
it("uuid.v4 against the 'uuid' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'uuid'
});
await expect(field.validate(uuid.v4()), 'to be fulfilled');
});
it("uuid.v1 against the 'uuid' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'uuid'
});
await expect(field.validate(uuid.v1()), 'to be fulfilled');
});
it("uuid.v4 against the 'uuid4' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'uuid4'
});
await expect(field.validate(uuid.v4()), 'to be fulfilled');
});
it("floating point values against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate(10.56), 'to be fulfilled');
});
it("floating point values without whole numbers against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate(0.5600976), 'to be fulfilled');
});
it("integer values against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate(30), 'to be fulfilled');
});
it("zero against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate(0), 'to be fulfilled');
});
it("string floating point values against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate('10.00'), 'to be fulfilled');
});
it("positive string floating point values against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate('+10.00345'), 'to be fulfilled');
});
it("negative floating point values against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate(-9923410.03), 'to be fulfilled');
});
it("buffer values against the 'binary' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'binary'
});
await expect(field.validate(Buffer.from('')), 'to be fulfilled');
});
it("email values against the 'email' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'email'
});
await expect(field.validate('foo@bar.com'), 'to be fulfilled');
});
});
describe('rejects for invalid types', function() {
it("true against the 'number' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'number'
});
await expect(field.validate(true), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for fractions against the 'integer' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'integer'
});
await expect(field.validate(1.5), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for string numbers against the 'integer' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'integer'
});
await expect(field.validate('1'), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for date strings against the 'dateTime' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'dateTime'
});
const dateString = new Date().toString();
await expect(field.validate(dateString), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for date strings against the 'date' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'date'
});
const dateString = new Date().toString();
await expect(field.validate(dateString), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for truthy values against the 'boolean' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'boolean'
});
await expect(field.validate(1), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("for falsy values against the 'boolean' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'boolean'
});
await expect(field.validate(0), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("invalid uuid's against the 'uuid' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'uuid'
});
await expect(
field.validate('not-valid-uuid'),
'to be rejected with',
{ name: 'ValidationError', type: 'TypeError' }
);
});
it("uuid.v1 against the 'uuid4' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'uuid4'
});
const uuidV1 = uuid.v1();
await expect(field.validate(uuidV1), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("strings against the 'decimal' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'decimal'
});
await expect(field.validate('foo'), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("string values against the 'binary' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'binary'
});
await expect(field.validate('bar'), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("object values against the 'binary' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'binary'
});
await expect(field.validate({}), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("invalid email values against the 'email' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'email'
});
await expect(field.validate('foo@bar'), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
it("integer values against the 'email' type", async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'email'
});
await expect(field.validate(1), 'to be rejected with', {
name: 'ValidationError',
type: 'TypeError'
});
});
});
});
describe('minLength', function() {
it('rejects with a `MinLengthError` if the value is shorter than the minLength', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
minLength: 6
});
await expect(field.validate('a'), 'to be rejected with', {
name: 'ValidationError',
type: 'MinLengthError'
});
});
it('does not reject an if the value is the same lenth as the minLength', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
minLength: 6
});
await expect(field.validate('123456'), 'to be fulfilled');
});
it('does not reject an if the value is longer than the minLength', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
minLength: 6
});
await expect(field.validate('1234567'), 'to be fulfilled');
});
it('does not reject if the value is `undefined`', async function() {
const field = new Field({
name: 'firstName',
model: User,
type: 'string',
minLength: 6
});
await expect(field.validate(undefined), 'to be fulfilled');
});