@@ -248,24 +248,32 @@ class SchemaUtils {
248
248
) ;
249
249
}
250
250
251
- const primitiveType = this . getSchemaPrimitiveType ( schema ) ;
252
-
253
- if ( primitiveType == null ) return this . config . Ts . Keyword . Any ;
254
-
255
251
let resultType ;
256
252
257
- const typeAlias =
258
- _ . get ( this . config . primitiveTypes , [ primitiveType , schema . format ] ) ||
259
- _ . get ( this . config . primitiveTypes , [ primitiveType , '$default' ] ) ||
260
- this . config . primitiveTypes [ primitiveType ] ;
261
-
262
- if ( _ . isFunction ( typeAlias ) ) {
263
- resultType = typeAlias ( schema , this ) ;
253
+ if ( this . isConstantSchema ( schema ) ) {
254
+ resultType = this . formatJsValue ( schema . const ) ;
264
255
} else {
265
- resultType = typeAlias || primitiveType ;
256
+ const primitiveType = this . getSchemaPrimitiveType ( schema ) ;
257
+
258
+ if ( primitiveType == null ) {
259
+ return this . config . Ts . Keyword . Any ;
260
+ }
261
+
262
+ const typeAlias =
263
+ _ . get ( this . config . primitiveTypes , [ primitiveType , schema . format ] ) ||
264
+ _ . get ( this . config . primitiveTypes , [ primitiveType , '$default' ] ) ||
265
+ this . config . primitiveTypes [ primitiveType ] ;
266
+
267
+ if ( _ . isFunction ( typeAlias ) ) {
268
+ resultType = typeAlias ( schema , this ) ;
269
+ } else {
270
+ resultType = typeAlias || primitiveType ;
271
+ }
266
272
}
267
273
268
- if ( ! resultType ) return this . config . Ts . Keyword . Any ;
274
+ if ( ! resultType ) {
275
+ return this . config . Ts . Keyword . Any ;
276
+ }
269
277
270
278
return this . checkAndAddRequiredKeys (
271
279
schema ,
@@ -284,6 +292,31 @@ class SchemaUtils {
284
292
) ,
285
293
) ;
286
294
} ;
295
+
296
+ isConstantSchema ( schema ) {
297
+ return 'const' in schema ;
298
+ }
299
+
300
+ formatJsValue = ( value ) => {
301
+ switch ( typeof value ) {
302
+ case 'string' : {
303
+ return this . config . Ts . StringValue ( value ) ;
304
+ }
305
+ case 'boolean' : {
306
+ return this . config . Ts . BooleanValue ( value ) ;
307
+ }
308
+ case 'number' : {
309
+ return this . config . Ts . NumberValue ( value ) ;
310
+ }
311
+ default : {
312
+ if ( value === null ) {
313
+ return this . config . Ts . NullValue ( value ) ;
314
+ }
315
+
316
+ return this . config . Ts . Keyword . Any ;
317
+ }
318
+ }
319
+ } ;
287
320
}
288
321
289
322
module . exports = {
0 commit comments