|
1 | 1 | import assert from 'node:assert'
|
2 | 2 |
|
3 | 3 | import {describe, expect, test} from '@jest/globals'
|
4 |
| -import {defineType} from '@sanity/types' |
| 4 | +import {defineField, defineType} from '@sanity/types' |
5 | 5 |
|
6 | 6 | import {Schema} from '../../src/legacy/Schema'
|
7 | 7 | import {extractSchema} from '../../src/sanity/extractSchema'
|
@@ -367,6 +367,76 @@ describe('Extract schema test', () => {
|
367 | 367 | ])
|
368 | 368 | })
|
369 | 369 |
|
| 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 | + |
370 | 440 | describe('Can extract sample fixtures', () => {
|
371 | 441 | const cases = Object.keys(schemaFixtures).map((schemaName) => {
|
372 | 442 | const schema = createSchema(schemaFixtures[schemaName])
|
|
0 commit comments