77 type TableRelationalConfig ,
88} from 'drizzle-orm'
99import type { Simplify } from 'type-fest'
10- import type { ZodObject } from 'zod'
10+ import type { ZodObject , ZodOptional } from 'zod'
1111import z from 'zod'
1212
1313import {
@@ -444,21 +444,30 @@ export class FieldBuilder<
444444 }
445445}
446446
447+ type CastOptionalFieldToZodSchema <
448+ TField extends Field < any > ,
449+ TSchema extends z . ZodTypeAny ,
450+ > = TField [ '_' ] extends { source : 'column' }
451+ ? TField [ '_' ] [ 'column' ] [ 'notNull' ] extends true
452+ ? ZodOptional < TSchema >
453+ : TSchema
454+ : TSchema
455+
447456type FieldToZodScheama < TField extends Field < any > > =
448457 TField extends FieldColumnStringCollectionOptions < any > [ keyof FieldColumnStringCollectionOptions < any > ]
449- ? z . ZodString
458+ ? CastOptionalFieldToZodSchema < TField , z . ZodString >
450459 : TField extends FieldColumnStringArrayCollectionOptions < any > [ keyof FieldColumnStringArrayCollectionOptions < any > ]
451- ? z . ZodArray < z . ZodString >
460+ ? CastOptionalFieldToZodSchema < TField , z . ZodArray < z . ZodString > >
452461 : TField extends FieldColumnNumberCollectionOptions < any > [ keyof FieldColumnNumberCollectionOptions < any > ]
453- ? z . ZodNumber
462+ ? CastOptionalFieldToZodSchema < TField , z . ZodNumber >
454463 : TField extends FieldColumnNumberArrayCollectionOptions < any > [ keyof FieldColumnNumberArrayCollectionOptions < any > ]
455- ? z . ZodArray < z . ZodNumber >
464+ ? CastOptionalFieldToZodSchema < TField , z . ZodArray < z . ZodNumber > >
456465 : TField extends FieldColumnBooleanCollectionOptions [ keyof FieldColumnBooleanCollectionOptions ]
457- ? z . ZodBoolean
466+ ? CastOptionalFieldToZodSchema < TField , z . ZodBoolean >
458467 : TField extends FieldColumnBooleanArrayCollectionOptions [ keyof FieldColumnBooleanArrayCollectionOptions ]
459- ? z . ZodArray < z . ZodBoolean >
468+ ? CastOptionalFieldToZodSchema < TField , z . ZodArray < z . ZodBoolean > >
460469 : TField extends FieldColumnDateCollectionOptions [ keyof FieldColumnDateCollectionOptions ]
461- ? z . ZodDate
470+ ? CastOptionalFieldToZodSchema < TField , z . ZodDate >
462471 : never
463472// TODO: Relation input
464473// TODO: Optioanl and default values
@@ -473,26 +482,47 @@ export function fieldToZodScheama<TField extends Field<any>>(
473482 case 'selectText' :
474483 case 'time' :
475484 case 'media' :
485+ if ( ! field . _ . column . notNull ) {
486+ return z . string ( ) . optional ( ) as FieldToZodScheama < TField >
487+ }
476488 return z . string ( ) as FieldToZodScheama < TField >
477489 // string[] input
478490 case 'comboboxText' :
491+ if ( ! field . _ . column . notNull ) {
492+ return z . array ( z . string ( ) ) as FieldToZodScheama < TField >
493+ }
479494 return z . array ( z . string ( ) ) as FieldToZodScheama < TField >
480495 // number input
481496 case 'number' :
482497 case 'selectNumber' :
498+ if ( ! field . _ . column . notNull ) {
499+ return z . number ( ) . optional ( ) as FieldToZodScheama < TField >
500+ }
483501 return z . number ( ) as FieldToZodScheama < TField >
484502 // number[] input
485503 case 'comboboxNumber' :
504+ if ( ! field . _ . column . notNull ) {
505+ return z . array ( z . number ( ) ) . optional ( ) as FieldToZodScheama < TField >
506+ }
486507 return z . array ( z . number ( ) ) as FieldToZodScheama < TField >
487508 // boolean input
488509 case 'checkbox' :
489510 case 'switch' :
511+ if ( ! field . _ . column . notNull ) {
512+ return z . boolean ( ) . optional ( ) as FieldToZodScheama < TField >
513+ }
490514 return z . boolean ( ) as FieldToZodScheama < TField >
491515 // boolean[] input
492516 case 'comboboxBoolean' :
517+ if ( ! field . _ . column . notNull ) {
518+ return z . array ( z . boolean ( ) ) . optional ( ) as FieldToZodScheama < TField >
519+ }
493520 return z . array ( z . boolean ( ) ) as FieldToZodScheama < TField >
494521 // date input
495522 case 'date' :
523+ if ( ! field . _ . column . notNull ) {
524+ return z . date ( ) . optional ( ) as FieldToZodScheama < TField >
525+ }
496526 return z . date ( ) as FieldToZodScheama < TField >
497527 // TODO: relation input
498528 case 'connect' :
@@ -515,6 +545,7 @@ export function fieldsToZodObject<TFields extends Fields<any>>(
515545) : FieldsToZodObject < TFields > {
516546 const zodObject = Object . entries ( fields ) . reduce (
517547 ( acc , [ key , field ] ) => {
548+ console . log ( key , field )
518549 acc [ key ] = fieldToZodScheama ( field )
519550 return acc
520551 } ,
0 commit comments