-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: context available to initialValue function and added schema to …
…validation context Initial value context contains client, schema, currentUser, projectId and dataset. Validation context now also contains schema (alongside client and others).
- Loading branch information
Showing
27 changed files
with
492 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,7 @@ tsconfig.tsbuildinfo | |
|
||
# Sanity temp folders | ||
.sanity | ||
|
||
#IntelliJ | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
dev/test-studio/schema/docs/v3/async-functions/schemaType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import {ConfigContext, defineField, defineType} from 'sanity' | ||
import {structureGroupOptions} from '../../../../structure/groupByOption' | ||
|
||
export const validationTest = defineType({ | ||
type: 'document', | ||
name: 'v3-validation', | ||
title: 'v3 validation', | ||
options: structureGroupOptions({ | ||
structureGroup: 'v3', | ||
}), | ||
fields: [ | ||
defineField({ | ||
type: 'string', | ||
name: 'title', | ||
title: 'Title', | ||
validation: (Rule) => | ||
Rule.custom( | ||
async (value, context) => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Context in custom validation function', context) | ||
resolve( | ||
`Always async error for. From context client->projectId: ${ | ||
context.client.config().projectId | ||
}` | ||
) | ||
}, 1000) | ||
}) | ||
), | ||
initialValue: async (params: undefined, context: ConfigContext) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Context in delayed initial value function, string', context) | ||
resolve(context.projectId) | ||
}, 1000) | ||
}) | ||
}, | ||
}), | ||
{ | ||
type: 'array', | ||
name: 'testArray', | ||
title: 'Test array', | ||
of: [ | ||
{ | ||
type: 'object', | ||
fields: [ | ||
{ | ||
type: 'string', | ||
name: 'title', | ||
title: 'Title', | ||
}, | ||
], | ||
initialValue: async (params: undefined, context: ConfigContext) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Context in delayed initial value function, array object', context) | ||
resolve({ | ||
title: context.projectId, | ||
}) | ||
}, 1000) | ||
}) | ||
}, | ||
}, | ||
], | ||
initialValue: async (params: undefined, context: ConfigContext) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Context in delayed initial value function, array', context) | ||
resolve([ | ||
{ | ||
_key: `${Math.random()}`, | ||
title: context.projectId, | ||
}, | ||
]) | ||
}, 1000) | ||
}) | ||
}, | ||
}, | ||
{ | ||
title: 'Block', | ||
name: 'blockText', | ||
type: 'array', | ||
of: [ | ||
{type: 'block'}, | ||
{ | ||
type: 'object', | ||
name: 'testObject', | ||
fields: [ | ||
{ | ||
type: 'string', | ||
name: 'title', | ||
title: 'Title', | ||
}, | ||
], | ||
initialValue: async (params: undefined, context: ConfigContext) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'Context in delayed initial value function, block array object', | ||
context | ||
) | ||
resolve({ | ||
_type: 'testObject', | ||
title: context.projectId, | ||
}) | ||
}, 1000) | ||
}) | ||
}, | ||
}, | ||
], | ||
initialValue: async (params: undefined, context: ConfigContext) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Context in delayed initial value function, block array', context) | ||
resolve([ | ||
{ | ||
style: 'normal', | ||
_type: 'block', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
text: context.projectId, | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
_type: 'testObject', | ||
title: context.projectId, | ||
}, | ||
]) | ||
}, 1000) | ||
}) | ||
}, | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import {example1SchemaType} from './example1' | ||
import {validationTest} from './async-functions/schemaType' | ||
|
||
export const v3docs = { | ||
types: [example1SchemaType], | ||
types: [example1SchemaType, validationTest], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type {StructureBuilder} from 'sanity/desk' | ||
import {Schema} from '@sanity/types' | ||
|
||
type StructureGroup = 'v3' // extend with union strings | ||
|
||
export interface StructureGroupOption { | ||
structureGroup?: StructureGroup | ||
} | ||
|
||
type MaybeStructureOptions = StructureGroupOption | undefined | ||
|
||
export function typesInOptionGroup( | ||
S: StructureBuilder, | ||
schema: Schema, | ||
groupName: StructureGroup | ||
): string[] { | ||
return S.documentTypeListItems() | ||
.map((item) => item.getId()) | ||
.filter((id): id is string => { | ||
return ( | ||
!!id && (schema.get(id)?.options as MaybeStructureOptions)?.structureGroup === groupName | ||
) | ||
}) | ||
} | ||
|
||
export function structureGroupOptions< | ||
O extends Required<StructureGroupOption> & Schema.ObjectOptions | ||
>(options: O): O & Schema.ObjectOptions { | ||
return options | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.