File tree 4 files changed +53
-8
lines changed
4 files changed +53
-8
lines changed Original file line number Diff line number Diff line change @@ -347,10 +347,11 @@ function isNumberType(typeDef: SanitySchemaType): typeDef is NumberSchemaType {
347
347
function createStringTypeNodeDefintion (
348
348
stringSchemaType : StringSchemaType ,
349
349
) : StringTypeNode | UnionTypeNode < StringTypeNode > {
350
- if ( stringSchemaType . options ?. list ) {
350
+ const listOptions = stringSchemaType . options ?. list
351
+ if ( listOptions && Array . isArray ( listOptions ) ) {
351
352
return {
352
353
type : 'union' ,
353
- of : stringSchemaType . options . list . map ( ( v ) => ( {
354
+ of : listOptions . map ( ( v ) => ( {
354
355
type : 'string' ,
355
356
value : typeof v === 'string' ? v : v . value ,
356
357
} ) ) ,
@@ -364,10 +365,11 @@ function createStringTypeNodeDefintion(
364
365
function createNumberTypeNodeDefintion (
365
366
numberSchemaType : NumberSchemaType ,
366
367
) : NumberTypeNode | UnionTypeNode < NumberTypeNode > {
367
- if ( numberSchemaType . options ?. list ) {
368
+ const listOptions = numberSchemaType . options ?. list
369
+ if ( listOptions && Array . isArray ( listOptions ) ) {
368
370
return {
369
371
type : 'union' ,
370
- of : numberSchemaType . options . list . map ( ( v ) => ( {
372
+ of : listOptions . map ( ( v ) => ( {
371
373
type : 'number' ,
372
374
value : typeof v === 'number' ? v : v . value ,
373
375
} ) ) ,
Original file line number Diff line number Diff line change @@ -437,6 +437,17 @@ describe('Extract schema test', () => {
437
437
expect ( book . attributes . subtitle . optional ) . toBe ( false )
438
438
} )
439
439
440
+ describe ( 'can handle `list` option that is not an array' , ( ) => {
441
+ const schema = createSchema ( schemaFixtures . listObjectOption )
442
+ const extracted = extractSchema ( schema )
443
+
444
+ const post = extracted . find ( ( type ) => type . name === 'post' )
445
+ assert ( post !== undefined ) // this is a workaround for TS, but leave the expect above for clarity in case of failure
446
+ assert ( post . type === 'document' ) // this is a workaround for TS, but leave the expect above for clarity in case of failure
447
+
448
+ expect ( post . attributes . align . value . type ) . toBe ( 'string' )
449
+ } )
450
+
440
451
describe ( 'Can extract sample fixtures' , ( ) => {
441
452
const cases = Object . keys ( schemaFixtures ) . map ( ( schemaName ) => {
442
453
const schema = createSchema ( schemaFixtures [ schemaName ] )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import assets from './assets'
3
3
import blocks from './blocks'
4
4
import exampleBlog from './example-blog'
5
5
import fieldsets from './fieldsets'
6
+ import listObjectOption from './listObjectOption'
6
7
import messyDevSchema from './messy-dev'
7
8
import oma from './oma'
8
9
import reference from './reference'
@@ -12,12 +13,13 @@ import vega from './vega'
12
13
export default {
13
14
arrays,
14
15
assets,
16
+ blocks,
15
17
exampleBlog,
16
18
fieldsets,
17
- reference,
18
- vega,
19
- blocks,
19
+ listObjectOption,
20
+ messyDevSchema,
20
21
oma,
22
+ reference,
21
23
selects,
22
- messyDevSchema ,
24
+ vega ,
23
25
}
Original file line number Diff line number Diff line change
1
+ export default {
2
+ name : 'listObjectOption' ,
3
+ types : [
4
+ {
5
+ name : 'stringWithListOption' ,
6
+ type : 'string' ,
7
+ } ,
8
+ {
9
+ name : 'post' ,
10
+ type : 'document' ,
11
+ fields : [
12
+ {
13
+ name : 'title' ,
14
+ title : 'Title' ,
15
+ type : 'string' ,
16
+ } ,
17
+ {
18
+ name : 'align' ,
19
+ title : 'Alignment' ,
20
+ type : 'stringWithListOption' ,
21
+ options : {
22
+ list : {
23
+ options : [ 'left' , 'right' , 'center' ] ,
24
+ } ,
25
+ } ,
26
+ } ,
27
+ ] ,
28
+ } ,
29
+ ] ,
30
+ }
You can’t perform that action at this time.
0 commit comments