Skip to content

Commit 4926b78

Browse files
authoredMar 1, 2024
fix(structure): provide better error handling if orderings contain invalid field (#5709)
* fix(structure): provide better error handling if orderings contain invalid field * chore: fixed lint issue * chore: fixed lint issue again * fix(structure): provide better error handling if orderings contain invalid field * fix(structure): provide better error handling if orderings contain invalid field * fix(structure): add comment explaining why we are calling this method
1 parent 9ed5cca commit 4926b78

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed
 

‎packages/sanity/src/structure/panes/documentList/listenSearchQuery.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from 'sanity'
2424
import {getSearchableTypes, getSearchTypesWithMaxDepth} from 'sanity/_internalBrowser'
2525

26+
import {getExtendedProjection} from '../../structureBuilder/util/getExtendedProjection'
2627
import {type SortOrder} from './types'
2728

2829
interface ListenQueryOptions {
@@ -90,7 +91,14 @@ export function listenSearchQuery(options: ListenQueryOptions): Observable<Sanit
9091
mergeMap((typeNames: string[]) => {
9192
const types = getSearchTypesWithMaxDepth(
9293
getSearchableTypes(schema).filter((type) => {
93-
return typeNames.includes(type.name)
94+
if (typeNames.includes(type.name)) {
95+
// make a call to getExtendedProjection in strict mode to verify that all fields are
96+
// known. This method will throw an exception if there are any unknown fields specified
97+
// in the sort by list
98+
getExtendedProjection(type, sort.by, true)
99+
return true
100+
}
101+
return false
94102
}),
95103
maxFieldDepth,
96104
)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {type SchemaType, type SortOrderingItem} from '@sanity/types'
22

3-
const IMPLICIT_FIELDS = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
3+
const IMPLICIT_SCHEMA_TYPE_FIELDS = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
44

55
// Takes a path array and a schema type and builds a GROQ join every time it enters a reference field
6-
function joinReferences(schemaType: SchemaType, path: string[]): string {
6+
function joinReferences(schemaType: SchemaType, path: string[], strict: boolean = false): string {
77
const [head, ...tail] = path
88

99
if (!('fields' in schemaType)) {
@@ -12,14 +12,14 @@ function joinReferences(schemaType: SchemaType, path: string[]): string {
1212

1313
const schemaField = schemaType.fields.find((field) => field.name === head)
1414
if (!schemaField) {
15-
if (!IMPLICIT_FIELDS.includes(head)) {
16-
// eslint-disable-next-line no-console
17-
console.warn(
18-
'The current ordering config targeted the nonexistent field "%s" on schema type "%s". It should be one of %o',
19-
head,
20-
schemaType.name,
21-
schemaType.fields.map((field) => field.name),
22-
)
15+
if (!IMPLICIT_SCHEMA_TYPE_FIELDS.includes(head)) {
16+
const errorMessage = `The current ordering config targeted the nonexistent field "${head}" on schema type "${schemaType.name}". It should be one of ${schemaType.fields.map((field) => field.name).join(', ')}`
17+
if (strict) {
18+
throw new Error(errorMessage)
19+
} else {
20+
// eslint-disable-next-line no-console
21+
console.warn(errorMessage)
22+
}
2323
}
2424
return ''
2525
}
@@ -34,6 +34,12 @@ function joinReferences(schemaType: SchemaType, path: string[]): string {
3434
return tail.length > 0 ? `${head}${tailWrapper}` : head
3535
}
3636

37-
export function getExtendedProjection(schemaType: SchemaType, orderBy: SortOrderingItem[]): string {
38-
return orderBy.map((ordering) => joinReferences(schemaType, ordering.field.split('.'))).join(', ')
37+
export function getExtendedProjection(
38+
schemaType: SchemaType,
39+
orderBy: SortOrderingItem[],
40+
strict: boolean = false,
41+
): string {
42+
return orderBy
43+
.map((ordering) => joinReferences(schemaType, ordering.field.split('.'), strict))
44+
.join(', ')
3945
}

0 commit comments

Comments
 (0)
Please sign in to comment.