@sanity/validation
Version:
Validation and warning infrastructure for Sanity projects
22 lines (17 loc) • 597 B
text/typescript
import {Path, isKeyedObject} from '@sanity/types'
export default function pathToString(path: Path | undefined = []): string {
return path.reduce<string>((target, segment, i) => {
const segmentType = typeof segment
if (segmentType === 'number') {
return `${target}[${segment}]`
}
if (segmentType === 'string') {
const separator = i === 0 ? '' : '.'
return `${target}${separator}${segment}`
}
if (isKeyedObject(segment)) {
return `${target}[_key=="${segment._key}"]`
}
throw new Error(`Unsupported path segment "${segment}"`)
}, '')
}