-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core):
resolveSchemaTypeForPath
updates
- Loading branch information
1 parent
9c48028
commit c9dbafe
Showing
3 changed files
with
80 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 60 additions & 37 deletions
97
packages/sanity/src/core/studio/copyPaste/resolveSchemaTypeForPath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,81 @@ | ||
import { | ||
type ArraySchemaType, | ||
isIndexSegment, | ||
isKeySegment, | ||
isReferenceSchemaType, | ||
isObjectSchemaType, | ||
type ObjectField, | ||
type ObjectSchemaType, | ||
type Path, | ||
type SchemaType, | ||
} from '@sanity/types' | ||
import {fromString, toString} from '@sanity/util/paths' | ||
import {type FormDocumentValue, getValueAtPath} from 'sanity' | ||
|
||
function getFieldTypeByName(type: SchemaType, fieldName: string): SchemaType | undefined { | ||
if (!('fields' in type)) { | ||
return undefined | ||
import {getItemType} from '../../form/store/utils/getItemType' | ||
|
||
export function getSchemaField( | ||
schemaType: SchemaType, | ||
fieldPath: string, | ||
): ObjectField<SchemaType> | undefined { | ||
if (!fieldPath) return undefined | ||
|
||
const paths = fromString(fieldPath) | ||
const firstPath = paths[0] | ||
|
||
if (firstPath && isObjectSchemaType(schemaType)) { | ||
const field = schemaType?.fields?.find((f) => f.name === firstPath) | ||
|
||
if (field) { | ||
const nextPath = toString(paths.slice(1)) | ||
|
||
if (nextPath) { | ||
return getSchemaField(field.type, nextPath) | ||
} | ||
|
||
return field | ||
} | ||
} | ||
|
||
const fieldType = type.fields.find((field) => field.name === fieldName) | ||
return fieldType ? fieldType.type : undefined | ||
return undefined | ||
} | ||
|
||
export function resolveSchemaTypeForPath(baseType: SchemaType, path: Path): SchemaType | undefined { | ||
const pathSegments = path | ||
export function resolveSchemaTypeForPath( | ||
baseType: SchemaType, | ||
path: Path, | ||
documentValue: FormDocumentValue | undefined, | ||
): SchemaType | undefined { | ||
if (!baseType) return undefined | ||
|
||
let current: SchemaType | undefined = baseType | ||
for (const segment of pathSegments) { | ||
if (!current) { | ||
return undefined | ||
} | ||
let currentField: ObjectSchemaType | ArraySchemaType<unknown> | SchemaType = baseType | ||
|
||
if (typeof segment === 'string') { | ||
current = getFieldTypeByName(current, segment) | ||
continue | ||
} | ||
path.forEach((segment, index) => { | ||
const currentPath = path.slice(0, index + 1) | ||
const isArrayItem = isKeySegment(segment) || isIndexSegment(segment) | ||
|
||
const isArrayAccessor = isKeySegment(segment) || isIndexSegment(segment) | ||
if (!isArrayAccessor || current.jsonType !== 'array') { | ||
return undefined | ||
} | ||
if (isArrayItem) { | ||
// We know that the currentField is an array schema type | ||
// if the current segment is an array item. | ||
const arraySchemaType = currentField as ArraySchemaType<unknown> | ||
|
||
const [memberType, otherType] = current.of || [] | ||
if (otherType || !memberType) { | ||
// Can't figure out the type without knowing the value | ||
return undefined | ||
} | ||
// Get the value of the array item at the current path | ||
const itemValue = getValueAtPath(documentValue, currentPath) as unknown[] | ||
|
||
if (!isReferenceSchemaType(memberType)) { | ||
current = memberType | ||
continue | ||
} | ||
// Get the schema type of the array item | ||
const item = getItemType(arraySchemaType, itemValue) | ||
|
||
const [refType, otherRefType] = memberType.to || [] | ||
if (otherRefType || !refType) { | ||
// Can't figure out the type without knowing the value | ||
return undefined | ||
if (item) { | ||
currentField = item as ObjectSchemaType | ||
|
||
return | ||
} | ||
} | ||
|
||
current = refType | ||
} | ||
const nextField = getSchemaField(currentField, toString(currentPath)) as ObjectSchemaType | ||
|
||
if (nextField.type) { | ||
currentField = nextField.type as ObjectSchemaType | ||
} | ||
}) | ||
|
||
return current | ||
return currentField | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters