
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI understand your issue! You're running into a fundamental limitation with validation rules - validation rules are meant for validating data, not for modifying it. The doc object you receive in a validation rule is read-only, which is why you're getting that error when trying to assign to doc.parent.listingElements.
The good news is that what you're trying to do (populate one field based on another) is absolutely possible in Sanity, but you need to use a custom input component instead of a validation rule.
Here's the approach:
Create a custom input component that:
listingElements) using the onChange prop with proper patch eventsHere's a basic example structure:
import { set, unset } from 'sanity'
import { useEffect } from 'react'
import { useFormValue } from 'sanity'
export function ListingElementsInput(props) {
const { onChange, value } = props
// Get the value of the first field (adjust path as needed)
const referenceField = useFormValue(['yourReferenceFieldName'])
useEffect(() => {
if (referenceField) {
// Make your request here
fetchProductRefs(referenceField).then(productRefs => {
// Update the field value
onChange(set(productRefs))
})
}
}, [referenceField])
// Render your field UI (or use renderDefault if you want the standard UI)
return props.renderDefault(props)
}Then in your schema:
{
name: 'listingElements',
type: 'array', // or whatever type
components: {
input: ListingElementsInput
}
}The key differences from validation:
onChange propuseFormValue() to watch other fields in the documentCheck out this guide on creating custom input components for more details on the implementation.
Why validation rules don't work for this: Validation rules are purely for checking if data is valid - they run after changes and return errors/warnings. They're intentionally read-only to prevent side effects during validation. Custom input components are the proper extension point for conditional field population and dynamic behavior.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store