Sanity Schema Mapper
AI-generatedOverview
This node transforms incoming data into a valid Sanity document based on a provided Sanity schema. It is useful when you want to prepare or reshape data before sending it to Sanity CMS, ensuring the data matches the expected schema structure.
Typical use cases include:
- Mapping raw input data fields to a Sanity document format for content ingestion.
- Automating content creation workflows by converting various data sources into Sanity-compatible documents.
- Preparing complex nested or typed fields (like slugs, references, images) according to Sanity's schema requirements.
For example, if you receive JSON data from an API or webhook, you can map its fields to your Sanity schema fields so that the output can be directly used in Sanity’s APIs or further processing nodes.
Properties
| Name | Meaning |
|---|---|
| Sanity Document Schema | The JSON schema of your Sanity document type. This defines the structure and field types of the document. Paste the schema as found in your Sanity project schema files. Must be a valid JSON object with name and fields properties. |
| Field Mappings | A collection of mapping rules where each rule maps an incoming data value to a specific field path in the Sanity document. Each mapping includes: - Sanity Field Path: The target field path in the Sanity document (e.g., title, slug.current, author). - Input Value: The value or expression from the incoming data to assign to the Sanity field. |
Output
The node outputs an array of JSON objects, each representing a Sanity document structured according to the provided schema and mappings. Each output item has the following structure:
{
"_type": "documentTypeName",
"field1": "value1",
"nestedField": {
"subField": "value2"
},
...
}
Special field types are handled accordingly:
- slug fields become objects like
{ "_type": "slug", "current": "value" }. - reference fields become
{ "_type": "reference", "_ref": "value" }. - image and file fields become objects referencing assets.
- portableText fields are converted into arrays of block objects suitable for rich text content.
No binary data output is produced by this node.
Dependencies
- Requires no external services or API keys.
- Depends on the user providing a valid Sanity document schema JSON.
- Uses lodash's
setfunction internally to set nested values in the output document.
Troubleshooting
Invalid Sanity Schema JSON provided.
This error occurs if the schema JSON cannot be parsed. Ensure the schema is valid JSON and correctly formatted.Schema must be a valid document schema object with "name" and "fields" properties.
The schema must be a Sanity document schema object containing at least anamestring and afieldsarray. Verify your schema matches this structure.Incorrect field paths in mappings.
If mapped field paths do not exist in the schema or are mistyped, the output may not match expectations. Double-check the field paths against your Sanity schema.Unsupported field types or unexpected input values.
The node supports common Sanity field types like slug, reference, image, file, and portableText. Custom or complex types might require additional handling outside this node.
Links and References
- Sanity.io Schema Documentation
- Sanity Portable Text Guide
- Lodash set method (used internally for setting nested fields)