Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #61 from saiichihashimoto/asset-type-infer
Browse files Browse the repository at this point in the history
fix(asset): asset types infer correctly
  • Loading branch information
kodiakhq[bot] authored Jun 9, 2022
2 parents aa875e7 + b8e2b37 commit 657d3b0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
27 changes: 11 additions & 16 deletions src/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import type { Faker } from "@faker-js/faker";
import type { PortableTextBlock } from "@portabletext/types";
import type { Schema } from "@sanity/types";

interface BlockType
extends SanityType<
Omit<
TypeValidation<Schema.BlockDefinition, PortableTextBlock>,
FieldOptionKeys
>,
z.ZodType<PortableTextBlock>
> {}

type BlockDef = Omit<
TypeValidation<Schema.BlockDefinition, PortableTextBlock>,
FieldOptionKeys | "type"
>;

export const block = ({
mock = (faker): PortableTextBlock => ({
style: "normal",
Expand All @@ -36,9 +22,18 @@ export const block = ({
],
}),
...def
}: BlockDef & {
}: Omit<
TypeValidation<Schema.BlockDefinition, PortableTextBlock>,
FieldOptionKeys | "type"
> & {
mock?: (faker: Faker) => PortableTextBlock;
} = {}): BlockType =>
} = {}): SanityType<
Omit<
TypeValidation<Schema.BlockDefinition, PortableTextBlock>,
FieldOptionKeys
>,
z.ZodType<PortableTextBlock>
> =>
createType({
mock,
// TODO Validate PortableTextBlock somehow
Expand Down
12 changes: 9 additions & 3 deletions src/field/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { flow, fromPairs } from "lodash/fp";
import { z } from "zod";

import type { InferZod, Resolve, SanityType, TypeValidation } from "../types";
import type {
AnyObject,
InferZod,
Resolve,
SanityType,
TypeValidation,
} from "../types";
import type {
PrepareViewOptions,
PreviewConfig,
Expand Down Expand Up @@ -218,7 +224,7 @@ export const field = <
>([options]);

export type Preview<
Value extends Record<string, unknown>,
Value extends AnyObject,
Select extends NonNullable<PreviewConfig["select"]>
> =
| {
Expand Down Expand Up @@ -262,7 +268,7 @@ export type Preview<
};

export const preview = <
Value extends Record<string, unknown>,
Value extends AnyObject,
Select extends NonNullable<PreviewConfig["select"]>
>(
preview: Preview<Value, Select> | undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import { createType } from "../types";

import type { FieldOptionKeys, FieldsType, InferFieldsZod } from "../field";
import type { SanityType, TypeValidation } from "../types";
import type { EmptyObject, SanityType, TypeValidation } from "../types";
import type { Faker } from "@faker-js/faker";
import type { Schema } from "@sanity/types";

Expand All @@ -22,7 +22,7 @@ type ZodFile<Fields extends FieldsType<any, any>> = z.ZodIntersection<
>;

export const file = <
Fields extends FieldsType<any, any> = FieldsType<never, Record<never, never>>
Fields extends FieldsType<any, any> = FieldsType<never, EmptyObject>
>(
def: Omit<
TypeValidation<Schema.FileDefinition, z.input<ZodFile<Fields>>>,
Expand Down
4 changes: 2 additions & 2 deletions src/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import { createType } from "../types";

import type { FieldsType, InferFieldsZod } from "../field";
import type { SanityType, TypeValidation } from "../types";
import type { EmptyObject, SanityType, TypeValidation } from "../types";
import type { Faker } from "@faker-js/faker";
import type { Schema } from "@sanity/types";

Expand Down Expand Up @@ -46,7 +46,7 @@ type ZodImage<

export const image = <
Hotspot extends boolean = false,
Fields extends FieldsType<any, any> = FieldsType<never, Record<never, never>>
Fields extends FieldsType<any, any> = FieldsType<never, EmptyObject>
>(
def: Omit<
TypeValidation<Schema.ImageDefinition, z.input<ZodImage<Hotspot, Fields>>>,
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import type {
import type { PartialDeep, SetOptional } from "type-fest";
import type { z } from "zod";

// TODO Type Definition across the board
export type AnyObject = Record<string, unknown>;
export type EmptyObject = Record<string, never>;

// TODO Type Definition across the board
export interface SanityType<Definition, Zod extends z.ZodType<any, any, any>> {
mock: (faker: Faker) => z.input<Zod>;
parse: (data: unknown) => z.output<Zod>;
Expand Down

0 comments on commit 657d3b0

Please sign in to comment.