@@ -8,48 +8,36 @@ import { z } from 'zod';
88 */
99const objectIdRegex = / ^ [ a - f \d ] { 24 } $ / i;
1010
11- const Subscription = z . object ( {
12- organization : z
13- . string ( )
14- . trim ( )
15- . regex ( objectIdRegex , 'organization must be a valid ObjectId' ) ,
16- stripeCustomerId : z
17- . string ( )
18- . trim ( )
19- . optional ( )
20- . transform ( ( val ) => ( val === '' ? undefined : val ) ) ,
21- stripeSubscriptionId : z
22- . string ( )
23- . trim ( )
24- . optional ( )
25- . transform ( ( val ) => ( val === '' ? undefined : val ) ) ,
26- plan : z . enum ( [ 'free' , 'starter' , 'pro' ] ) . default ( 'free' ) ,
27- status : z . enum ( [ 'active' , 'past_due' , 'canceled' , 'trialing' , 'incomplete' ] ) . default ( 'active' ) ,
11+ const optionalStripeId = z
12+ . string ( )
13+ . trim ( )
14+ . optional ( )
15+ . transform ( ( val ) => ( val === '' ? undefined : val ) ) ;
16+
17+ const plans = [ 'free' , 'starter' , 'pro' ] ;
18+ const statuses = [ 'active' , 'past_due' , 'canceled' , 'trialing' , 'incomplete' ] ;
19+
20+ const baseShape = {
21+ organization : z . string ( ) . trim ( ) . regex ( objectIdRegex , 'organization must be a valid ObjectId' ) ,
22+ stripeCustomerId : optionalStripeId ,
23+ stripeSubscriptionId : optionalStripeId ,
2824 currentPeriodEnd : z . coerce . date ( ) . nullable ( ) . optional ( ) ,
25+ } ;
26+
27+ const Subscription = z . object ( {
28+ ...baseShape ,
29+ plan : z . enum ( plans ) . default ( 'free' ) ,
30+ status : z . enum ( statuses ) . default ( 'active' ) ,
2931 cancelAtPeriodEnd : z . boolean ( ) . default ( false ) ,
3032} ) ;
3133
3234/**
3335 * Update schema without defaults to avoid populating unspecified fields during PATCH
3436 */
3537const SubscriptionCore = z . object ( {
36- organization : z
37- . string ( )
38- . trim ( )
39- . regex ( objectIdRegex , 'organization must be a valid ObjectId' ) ,
40- stripeCustomerId : z
41- . string ( )
42- . trim ( )
43- . optional ( )
44- . transform ( ( val ) => ( val === '' ? undefined : val ) ) ,
45- stripeSubscriptionId : z
46- . string ( )
47- . trim ( )
48- . optional ( )
49- . transform ( ( val ) => ( val === '' ? undefined : val ) ) ,
50- plan : z . enum ( [ 'free' , 'starter' , 'pro' ] ) ,
51- status : z . enum ( [ 'active' , 'past_due' , 'canceled' , 'trialing' , 'incomplete' ] ) ,
52- currentPeriodEnd : z . coerce . date ( ) . nullable ( ) . optional ( ) ,
38+ ...baseShape ,
39+ plan : z . enum ( plans ) ,
40+ status : z . enum ( statuses ) ,
5341 cancelAtPeriodEnd : z . boolean ( ) ,
5442} ) ;
5543
0 commit comments