Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validators] Export BuildSchema type and simplify JSON schema #3944

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions drizzle-typebox/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ import { isColumnType, isWithEnum } from './utils.ts';
import type { BufferSchema, JsonSchema } from './utils.ts';

export const literalSchema = t.Union([t.String(), t.Number(), t.Boolean(), t.Null()]);
export const jsonSchema: JsonSchema = t.Recursive((self) =>
t.Union([literalSchema, t.Array(self), t.Record(t.String(), self)])
) as any;
export const jsonSchema: JsonSchema = t.Union([literalSchema, t.Array(t.Any()), t.Record(t.String(), t.Any())]) as any;
TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof
export const bufferSchema: BufferSchema = { [Kind]: 'Buffer', type: 'buffer' } as any;

Expand Down
1 change: 1 addition & 0 deletions drizzle-typebox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './schema.ts';
export type { BuildSchema } from './schema.types.internal.ts';
export * from './schema.types.ts';
2 changes: 1 addition & 1 deletion drizzle-typebox/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function isWithEnum(column: Column): column is typeof column & { enumValu
export const isPgEnum: (entity: any) => entity is PgEnum<[string, ...string[]]> = isWithEnum as any;

type Literal = Static<typeof literalSchema>;
export type Json = Literal | { [key: string]: Json } | Json[];
export type Json = Literal | { [key: string]: any } | any[];
export interface JsonSchema extends TSchema {
[Kind]: 'Union';
static: Json;
Expand Down
4 changes: 2 additions & 2 deletions drizzle-valibot/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ import type { Json } from './utils.ts';
export const literalSchema = v.union([v.string(), v.number(), v.boolean(), v.null()]);
export const jsonSchema: v.GenericSchema<Json> = v.union([
literalSchema,
v.array(v.lazy(() => jsonSchema)),
v.record(v.string(), v.lazy(() => jsonSchema)),
v.array(v.any()),
v.record(v.string(), v.any()),
]);
export const bufferSchema: v.GenericSchema<Buffer> = v.custom<Buffer>((v) => v instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof

Expand Down
1 change: 1 addition & 0 deletions drizzle-valibot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './schema.ts';
export type { BuildSchema } from './schema.types.internal.ts';
export * from './schema.types.ts';
2 changes: 1 addition & 1 deletion drizzle-valibot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function isWithEnum(column: Column): column is typeof column & { enumValu
export const isPgEnum: (entity: any) => entity is PgEnum<[string, ...string[]]> = isWithEnum as any;

type Literal = v.InferOutput<typeof literalSchema>;
export type Json = Literal | { [key: string]: Json } | Json[];
export type Json = Literal | { [key: string]: any } | any[];

export type IsNever<T> = [T] extends [never] ? true : false;

Expand Down
4 changes: 1 addition & 3 deletions drizzle-zod/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ import { isColumnType, isWithEnum } from './utils.ts';
import type { Json } from './utils.ts';

export const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
export const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
export const jsonSchema: z.ZodType<Json> = z.union([literalSchema, z.record(z.any()), z.array(z.any())]);
export const bufferSchema: z.ZodType<Buffer> = z.custom<Buffer>((v) => v instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof

export function columnToSchema(column: Column, factory: CreateSchemaFactoryOptions | undefined): z.ZodTypeAny {
Expand Down
1 change: 1 addition & 0 deletions drizzle-zod/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './schema.ts';
export type { BuildSchema } from './schema.types.internal.ts';
export * from './schema.types.ts';
2 changes: 1 addition & 1 deletion drizzle-zod/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function isWithEnum(column: Column): column is typeof column & { enumValu
export const isPgEnum: (entity: any) => entity is PgEnum<[string, ...string[]]> = isWithEnum as any;

type Literal = z.infer<typeof literalSchema>;
export type Json = Literal | { [key: string]: Json } | Json[];
export type Json = Literal | { [key: string]: any } | any[];

export type IsNever<T> = [T] extends [never] ? true : false;

Expand Down
Loading