@@ -2,45 +2,22 @@ import { z } from "zod";
22
33import { UnixTimestampSchema } from "./common.js" ;
44
5- /**
6- * Catalog management schemas and types
7- */
8-
9- export const CatalogItemSchema = z . object ( {
10- id : z . string ( ) ,
11- name : z . string ( ) ,
12- description : z . string ( ) . optional ( ) ,
13- price : z . number ( ) . optional ( ) ,
14- categories : z . array ( z . string ( ) ) . optional ( ) ,
15- imageUrl : z . string ( ) . optional ( ) ,
16- url : z . string ( ) . optional ( ) ,
17- dataFields : z . record ( z . string ( ) , z . any ( ) ) . optional ( ) ,
18- } ) ;
19-
20- export type CatalogItem = z . infer < typeof CatalogItemSchema > ;
21- export type UpdateCatalogItemParams = z . infer <
22- typeof UpdateCatalogItemParamsSchema
23- > ;
24- export type GetCatalogItemParams = z . infer < typeof GetCatalogItemParamsSchema > ;
25- export type DeleteCatalogItemParams = z . infer <
26- typeof DeleteCatalogItemParamsSchema
27- > ;
28-
29- export const UpdateCatalogItemParamsSchema = z . object ( {
30- catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
31- items : z . array ( CatalogItemSchema ) . describe ( "Catalog items to update" ) ,
32- } ) ;
33-
345export const GetCatalogItemParamsSchema = z . object ( {
356 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
367 itemId : z . string ( ) . describe ( "ID of the catalog item to retrieve" ) ,
378} ) ;
389
10+ export type GetCatalogItemParams = z . infer < typeof GetCatalogItemParamsSchema > ;
11+
3912export const DeleteCatalogItemParamsSchema = z . object ( {
4013 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
4114 itemId : z . string ( ) . describe ( "ID of the catalog item to delete" ) ,
4215} ) ;
4316
17+ export type DeleteCatalogItemParams = z . infer <
18+ typeof DeleteCatalogItemParamsSchema
19+ > ;
20+
4421export const CatalogNameSchema = z . object ( {
4522 name : z . string ( ) ,
4623} ) ;
@@ -193,6 +170,82 @@ export type UpdateCatalogFieldMappingsParams = z.infer<
193170 typeof UpdateCatalogFieldMappingsParamsSchema
194171> ;
195172
173+ const CATALOG_ITEM_ID_REGEX = / ^ [ a - z A - Z 0 - 9 - ] { 1 , 255 } $ / ;
174+ const MAX_CATALOG_BULK_DOCUMENTS = 1000 ;
175+
176+ export const CatalogDocumentFieldsSchema = z
177+ . record ( z . string ( ) , z . unknown ( ) )
178+ . describe ( "Catalog item field values" ) ;
179+
180+ export type CatalogDocumentFields = z . infer < typeof CatalogDocumentFieldsSchema > ;
181+
182+ function validateCatalogDocuments (
183+ documents : Record < string , CatalogDocumentFields > ,
184+ ctx : z . RefinementCtx
185+ ) : void {
186+ const itemIds = Object . keys ( documents ) ;
187+
188+ if ( itemIds . length === 0 ) {
189+ ctx . addIssue ( {
190+ code : "custom" ,
191+ message : "documents must contain at least one catalog item" ,
192+ } ) ;
193+ return ;
194+ }
195+
196+ if ( itemIds . length > MAX_CATALOG_BULK_DOCUMENTS ) {
197+ ctx . addIssue ( {
198+ code : "custom" ,
199+ message : `documents may contain at most ${ MAX_CATALOG_BULK_DOCUMENTS } items` ,
200+ } ) ;
201+ }
202+
203+ for ( const itemId of itemIds ) {
204+ if ( ! CATALOG_ITEM_ID_REGEX . test ( itemId ) ) {
205+ ctx . addIssue ( {
206+ code : "custom" ,
207+ message : `Invalid catalog item ID "${ itemId } " (alphanumeric and dashes only, max 255 chars)` ,
208+ } ) ;
209+ }
210+
211+ for ( const fieldName of Object . keys ( documents [ itemId ] ?? { } ) ) {
212+ if ( fieldName . includes ( "." ) ) {
213+ ctx . addIssue ( {
214+ code : "custom" ,
215+ message : `Field name "${ fieldName } " must not contain periods` ,
216+ } ) ;
217+ }
218+ }
219+ }
220+ }
221+
222+ export const CatalogDocumentsSchema = z
223+ . record ( z . string ( ) , CatalogDocumentFieldsSchema )
224+ . superRefine ( validateCatalogDocuments )
225+ . describe ( "Map of catalog item ID to field values" ) ;
226+
227+ export type CatalogDocuments = z . infer < typeof CatalogDocumentsSchema > ;
228+
229+ const BulkCatalogItemsParamsSchema = z . object ( {
230+ catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
231+ documents : CatalogDocumentsSchema ,
232+ } ) ;
233+
234+ // Bulk partial update catalog items
235+ export const PartialUpdateCatalogItemsParamsSchema =
236+ BulkCatalogItemsParamsSchema ;
237+
238+ export type PartialUpdateCatalogItemsParams = z . infer <
239+ typeof PartialUpdateCatalogItemsParamsSchema
240+ > ;
241+
242+ // Bulk replace catalog items
243+ export const ReplaceCatalogItemsParamsSchema = BulkCatalogItemsParamsSchema ;
244+
245+ export type ReplaceCatalogItemsParams = z . infer <
246+ typeof ReplaceCatalogItemsParamsSchema
247+ > ;
248+
196249// Bulk delete catalog items
197250export const BulkDeleteCatalogItemsParamsSchema = z . object ( {
198251 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
@@ -207,7 +260,7 @@ export type BulkDeleteCatalogItemsParams = z.infer<
207260export const PartialUpdateCatalogItemParamsSchema = z . object ( {
208261 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
209262 itemId : z . string ( ) . describe ( "ID of the catalog item" ) ,
210- update : z . record ( z . string ( ) , z . any ( ) ) . describe ( "Fields to update" ) ,
263+ update : CatalogDocumentFieldsSchema . describe ( "Fields to update" ) ,
211264} ) ;
212265
213266export type PartialUpdateCatalogItemParams = z . infer <
@@ -218,7 +271,7 @@ export type PartialUpdateCatalogItemParams = z.infer<
218271export const ReplaceCatalogItemParamsSchema = z . object ( {
219272 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
220273 itemId : z . string ( ) . describe ( "ID of the catalog item" ) ,
221- value : z . record ( z . string ( ) , z . any ( ) ) . describe ( "New value for the item" ) ,
274+ value : CatalogDocumentFieldsSchema . describe ( "New value for the item" ) ,
222275} ) ;
223276
224277export type ReplaceCatalogItemParams = z . infer <
0 commit comments