@@ -53,6 +53,21 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])
5353 }
5454 }
5555
56+ let _multiType : Set < string > ;
57+ const isMultiTypeUnion = ( ) => {
58+ if ( ! info . union || info . union . length < 2 ) return false ;
59+ if ( info . union . some ( ( i ) => i . enum ) ) return true ;
60+ console . log ( info . union , info . types , _multiType ) ; //debug
61+ if ( ! _multiType ) {
62+ _multiType = new Set (
63+ info . types . map ( ( i ) => {
64+ return [ 'integer' , 'unix-time' ] . includes ( i ) ? 'number' : i ;
65+ } )
66+ ) ;
67+ }
68+ return _multiType . size > 1 ;
69+ } ;
70+
5671 // Check unions first, so default values can take precedence over nullable and optional
5772 if ( ! objectDefaults && info . union ) {
5873 const singleDefault = info . union . filter (
@@ -65,20 +80,17 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])
6580 'Only one default value can exist in a union, or set a default value for the whole union.' ,
6681 path
6782 ) ;
68- } else if ( info . union . length > 1 ) {
69- throw new SchemaError (
70- 'Unions must have a default value, or exactly one of the union types must have.' ,
71- path
72- ) ;
7383 } else {
7484 // Null takes priority over undefined
7585 if ( info . isNullable ) return null ;
7686 if ( info . isOptional ) return undefined ;
7787
78- throw new SchemaError (
79- 'Unions must have a default value, or exactly one of the union types must have.' ,
80- path
81- ) ;
88+ if ( isMultiTypeUnion ( ) ) {
89+ throw new SchemaError (
90+ 'Multi-type unions must have a default value, or exactly one of the union types must have.' ,
91+ path
92+ ) ;
93+ }
8294 }
8395 }
8496
@@ -114,7 +126,7 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])
114126 }
115127
116128 // Basic type
117- if ( info . types . length > 1 ) {
129+ if ( isMultiTypeUnion ( ) ) {
118130 throw new SchemaError ( 'Default values cannot have more than one type.' , path ) ;
119131 } else if ( info . types . length == 0 ) {
120132 //console.warn('No type or format for property:', path); //debug
0 commit comments