@@ -13,7 +13,7 @@ describe('driver', function() {
13
13
14
14
class Collection {
15
15
findOne ( filter , options ) { // eslint-disable-line no-unused-vars
16
- return Promise . resolve ( { answer : 42 } ) ;
16
+ return Promise . resolve ( { answer : 42 , date : '2023-10-01' } ) ;
17
17
}
18
18
}
19
19
class Connection extends EventEmitter {
@@ -35,18 +35,34 @@ describe('driver', function() {
35
35
const driver = {
36
36
Collection,
37
37
Connection,
38
- plugins : [ foo ]
38
+ plugins : [ foo ] ,
39
+ SchemaTypes : {
40
+ DateString : class DateString extends mongoose . SchemaType {
41
+ constructor ( key , options ) {
42
+ super ( key , options , 'DateString' ) ;
43
+ }
44
+
45
+ cast ( val ) {
46
+ if ( typeof val !== 'string' || ! / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( val ) ) {
47
+ throw new Error ( 'DateString must be in format yyyy-mm-dd' ) ;
48
+ }
49
+ return val ;
50
+ }
51
+ }
52
+ }
39
53
} ;
40
54
41
55
m . setDriver ( driver ) ;
42
56
assert . deepStrictEqual ( m . plugins . slice ( mongoose . plugins . length ) , [ [ foo , undefined ] ] ) ;
43
57
44
58
await m . connect ( ) ;
45
59
46
- const Test = m . model ( 'Test' , m . Schema ( { answer : Number } ) ) ;
60
+ const Test = m . model ( 'Test' , m . Schema ( { answer : Number , date : 'DateString' } ) ) ;
47
61
48
62
const res = await Test . findOne ( ) ;
49
- assert . deepEqual ( res . toObject ( ) , { answer : 42 } ) ;
63
+ assert . deepEqual ( res . toObject ( ) , { answer : 42 , date : '2023-10-01' } ) ;
64
+
65
+ await assert . rejects ( Test . create ( { answer : 12 , date : '2025-01' } ) , / p a t h " d a t e " / ) ;
50
66
51
67
function foo ( ) { }
52
68
} ) ;
0 commit comments