File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -278,6 +278,36 @@ describe('betterAjvErrors', () => {
278278 ] ) ;
279279 } ) ;
280280
281+ it ( 'should not crash when allowedValues contains null' , ( ) => {
282+ data = {
283+ str : 'str' ,
284+ enum : 'invalid' ,
285+ } ;
286+ schema = {
287+ type : 'object' ,
288+ properties : {
289+ str : { type : 'string' } ,
290+ enum : {
291+ type : [ 'string' , 'null' ] ,
292+ enum : [ 'one' , 'two' , null ] ,
293+ } ,
294+ } ,
295+ } ;
296+ ajv . validate ( schema , data ) ;
297+ const errors = betterAjvErrors ( { data, schema, errors : ajv . errors } ) ;
298+ expect ( errors ) . toEqual ( [
299+ {
300+ context : {
301+ errorType : 'enum' ,
302+ allowedValues : [ 'one' , 'two' , null ] ,
303+ } ,
304+ message : "'enum' property must be equal to one of the allowed values" ,
305+ path : '{base}.enum' ,
306+ suggestion : "Did you mean 'one'?" ,
307+ } ,
308+ ] ) ;
309+ } ) ;
310+
281311 it ( 'should not crash on null value' , ( ) => {
282312 data = {
283313 type : null ,
Original file line number Diff line number Diff line change @@ -55,7 +55,7 @@ export const betterAjvErrors = <S = any>({
5555 break ;
5656 }
5757 case 'enum' : {
58- const suggestions = error . params . allowedValues . map ( ( value ) => value . toString ( ) ) ;
58+ const suggestions = error . params . allowedValues . map ( ( value ) => String ( value ?? '' ) ) ;
5959 const prop = getLastSegment ( error . instancePath ) ;
6060 const value = safeJsonPointer ( { object : data , pnter : error . instancePath , fallback : '' } ) ;
6161 validationError = {
You can’t perform that action at this time.
0 commit comments