Skip to content

Commit

Permalink
feat(schema): include title, description, and deprecated attrib…
Browse files Browse the repository at this point in the history
…utes when extracting schema
  • Loading branch information
juice49 committed Apr 23, 2024
1 parent 967f956 commit 60cb576
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/@sanity/schema/src/sanity/extractSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import {
type UnknownTypeNode,
} from 'groq-js'

type Metadata<Type = unknown> = Type &
Pick<SanitySchemaType, 'title' | 'description' | 'deprecated'>

const documentDefaultFields = (typeName: string): Record<string, ObjectAttribute> => ({
_id: {
type: 'objectAttribute',
Expand Down Expand Up @@ -89,14 +92,20 @@ export function extractSchema(

function convertBaseType(
schemaType: SanitySchemaType,
): DocumentSchemaType | TypeDeclarationSchemaType | null {
): Metadata<DocumentSchemaType | TypeDeclarationSchemaType> | null {
let typeName: string | undefined
if (schemaType.type) {
typeName = schemaType.type.name
} else if ('jsonType' in schemaType) {
typeName = schemaType.jsonType
}

const metadata: Metadata = {
title: schemaType.title,
description: schemaType.description,
deprecated: schemaType.deprecated,
}

if (typeName === 'document' && isObjectType(schemaType)) {
const defaultAttributes = documentDefaultFields(schemaType.name)

Expand All @@ -106,6 +115,7 @@ export function extractSchema(
}

return {
...metadata,
name: schemaType.name,
type: 'document',
attributes: {
Expand All @@ -121,6 +131,7 @@ export function extractSchema(
}
if (value.type === 'object') {
return {
...metadata,
name: schemaType.name,
type: 'type',
value: {
Expand All @@ -140,6 +151,7 @@ export function extractSchema(
}

return {
...metadata,
name: schemaType.name,
type: 'type',
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
exports[`Extract schema test Extracts schema general 1`] = `
Array [
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imagePaletteSwatch",
"title": "Image palette swatch",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -47,7 +50,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imagePalette",
"title": "Image palette",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -119,7 +125,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imageDimensions",
"title": "Image dimensions",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -156,7 +165,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "geopoint",
"title": "Geographical Point",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -193,7 +205,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "slug",
"title": "Slug",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -223,7 +238,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "someTextType",
"title": "Text",
"type": "type",
"value": Object {
"type": "string",
Expand Down Expand Up @@ -362,11 +380,17 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "sanity.fileAsset",
"title": "File",
"type": "document",
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "code",
"title": "Code",
"type": "type",
"value": Object {
"attributes": Object {
Expand All @@ -389,14 +413,20 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "customStringType",
"title": "My custom string type",
"type": "type",
"value": Object {
"type": "string",
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "blocksTest",
"title": "Blocks test",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -3416,7 +3446,10 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "book",
"title": "Book",
"type": "document",
},
Object {
Expand Down Expand Up @@ -3536,11 +3569,17 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "author",
"title": "Author",
"type": "document",
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imageCrop",
"title": "Image crop",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -3584,7 +3623,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imageHotspot",
"title": "Image hotspot",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -3768,11 +3810,17 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "sanity.imageAsset",
"title": "Image",
"type": "document",
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.assetSourceData",
"title": "Asset Source Data",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -3809,7 +3857,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "sanity.imageMetadata",
"title": "Image metadata",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -4291,7 +4342,10 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "validDocument",
"title": "Valid document",
"type": "document",
},
Object {
Expand Down Expand Up @@ -4335,11 +4389,17 @@ Array [
},
},
},
"deprecated": undefined,
"description": undefined,
"name": "otherValidDocument",
"title": "Other valid document",
"type": "document",
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "manuscript",
"title": "Manuscript",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down Expand Up @@ -4422,7 +4482,10 @@ Array [
},
},
Object {
"deprecated": undefined,
"description": undefined,
"name": "obj",
"title": "Obj",
"type": "type",
"value": Object {
"attributes": Object {
Expand Down

0 comments on commit 60cb576

Please sign in to comment.