Sanity V2 schema registry package
Takes your Sanity Schema and makes it usable in JS as a nice tidy package.
Takes your Sanity Schema...
// /studio/schema/documents/templates/schemaPage.js
export const schemaPage = {
name: 'page',
title: 'Page',
type: 'document',
groups: [
{
name: 'content',
title: 'Content',
default: true,
},
{
name: 'excerpt',
title: 'Excerpt',
default: false,
},
{
name: 'meta',
title: 'Meta',
default: false,
},
{
name: 'seo',
title: 'SEO',
default: false,
},
],
fieldsets: [
{
name: 'seo',
title: 'SEO',
options: {
collapsible: true,
collapsed: false,
},
},
{
name: 'meta',
title: 'Meta',
options: {
collapsible: true,
collapsed: false,
},
},
],
fields: [
{
name: 'title',
title: 'Title',
group: 'content',
type: 'string',
},
{
name: 'slug',
title: 'Slug',
description: 'Leave blank to autofill.',
options: {
source: 'title',
maxLength: 96,
minLength: 4,
},
group: 'meta',
type: 'slug',
},
{
name: 'parent',
title: 'Parent Page',
to: [
{
type: 'page',
},
],
group: 'meta',
description:
"Set parent page for nested URL structures. Path will prepend parent page's path.",
type: 'reference',
},
{
name: 'path',
title: 'Path',
readOnly: true,
group: 'meta',
description: 'Automatically updates on Publish.',
type: 'string',
},
{
name: 'hero',
title: 'Hero',
type: 'hero',
group: 'content',
},
{
name: 'components',
title: 'Components',
type: 'components',
group: 'content',
},
{
name: 'cta',
title: 'Call to Action',
to: [
{
type: 'cta',
},
],
description: 'Leave blank to omit page call to action.',
type: 'reference',
group: 'content',
},
{
name: 'seo',
title: ' ',
fieldset: 'seo',
type: 'seo',
group: 'seo',
},
],
preview: {
select: {
title: 'title',
media: 'image',
},
},
};
// /studio/schema/documents/templates/schemaPage.js
import { AiOutlineFileText as icon } from 'react-icons/ai';
import { F, FS, G, P } from 'part:gearbox-schema-tool/schema-builder';
export const schemaPage = {
icon,
name: 'page',
title: 'Page',
type: 'document',
groups: [...G.fieldGroupDefaults(), G.fieldGroup('seo', { title: 'SEO' })],
fieldsets: [FS.seo(), FS.fieldset('meta', { collapsed: false })],
fields: [
...F.fieldDefaults(),
...G.group('content', [
F.field('hero'),
F.field('components', {}),
F.reference('cta', {
name: 'cta',
title: 'Call to Action',
description: 'Leave blank to omit page call to action.',
}),
]),
...G.group('seo', [F.seo()]),
],
preview: P.titleImage({ subtitle: 'parent.title' }),
};
...and makes the Sanity schema registry available as a package.
getSchemaRegistry
Get schema from the registry by type.
// Type getter
import { getSchemaRegistry } from 'schema-registry';
const schema = getSchemaRegistry('page');
default
Get the entire schema registry.
// Entire schema (BIG!!)
import schemaRegistry from 'schema-registry';
const registry = schemaRegistry;
Top level schema registry properties.
{
jsonType: 'object',
type: {},
name: 'page',
fields: [],
icon: undefined,
title: 'Page',
groups: [],
fieldsets: [],
preview: {},
options: {},
orderings: [],
validation: []
}
Property | Description |
---|---|
jsonType | "object" | "array" | "boolean" | "string" | "number" | "any" |
type | Object mapping to schema/Sanity types. ie. document , string , array , etc. |
name | The value of thename field as defined in your schema |
fields | An array of field types as defined in your schema |
icon | "undefined" |
title | The value of thetitle field as defined in your schema |
groups | The value of thegroups definitions in your schema |
fieldsets | The value of thefieldsets definitions in your schema |
preview | The value of thepreview definitions in your schema |
options | The value of theoptions definitions in your schema |
orderings | The value of theorderings definitions in your schema |
validation | The value of thevalidation definition in your schema |
{
jsonType: 'object',
type: {
name: 'document',
type: null,
jsonType: 'object',
validation: [ [Rule] ]
},
name: 'page',
fields: [
{
name: 'title',
group: 'content',
fieldset: undefined,
type: [Object]
},
{
name: 'slug',
group: 'meta',
fieldset: undefined,
type: [Object]
},
{
name: 'parent',
group: 'meta',
fieldset: undefined,
type: [Object]
},
{
name: 'path',
group: 'meta',
fieldset: undefined,
type: [Object]
},
{
name: 'hero',
group: 'content',
fieldset: undefined,
type: [Object]
},
{
name: 'components',
group: 'content',
fieldset: undefined,
type: [Object]
},
{
name: 'cta',
group: 'content',
fieldset: undefined,
type: [Object]
},
{ name: 'seo', group: 'seo', fieldset: 'seo', type: [Object] },
{
name: 'redirectToLegacy',
group: 'options',
fieldset: undefined,
type: [Object]
},
{
name: 'options',
group: 'options',
fieldset: undefined,
type: [Object]
}
],
icon: undefined,
title: 'Page',
groups: [
{
name: 'content',
title: 'Content',
description: undefined,
icon: undefined,
readOnly: undefined,
default: true,
hidden: undefined,
fields: [Array]
},
{
name: 'meta',
title: 'Meta',
description: undefined,
icon: undefined,
readOnly: undefined,
default: false,
hidden: undefined,
fields: [Array]
},
{
name: 'seo',
title: 'SEO',
description: undefined,
icon: undefined,
readOnly: undefined,
default: false,
hidden: undefined,
fields: [Array]
},
{
name: 'options',
title: 'Page Options',
description: undefined,
icon: undefined,
readOnly: undefined,
default: false,
hidden: undefined,
fields: [Array]
}
],
fieldsets: [
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{ single: true, field: [Object] },
{
name: 'seo',
title: 'SEO',
description: undefined,
options: [Object],
group: undefined,
fields: [Array],
hidden: undefined,
readOnly: undefined
},
{ single: true, field: [Object] },
{ single: true, field: [Object] }
],
preview: {
prepare: [Function: prepare],
select: {
title: 'title',
media: 'image',
redirect: 'redirectToLegacy',
subtitle: 'parent.title'
}
},
options: {},
orderings: [ { name: 'title', title: 'Title', by: [Array] } ],
validation: [
Rule {
_type: 'Object',
_level: 'error',
_required: undefined,
_typeDef: [Circular *1],
_message: undefined,
_rules: [Array],
_fieldRules: undefined,
valueOfField: [Function: bound ]
}
]
}
{
jsonType: 'array',
type: {
jsonType: 'array',
type: {
name: 'array',
type: null,
jsonType: 'array',
of: [],
validation: [Array]
},
name: 'blockContentSimple',
title: 'Block Content Simple',
validation: [ [Rule] ],
parseType: 'blockContent',
of: [ [Object] ]
},
name: 'blockContentSimple',
title: 'Content',
validation: [
Rule {
_type: 'Array',
_level: 'error',
_required: 'required',
_typeDef: [Circular *1],
_message: undefined,
_rules: [Array],
_fieldRules: undefined,
valueOfField: [Function: bound ]
}
],
parseType: 'blockContent',
of: [
{
type: [Object],
name: 'block',
jsonType: 'object',
title: 'Block',
options: {},
fields: [Array],
preview: [Object],
validation: [Array]
}
],
initialValue: [
{
_type: 'block',
children: [Array],
markDefs: [],
style: 'normal'
}
]
}
At the Full Registry Examples ➡️