@@ -97,6 +97,56 @@ describe('schema', function() {
9797 await Test . path ( 'state' ) . doValidate ( 'open' ) ;
9898 } ) ;
9999
100+ it ( 'number enum' , async function ( ) {
101+ const Test = new Schema ( {
102+ status : { type : Number , enum : [ 1 , 2 , 3 , null ] } ,
103+ priority : { type : Number }
104+ } ) ;
105+
106+ assert . ok ( Test . path ( 'status' ) instanceof SchemaTypes . Number ) ;
107+ assert . deepEqual ( Test . path ( 'status' ) . enumValues , [ 1 , 2 , 3 , null ] ) ;
108+ assert . equal ( Test . path ( 'status' ) . validators . length , 1 ) ;
109+
110+ Test . path ( 'status' ) . enum ( 4 , 5 ) ;
111+
112+ assert . deepEqual ( Test . path ( 'status' ) . enumValues , [ 1 , 2 , 3 , null , 4 , 5 ] ) ;
113+
114+ // with SchemaTypes validate method
115+ Test . path ( 'priority' ) . enum ( {
116+ values : [ 10 , 20 , 30 ] ,
117+ message : 'enum validator failed for path `{PATH}`: test'
118+ } ) ;
119+
120+ assert . equal ( Test . path ( 'priority' ) . validators . length , 1 ) ;
121+ assert . deepEqual ( Test . path ( 'priority' ) . enumValues , [ 10 , 20 , 30 ] ) ;
122+
123+ await assert . rejects ( Test . path ( 'status' ) . doValidate ( 6 ) , ValidatorError ) ;
124+
125+ // allow unsetting enums
126+ await Test . path ( 'status' ) . doValidate ( undefined ) ;
127+
128+ await Test . path ( 'status' ) . doValidate ( null ) ;
129+
130+ await assert . rejects (
131+ Test . path ( 'status' ) . doValidate ( 99 ) ,
132+ ValidatorError
133+ ) ;
134+
135+ await assert . rejects (
136+ Test . path ( 'priority' ) . doValidate ( 40 ) ,
137+ err => {
138+ assert . ok ( err instanceof ValidatorError ) ;
139+ assert . equal ( err . message ,
140+ 'enum validator failed for path `priority`: test' ) ;
141+ return true ;
142+ }
143+ ) ;
144+
145+ await Test . path ( 'status' ) . doValidate ( 1 ) ;
146+
147+ await Test . path ( 'status' ) . doValidate ( 2 ) ;
148+ } ) ;
149+
100150 it ( 'string regexp' , async function ( ) {
101151 const Test = new Schema ( {
102152 simple : { type : String , match : / [ a - z ] / }
0 commit comments