Skip to content

Commit

Permalink
fix(schemaExtract): guard for list options not being an array (#6128)
Browse files Browse the repository at this point in the history
* fix(schemaExtract): guard for list options not being an array

* test(schema): add non-array field `list` option

---------

Co-authored-by: Espen Hovlandsdal <[email protected]>
  • Loading branch information
sgulseth and rexxars authored Mar 26, 2024
1 parent aca88e9 commit e4f28e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/@sanity/schema/src/sanity/extractSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,11 @@ function isNumberType(typeDef: SanitySchemaType): typeDef is NumberSchemaType {
function createStringTypeNodeDefintion(
stringSchemaType: StringSchemaType,
): StringTypeNode | UnionTypeNode<StringTypeNode> {
if (stringSchemaType.options?.list) {
const listOptions = stringSchemaType.options?.list
if (listOptions && Array.isArray(listOptions)) {
return {
type: 'union',
of: stringSchemaType.options.list.map((v) => ({
of: listOptions.map((v) => ({
type: 'string',
value: typeof v === 'string' ? v : v.value,
})),
Expand All @@ -364,10 +365,11 @@ function createStringTypeNodeDefintion(
function createNumberTypeNodeDefintion(
numberSchemaType: NumberSchemaType,
): NumberTypeNode | UnionTypeNode<NumberTypeNode> {
if (numberSchemaType.options?.list) {
const listOptions = numberSchemaType.options?.list
if (listOptions && Array.isArray(listOptions)) {
return {
type: 'union',
of: numberSchemaType.options.list.map((v) => ({
of: listOptions.map((v) => ({
type: 'number',
value: typeof v === 'number' ? v : v.value,
})),
Expand Down
11 changes: 11 additions & 0 deletions packages/@sanity/schema/test/extractSchema/extractSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ describe('Extract schema test', () => {
expect(book.attributes.subtitle.optional).toBe(false)
})

describe('can handle `list` option that is not an array', () => {
const schema = createSchema(schemaFixtures.listObjectOption)
const extracted = extractSchema(schema)

const post = extracted.find((type) => type.name === 'post')
assert(post !== undefined) // this is a workaround for TS, but leave the expect above for clarity in case of failure
assert(post.type === 'document') // this is a workaround for TS, but leave the expect above for clarity in case of failure

expect(post.attributes.align.value.type).toBe('string')
})

describe('Can extract sample fixtures', () => {
const cases = Object.keys(schemaFixtures).map((schemaName) => {
const schema = createSchema(schemaFixtures[schemaName])
Expand Down
10 changes: 6 additions & 4 deletions packages/@sanity/schema/test/legacy/fixtures/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assets from './assets'
import blocks from './blocks'
import exampleBlog from './example-blog'
import fieldsets from './fieldsets'
import listObjectOption from './listObjectOption'
import messyDevSchema from './messy-dev'
import oma from './oma'
import reference from './reference'
Expand All @@ -12,12 +13,13 @@ import vega from './vega'
export default {
arrays,
assets,
blocks,
exampleBlog,
fieldsets,
reference,
vega,
blocks,
listObjectOption,
messyDevSchema,
oma,
reference,
selects,
messyDevSchema,
vega,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default {
name: 'listObjectOption',
types: [
{
name: 'stringWithListOption',
type: 'string',
},
{
name: 'post',
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
},
{
name: 'align',
title: 'Alignment',
type: 'stringWithListOption',
options: {
list: {
options: ['left', 'right', 'center'],
},
},
},
],
},
],
}

0 comments on commit e4f28e5

Please sign in to comment.