Skip to content

Commit

Permalink
Merge pull request #3944 from L-Mario564/export-internals
Browse files Browse the repository at this point in the history
[Validators] Export `BuildSchema` type and simplify JSON schema
  • Loading branch information
AndriiSherman authored Jan 23, 2025
2 parents 3dff816 + 0e56bba commit 0cd20b6
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 11 deletions.
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

0 comments on commit 0cd20b6

Please sign in to comment.