/
schemaDefinition.ts
161 lines (151 loc) · 4.58 KB
/
schemaDefinition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {ComponentType, ReactNode} from 'react'
import {PreviewConfig} from '../preview'
import {InitialValueProperty, SchemaValidationValue} from '../types'
import {
ArrayDefinition,
BlockDefinition,
BooleanDefinition,
CrossDatasetReferenceDefinition,
DateDefinition,
DatetimeDefinition,
DocumentDefinition,
FileDefinition,
GeopointDefinition,
ImageDefinition,
NumberDefinition,
ObjectDefinition,
ReferenceDefinition,
SlugDefinition,
StringDefinition,
TextDefinition,
UrlDefinition,
EmailDefinition,
} from './type'
import {BaseSchemaDefinition} from './type/common'
/**
* `IntrinsicDefinitions` is a lookup map for "predefined" schema definitions.
* Schema types in `IntrinsicDefinitions` will have good type-completion and type-safety in {@link defineType},
* {@link defineField} and {@link defineArrayMember} once the `type` property is provided.
*
* By default, `IntrinsicDefinitions` contains all standard Sanity schema types (`array`, `string`, `number` ect),
* but it is an interface and as such, open for extension.
*
* This type can be extended using declaration merging; this way new entries can be added.
* See {@link defineType} for examples on how this can be accomplished.
*
* @see defineType
*
* @public
*/
export interface IntrinsicDefinitions {
array: ArrayDefinition
block: BlockDefinition
boolean: BooleanDefinition
date: DateDefinition
datetime: DatetimeDefinition
document: DocumentDefinition
file: FileDefinition
geopoint: GeopointDefinition
image: ImageDefinition
number: NumberDefinition
object: ObjectDefinition
reference: ReferenceDefinition
crossDatasetReference: CrossDatasetReferenceDefinition
slug: SlugDefinition
string: StringDefinition
text: TextDefinition
url: UrlDefinition
email: EmailDefinition
}
/**
* A union of all intrinsic types allowed natively in the schema.
*
* @see IntrinsicDefinitions
*
* @public
*/
export type IntrinsicTypeName = IntrinsicDefinitions[keyof IntrinsicDefinitions]['type']
/**
* Represents a Sanity schema type definition with an optional type parameter.
*
* It's recommend to use the `defineType` helper instead of this type by
* itself.
*
* @see defineType
*
* @public
*/
export type SchemaTypeDefinition<TType extends IntrinsicTypeName = IntrinsicTypeName> =
| IntrinsicDefinitions[IntrinsicTypeName]
| TypeAliasDefinition<string, TType>
/**
* Represents a reference to another type registered top-level in your schema.
*
* @public
*/
export interface TypeReference {
type: string
name?: string
icon?: ComponentType | ReactNode
options?: {[key: string]: unknown}
}
/**
* Represents a type definition that is an alias/extension of an existing type
* in your schema. Creating a type alias will re-register that existing type
* under a different name. You can also override the default type options with
* a type alias definition.
*
* @public
*/
export interface TypeAliasDefinition<
TType extends string,
TAlias extends IntrinsicTypeName | undefined
> extends BaseSchemaDefinition {
type: TType
options?: TAlias extends IntrinsicTypeName ? IntrinsicDefinitions[TAlias]['options'] : unknown
validation?: SchemaValidationValue
initialValue?: InitialValueProperty<any, any>
preview?: PreviewConfig
components?: {
annotation?: ComponentType<any>
block?: ComponentType<any>
inlineBlock?: ComponentType<any>
diff?: ComponentType<any>
field?: ComponentType<any>
input?: ComponentType<any>
item?: ComponentType<any>
preview?: ComponentType<any>
}
}
/** @public */
export interface FieldDefinitionBase {
fieldset?: string
group?: string | string[]
}
/** @public */
export type InlineFieldDefinition = {
[K in keyof IntrinsicDefinitions]: Omit<
IntrinsicDefinitions[K],
'initialValue' | 'validation'
> & {
// widen these so these are not unknown in FieldDefinition arrays due to mutually exclusive unions
validation?: SchemaValidationValue
initialValue?: InitialValueProperty<any, any>
}
}
/**
* The shape of a field definition. Note, it's recommended to use the
* `defineField` function instead of using this type directly.
*
* Where `defineField` infers the exact field type,
* FieldDefinition is a compromise union of all types a field can have.
*
* A field definition can be a reference to another registered top-level type
* or a inline type definition.
*
* @public
*/
export type FieldDefinition<
TType extends IntrinsicTypeName = IntrinsicTypeName,
TAlias extends IntrinsicTypeName | undefined = undefined
> = (InlineFieldDefinition[TType] | TypeAliasDefinition<string, TAlias>) & FieldDefinitionBase