Using locally scoped variables for objects and documents in Sanity schema files.
44 replies
Last updated: Feb 18, 2023
P
is there a way to define multiple objects and documents in the same file? without having to inform the config with new
schemaTypes. like I want to create a document type and then use that document type in the same file for a field? is there a way to compile in place the document type and use that immediately or does sanity have to do that for every single object and document type individually first? I find it annoying that I have to update a gigantic array of documents and objects before I can use them.
Feb 18, 2023, 7:09 PM
P
Feb 18, 2023, 7:13 PM
P
like this should be allowed but its not.
Feb 18, 2023, 7:14 PM
P
defineField({ name: "sectionBlocks", type: "array", title: "Content Blocks", description: "Add, and edit section blocks", of: [ { type: "reference", to: [ { title: "Section Block", type: "sectionBlock" }, { title: "Form", type: "form" }, { title: "Tag Index", type: "tagIndex" } ], }, ], }),
Feb 18, 2023, 7:28 PM
P
???
Feb 18, 2023, 7:28 PM
P
You want to reference an array of different types of references?
Feb 18, 2023, 7:29 PM
P
no
Feb 18, 2023, 7:29 PM
P
read that again and see my example
Feb 18, 2023, 7:29 PM
P
Ah... I see, you are creating multiple schema definitions in the same file.
Feb 18, 2023, 7:30 PM
P
I do keep my schema files separated so that they are easy to manage. However, I did find it annoying to keep adding references all the time to the main schema file so....
Feb 18, 2023, 7:31 PM
P
I do something like this in my main schema file....
Feb 18, 2023, 7:31 PM
P
/** * OBJECTS : Root * Re-usable objects to make life easier */ import * as objects from "./objects"; const allObjects = Object.values(objects).map((myObject) => { return { ...myObject }; });
Feb 18, 2023, 7:31 PM
P
mmmm
Feb 18, 2023, 7:32 PM
P
export default { types: [ ...allDocuments, ...allConfigs, ...allObjects, ...allPlugs, ...allTagIndexTypes, ...allBlockTypeOptions, ...allMotionAnimationOptions ], };
Feb 18, 2023, 7:32 PM
P
good idea
Feb 18, 2023, 7:32 PM
P
Note that I have an 'objects' folder where all of my object are.
Feb 18, 2023, 7:32 PM
P
I just wish I could do what I have in my example. thats how it works in literally every other schema / migration api out there.
Feb 18, 2023, 7:32 PM
P
I have an index.ts file that looks like this in my objects folder...
Feb 18, 2023, 7:32 PM
P
export { default as anchor } from './anchor' export { default as animatedBLockConfig } from './animatedBlockConfig' export { default as animatedLineConfig } from './animatedLineConfig' export { default as animationKeysNumbers } from './animationKeysNumbers'
Feb 18, 2023, 7:33 PM
P
in that example, anchor, animatedBlockConfig, etc. are all their own files
Feb 18, 2023, 7:33 PM
P
I don't so much mind having it broken down into spearately managed files. But I tend to over document everything and take DRY as far as possible.
Feb 18, 2023, 7:35 PM
P
That being said... in v3, it is just a javascript file, so technically I suppose you could do what you're suggesting if you combine my approach and yours. Isn't is just named and default exports at the end of the day?
Feb 18, 2023, 7:36 PM
P
I dont want to export the named example. I want to use that one-off non-reusable object definition in the same file. its not something that will be used in any other schema definition. Like I said sanity is the snowflake here with how it works.
Feb 18, 2023, 7:40 PM
P
hmmmm... I see what your saying more clearly now. I have to try this. brb
Feb 18, 2023, 7:41 PM
P
Ha... i got it to work.
Feb 18, 2023, 7:46 PM
P
defined an object like this...
Feb 18, 2023, 7:47 PM
P
const wtf = defineType({ name: "wtf", type: "object", title: "What the F", description: "So weird if this doesn't work", fields: [ { name: "name", title: "Name", type: "string", description: "Internal Name for Reference Onl", } ] })
Feb 18, 2023, 7:47 PM
P
then in the main schema...
Feb 18, 2023, 7:47 PM
P
fields []
Feb 18, 2023, 7:47 PM
P
fields: [ wtf, defineField({.... ]
Feb 18, 2023, 7:48 PM
P
hmm but what does that make? what if i want to use the document as an array item
Feb 18, 2023, 7:49 PM
P
hold on
Feb 18, 2023, 7:49 PM
P
let me try something
Feb 18, 2023, 7:49 PM
P
fundamentally, if you haven't added an object or document to the schema, you can't reference as if it has been.
Feb 18, 2023, 7:49 PM
P
ohhhh damn it totally does work
Feb 18, 2023, 7:50 PM
P
party
Feb 18, 2023, 7:50 PM
P
i can just do
of: [article]instead of
of: [{type: article}]
Feb 18, 2023, 7:50 PM
P
weird, I’ll be damned if any documentation about that being a possibility exists
Feb 18, 2023, 7:51 PM
P
right... because of:[{type: article}] is trying to reference something that been added to the schema. But you haven't added it so it doesn't know what you're talking about.
Feb 18, 2023, 7:51 PM
P
damn thanks! this changes literally everything for me
Feb 18, 2023, 7:51 PM
P
but of: [article] is just basically using a locally scoped variable, sort of.
Feb 18, 2023, 7:52 PM
P
Cool, glad I could help. I earned good parking Karma for the day.
Feb 18, 2023, 7:52 PM
P
I see what you did there.
Feb 18, 2023, 7:52 PM
P
another gotcha is if I want to scope this to a field with its own name I had to do
they should really document this stuff in their schema docs.
defineField({ ...brands, name: "field_name", }),
Feb 18, 2023, 8:04 PM
P
More examples would be nice. I would like more examples for creating custom components and schema builder though. I think those things are really lacking.
Feb 18, 2023, 10:46 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.