Skip to content

Commit e4f28e5

Browse files
sgulsethrexxars
andauthoredMar 26, 2024··
fix(schemaExtract): guard for list options not being an array (#6128)
* fix(schemaExtract): guard for list options not being an array * test(schema): add non-array field `list` option --------- Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
1 parent aca88e9 commit e4f28e5

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed
 

‎packages/@sanity/schema/src/sanity/extractSchema.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,11 @@ function isNumberType(typeDef: SanitySchemaType): typeDef is NumberSchemaType {
347347
function createStringTypeNodeDefintion(
348348
stringSchemaType: StringSchemaType,
349349
): StringTypeNode | UnionTypeNode<StringTypeNode> {
350-
if (stringSchemaType.options?.list) {
350+
const listOptions = stringSchemaType.options?.list
351+
if (listOptions && Array.isArray(listOptions)) {
351352
return {
352353
type: 'union',
353-
of: stringSchemaType.options.list.map((v) => ({
354+
of: listOptions.map((v) => ({
354355
type: 'string',
355356
value: typeof v === 'string' ? v : v.value,
356357
})),
@@ -364,10 +365,11 @@ function createStringTypeNodeDefintion(
364365
function createNumberTypeNodeDefintion(
365366
numberSchemaType: NumberSchemaType,
366367
): NumberTypeNode | UnionTypeNode<NumberTypeNode> {
367-
if (numberSchemaType.options?.list) {
368+
const listOptions = numberSchemaType.options?.list
369+
if (listOptions && Array.isArray(listOptions)) {
368370
return {
369371
type: 'union',
370-
of: numberSchemaType.options.list.map((v) => ({
372+
of: listOptions.map((v) => ({
371373
type: 'number',
372374
value: typeof v === 'number' ? v : v.value,
373375
})),

‎packages/@sanity/schema/test/extractSchema/extractSchema.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ describe('Extract schema test', () => {
437437
expect(book.attributes.subtitle.optional).toBe(false)
438438
})
439439

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+
440451
describe('Can extract sample fixtures', () => {
441452
const cases = Object.keys(schemaFixtures).map((schemaName) => {
442453
const schema = createSchema(schemaFixtures[schemaName])

‎packages/@sanity/schema/test/legacy/fixtures/schemas/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assets from './assets'
33
import blocks from './blocks'
44
import exampleBlog from './example-blog'
55
import fieldsets from './fieldsets'
6+
import listObjectOption from './listObjectOption'
67
import messyDevSchema from './messy-dev'
78
import oma from './oma'
89
import reference from './reference'
@@ -12,12 +13,13 @@ import vega from './vega'
1213
export default {
1314
arrays,
1415
assets,
16+
blocks,
1517
exampleBlog,
1618
fieldsets,
17-
reference,
18-
vega,
19-
blocks,
19+
listObjectOption,
20+
messyDevSchema,
2021
oma,
22+
reference,
2123
selects,
22-
messyDevSchema,
24+
vega,
2325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.