Skip to content

Commit f568e14

Browse files
authoredMar 25, 2024··
fix(schema): correctly assert optional fields with enforce required fields (#6121)
1 parent a272c71 commit f568e14

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed
 

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,15 @@ export function extractSchema(
207207
if (value === null) {
208208
continue
209209
}
210+
211+
// if we extract with enforceRequiredFields, we will mark the field as optional only if it is not a required field,
212+
// else we will always mark it as optional
213+
const optional = extractOptions.enforceRequiredFields ? fieldIsRequired === false : true
214+
210215
attributes[field.name] = {
211216
type: 'objectAttribute',
212217
value,
213-
optional: extractOptions.enforceRequiredFields ? fieldIsRequired : true,
218+
optional,
214219
}
215220
}
216221

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

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert'
22

33
import {describe, expect, test} from '@jest/globals'
4-
import {defineType} from '@sanity/types'
4+
import {defineField, defineType} from '@sanity/types'
55

66
import {Schema} from '../../src/legacy/Schema'
77
import {extractSchema} from '../../src/sanity/extractSchema'
@@ -367,6 +367,76 @@ describe('Extract schema test', () => {
367367
])
368368
})
369369

370+
test('all fields are marked as optional without "enforceRequiredFields"', () => {
371+
const schema1 = createSchema({
372+
name: 'test',
373+
types: [
374+
{
375+
title: 'Book',
376+
name: 'book',
377+
type: 'document',
378+
fields: [
379+
{
380+
title: 'Title',
381+
name: 'title',
382+
type: 'string',
383+
},
384+
defineField({
385+
title: 'Subtitle',
386+
name: 'subtitle',
387+
type: 'string',
388+
validation: (Rule) => Rule.required(),
389+
}),
390+
],
391+
},
392+
],
393+
})
394+
395+
const extracted = extractSchema(schema1, {enforceRequiredFields: false})
396+
397+
const book = extracted.find((type) => type.name === 'book')
398+
expect(book).toBeDefined()
399+
assert(book !== undefined) // this is a workaround for TS, but leave the expect above for clarity in case of failure
400+
assert(book.type === 'document') // this is a workaround for TS, but leave the expect above for clarity in case of failure
401+
expect(book.attributes.title.optional).toBe(true)
402+
expect(book.attributes.subtitle.optional).toBe(true)
403+
})
404+
405+
test('can extract with enforceRequiredFields', () => {
406+
const schema1 = createSchema({
407+
name: 'test',
408+
types: [
409+
{
410+
title: 'Book',
411+
name: 'book',
412+
type: 'document',
413+
fields: [
414+
{
415+
title: 'Title',
416+
name: 'title',
417+
type: 'string',
418+
},
419+
defineField({
420+
title: 'Subtitle',
421+
name: 'subtitle',
422+
type: 'string',
423+
validation: (Rule) => Rule.required(),
424+
}),
425+
],
426+
},
427+
],
428+
})
429+
430+
const extracted = extractSchema(schema1, {enforceRequiredFields: true})
431+
432+
const book = extracted.find((type) => type.name === 'book')
433+
expect(book).toBeDefined()
434+
assert(book !== undefined) // this is a workaround for TS, but leave the expect above for clarity in case of failure
435+
assert(book.type === 'document') // this is a workaround for TS, but leave the expect above for clarity in case of failure
436+
expect(book.attributes.title.optional).toBe(true)
437+
expect(book.attributes.subtitle.optional).toBe(false)
438+
})
439+
370440
describe('Can extract sample fixtures', () => {
371441
const cases = Object.keys(schemaFixtures).map((schemaName) => {
372442
const schema = createSchema(schemaFixtures[schemaName])

0 commit comments

Comments
 (0)
Please sign in to comment.