@@ -239,14 +239,25 @@ export const AccountIdParamsSchema = z.object({
239239 accountId : z . string ( ) . uuid ( ) ,
240240} ) ;
241241
242+ // Schema for transaction objects when adding to a specific account
243+ // Note: account field is not needed as it's in the URL path
244+ export const AccountTransactionSchema = z . object ( {
245+ amount : z . number ( ) ,
246+ date : z . string ( ) . optional ( ) ,
247+ payee : z . string ( ) . max ( 255 ) . optional ( ) ,
248+ notes : z . string ( ) . max ( 1000 ) . optional ( ) ,
249+ category : z . string ( ) . optional ( ) ,
250+ cleared : z . boolean ( ) . optional ( ) ,
251+ } ) ;
252+
242253export const TransactionsAddSchema = z . object ( {
243- transactions : z . array ( CreateTransactionSchema ) ,
254+ transactions : z . array ( AccountTransactionSchema ) ,
244255 runTransfers : z . boolean ( ) . optional ( ) . default ( false ) ,
245256 learnCategories : z . boolean ( ) . optional ( ) . default ( false ) ,
246257} ) ;
247258
248259export const TransactionsImportSchema = z . object ( {
249- transactions : z . array ( CreateTransactionSchema ) ,
260+ transactions : z . array ( AccountTransactionSchema ) ,
250261} ) ;
251262
252263// Rules schemas
@@ -284,12 +295,15 @@ export const ClientIdParamsSchema = z.object({
284295export const validateBody = ( schema ) => ( req , res , next ) => {
285296 const result = schema . safeParse ( req . body ) ;
286297 if ( ! result . success ) {
298+ const errors = result . error ?. errors || [ ] ;
287299 return res . status ( 400 ) . json ( {
288300 error : 'Validation failed' ,
289- details : result . error . errors . map ( ( err ) => ( {
290- field : err . path . join ( '.' ) ,
291- message : err . message ,
292- } ) ) ,
301+ details : errors . length > 0
302+ ? errors . map ( ( err ) => ( {
303+ field : err . path ?. join ( '.' ) || 'unknown' ,
304+ message : err . message || 'Validation error' ,
305+ } ) )
306+ : [ { field : 'unknown' , message : result . error ?. message || 'Validation failed' } ] ,
293307 } ) ;
294308 }
295309 req . validatedBody = result . data ;
@@ -299,12 +313,15 @@ export const validateBody = (schema) => (req, res, next) => {
299313export const validateParams = ( schema ) => ( req , res , next ) => {
300314 const result = schema . safeParse ( req . params ) ;
301315 if ( ! result . success ) {
316+ const errors = result . error ?. errors || [ ] ;
302317 return res . status ( 400 ) . json ( {
303318 error : 'Invalid parameters' ,
304- details : result . error . errors . map ( ( err ) => ( {
305- field : err . path . join ( '.' ) ,
306- message : err . message ,
307- } ) ) ,
319+ details : errors . length > 0
320+ ? errors . map ( ( err ) => ( {
321+ field : err . path ?. join ( '.' ) || 'unknown' ,
322+ message : err . message || 'Validation error' ,
323+ } ) )
324+ : [ { field : 'unknown' , message : result . error ?. message || 'Invalid parameters' } ] ,
308325 } ) ;
309326 }
310327 req . validatedParams = result . data ;
@@ -314,12 +331,15 @@ export const validateParams = (schema) => (req, res, next) => {
314331export const validateQuery = ( schema ) => ( req , res , next ) => {
315332 const result = schema . safeParse ( req . query ) ;
316333 if ( ! result . success ) {
334+ const errors = result . error ?. errors || [ ] ;
317335 return res . status ( 400 ) . json ( {
318336 error : 'Invalid query parameters' ,
319- details : result . error . errors . map ( ( err ) => ( {
320- field : err . path . join ( '.' ) ,
321- message : err . message ,
322- } ) ) ,
337+ details : errors . length > 0
338+ ? errors . map ( ( err ) => ( {
339+ field : err . path ?. join ( '.' ) || 'unknown' ,
340+ message : err . message || 'Validation error' ,
341+ } ) )
342+ : [ { field : 'unknown' , message : result . error ?. message || 'Invalid query parameters' } ] ,
323343 } ) ;
324344 }
325345 req . validatedQuery = result . data ;
0 commit comments