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

[BUG]: Drizzle typebox enum array wrong schema and type #1345

Open
itsyoboieltr opened this issue Oct 6, 2023 · 2 comments
Open

[BUG]: Drizzle typebox enum array wrong schema and type #1345

itsyoboieltr opened this issue Oct 6, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@itsyoboieltr
Copy link

What version of drizzle-orm are you using?

0.28.6

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

The typebox schema and types generated for enum arrays is wrong.

Reproduction:

import { pgTable, pgEnum } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';

export const flags = pgEnum('flags', ['flag-1', 'flag-2', 'flag-3', 'flag-4']);

export const test = pgTable('Test', {
  flags: flags('flags').array().notNull(),
});

const insertSchema = createInsertSchema(test);
type Insert = typeof insertSchema.static;

/* 
type Insert = {
  flags: 'flag-1' | 'flag-2' | 'flag-3' | 'flag-4';
};
*/

const selectSchema = createSelectSchema(test);
type Select = typeof selectSchema.static;

/* 
type Select = {
  flags: 'flag-1' | 'flag-2' | 'flag-3' | 'flag-4';
};
*/

Manual workaround:

import { pgTable, pgEnum } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';
import { Type } from '@sinclair/typebox';

export const flags = pgEnum('flags', ['flag-1', 'flag-2', 'flag-3', 'flag-4']);

export const test = pgTable('Test', {
  flags: flags('flags').array().notNull(),
});

const insertSchema = createInsertSchema(test, {
  flags: Type.Array(
    Type.Union([
      Type.Literal('flag-1'),
      Type.Literal('flag-2'),
      Type.Literal('flag-3'),
      Type.Literal('flag-4'),
    ]),
  ),
});
type Insert = typeof insertSchema.static;

/* 
type Insert = {
  flags: ('flag-1' | 'flag-2' | 'flag-3' | 'flag-4')[];
};
*/

const selectSchema = createSelectSchema(test, {
  flags: Type.Array(
    Type.Union([
      Type.Literal('flag-1'),
      Type.Literal('flag-2'),
      Type.Literal('flag-3'),
      Type.Literal('flag-4'),
    ]),
  ),
});
type Select = typeof selectSchema.static;

/* 
type Select = {
  flags: ('flag-1' | 'flag-2' | 'flag-3' | 'flag-4')[];
};
*/

Expected behavior

Return array, as specified in the drizzle schema, without manually overriding the field.

import { pgTable, pgEnum } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';

export const flags = pgEnum('flags', ['flag-1', 'flag-2', 'flag-3', 'flag-4']);

export const test = pgTable('Test', {
  flags: flags('flags').array().notNull(),
});

const insertSchema = createInsertSchema(test);
type Insert = typeof insertSchema.static;

/* 
type Insert = {
  flags: ('flag-1' | 'flag-2' | 'flag-3' | 'flag-4')[];
};
*/

const selectSchema = createSelectSchema(test);
type Select = typeof selectSchema.static;

/* 
type Select = {
  flags: ('flag-1' | 'flag-2' | 'flag-3' | 'flag-4')[];
};
*/

Environment & setup

No response

@itsyoboieltr itsyoboieltr added the bug Something isn't working label Oct 6, 2023
@noobmaster19
Copy link

Having this issue as well

@MAST1999
Copy link

Hi

I've looked into this and found a solution.
I'll be sending a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants