-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[types] Improve schema type typings with additional options
- Loading branch information
Showing
11 changed files
with
118 additions
and
30 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isObject(obj: unknown): obj is Record<string, unknown> { | ||
return typeof obj === 'object' && obj !== null && !Array.isArray(obj) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {isObject} from '../helpers' | ||
import {Reference} from './types' | ||
|
||
export function isReference(reference: unknown): reference is Reference { | ||
return isObject(reference) && typeof reference._ref === 'string' | ||
} |
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,2 @@ | ||
export * from './asserters' | ||
export * from './types' |
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,27 @@ | ||
import {Path} from '../paths' | ||
import {SanityDocument} from '../documents' | ||
|
||
export interface Reference { | ||
_ref: string | ||
_key?: string | ||
_weak?: boolean | ||
} | ||
|
||
export interface WeakReference extends Reference { | ||
_weak: true | ||
} | ||
|
||
export type ReferenceFilterSearchOptions = { | ||
filter?: string | ||
params?: Record<string, unknown> | ||
} | ||
|
||
export type ReferenceFilterResolver = (options: { | ||
document: SanityDocument | ||
parent?: Record<string, unknown> | Record<string, unknown>[] | ||
parentPath: Path | ||
}) => ReferenceFilterSearchOptions | ||
|
||
export type ReferenceOptions = | ||
| {filter: ReferenceFilterResolver} | ||
| {filter: string; filterParams?: Record<string, unknown>} |
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,21 @@ | ||
import {ObjectSchemaType, SchemaType} from './types' | ||
import {ObjectSchemaType, ReferenceSchemaType, SchemaType, TitledListValue} from './types' | ||
|
||
export function isObjectSchemaType(type: SchemaType): type is ObjectSchemaType { | ||
return type.jsonType === 'object' | ||
} | ||
|
||
export function isReferenceSchemaType( | ||
type: SchemaType | ReferenceSchemaType | ||
): type is ReferenceSchemaType { | ||
return type.jsonType === 'object' && 'to' in type && Array.isArray(type.to) | ||
} | ||
|
||
export function isTitledListValue(item: unknown): item is TitledListValue { | ||
return ( | ||
typeof item === 'object' && | ||
item !== null && | ||
'title' in item && | ||
'value' in item && | ||
Object.keys(item).length === 2 | ||
) | ||
} |
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