@@ -2,45 +2,84 @@ 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- > ;
5+ const CATALOG_ITEM_ID_REGEX = / ^ [ a - z A - Z 0 - 9 - ] { 1 , 255 } $ / ;
6+ const MAX_CATALOG_BULK_DOCUMENTS = 1000 ;
7+
8+ export const CatalogItemIdSchema = z
9+ . string ( )
10+ . regex (
11+ CATALOG_ITEM_ID_REGEX ,
12+ "Invalid catalog item ID (alphanumeric and dashes only, max 255 chars)"
13+ ) ;
2814
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- } ) ;
15+ function validateCatalogItemFieldNames (
16+ fields : Record < string , unknown > ,
17+ ctx : z . RefinementCtx
18+ ) : void {
19+ for ( const fieldName of Object . keys ( fields ) ) {
20+ if ( fieldName . includes ( "." ) ) {
21+ ctx . addIssue ( {
22+ code : "custom" ,
23+ message : `Field name "${ fieldName } " must not contain periods` ,
24+ } ) ;
25+ }
26+ }
27+ }
28+
29+ export const CatalogDocumentFieldsSchema = z
30+ . record ( z . string ( ) , z . unknown ( ) )
31+ . superRefine ( validateCatalogItemFieldNames )
32+ . describe ( "Catalog item field values" ) ;
33+
34+ export type CatalogDocumentFields = z . infer < typeof CatalogDocumentFieldsSchema > ;
35+
36+ function validateCatalogDocuments (
37+ documents : Record < string , CatalogDocumentFields > ,
38+ ctx : z . RefinementCtx
39+ ) : void {
40+ const itemIds = Object . keys ( documents ) ;
41+
42+ if ( itemIds . length === 0 ) {
43+ ctx . addIssue ( {
44+ code : "custom" ,
45+ message : "documents must contain at least one catalog item" ,
46+ } ) ;
47+ return ;
48+ }
49+
50+ if ( itemIds . length > MAX_CATALOG_BULK_DOCUMENTS ) {
51+ ctx . addIssue ( {
52+ code : "custom" ,
53+ message : `documents may contain at most ${ MAX_CATALOG_BULK_DOCUMENTS } items` ,
54+ } ) ;
55+ }
56+
57+ for ( const itemId of itemIds ) {
58+ if ( ! CATALOG_ITEM_ID_REGEX . test ( itemId ) ) {
59+ ctx . addIssue ( {
60+ code : "custom" ,
61+ message : `Invalid catalog item ID "${ itemId } " (alphanumeric and dashes only, max 255 chars)` ,
62+ } ) ;
63+ }
64+ }
65+ }
3366
3467export const GetCatalogItemParamsSchema = z . object ( {
3568 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
36- itemId : z . string ( ) . describe ( "ID of the catalog item to retrieve" ) ,
69+ itemId : CatalogItemIdSchema . describe ( "ID of the catalog item to retrieve" ) ,
3770} ) ;
3871
72+ export type GetCatalogItemParams = z . infer < typeof GetCatalogItemParamsSchema > ;
73+
3974export const DeleteCatalogItemParamsSchema = z . object ( {
4075 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
41- itemId : z . string ( ) . describe ( "ID of the catalog item to delete" ) ,
76+ itemId : CatalogItemIdSchema . describe ( "ID of the catalog item to delete" ) ,
4277} ) ;
4378
79+ export type DeleteCatalogItemParams = z . infer <
80+ typeof DeleteCatalogItemParamsSchema
81+ > ;
82+
4483export const CatalogNameSchema = z . object ( {
4584 name : z . string ( ) ,
4685} ) ;
@@ -193,10 +232,39 @@ export type UpdateCatalogFieldMappingsParams = z.infer<
193232 typeof UpdateCatalogFieldMappingsParamsSchema
194233> ;
195234
235+ export const CatalogDocumentsSchema = z
236+ . record ( z . string ( ) , CatalogDocumentFieldsSchema )
237+ . superRefine ( validateCatalogDocuments )
238+ . describe ( "Map of catalog item ID to field values" ) ;
239+
240+ export type CatalogDocuments = z . infer < typeof CatalogDocumentsSchema > ;
241+
242+ const BulkCatalogItemsParamsSchema = z . object ( {
243+ catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
244+ documents : CatalogDocumentsSchema ,
245+ } ) ;
246+
247+ // Bulk partial update catalog items
248+ export const PartialUpdateCatalogItemsParamsSchema =
249+ BulkCatalogItemsParamsSchema ;
250+
251+ export type PartialUpdateCatalogItemsParams = z . infer <
252+ typeof PartialUpdateCatalogItemsParamsSchema
253+ > ;
254+
255+ // Bulk replace catalog items
256+ export const ReplaceCatalogItemsParamsSchema = BulkCatalogItemsParamsSchema ;
257+
258+ export type ReplaceCatalogItemsParams = z . infer <
259+ typeof ReplaceCatalogItemsParamsSchema
260+ > ;
261+
196262// Bulk delete catalog items
197263export const BulkDeleteCatalogItemsParamsSchema = z . object ( {
198264 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
199- itemIds : z . array ( z . string ( ) ) . describe ( "Array of item IDs to delete" ) ,
265+ itemIds : z
266+ . array ( CatalogItemIdSchema )
267+ . describe ( "Array of item IDs to delete" ) ,
200268} ) ;
201269
202270export type BulkDeleteCatalogItemsParams = z . infer <
@@ -206,8 +274,8 @@ export type BulkDeleteCatalogItemsParams = z.infer<
206274// Partial update catalog item (PATCH)
207275export const PartialUpdateCatalogItemParamsSchema = z . object ( {
208276 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
209- itemId : z . string ( ) . describe ( "ID of the catalog item" ) ,
210- update : z . record ( z . string ( ) , z . any ( ) ) . describe ( "Fields to update" ) ,
277+ itemId : CatalogItemIdSchema . describe ( "ID of the catalog item" ) ,
278+ update : CatalogDocumentFieldsSchema . describe ( "Fields to update" ) ,
211279} ) ;
212280
213281export type PartialUpdateCatalogItemParams = z . infer <
@@ -217,8 +285,8 @@ export type PartialUpdateCatalogItemParams = z.infer<
217285// Replace catalog item (PUT)
218286export const ReplaceCatalogItemParamsSchema = z . object ( {
219287 catalogName : z . string ( ) . describe ( "Name of the catalog" ) ,
220- itemId : z . string ( ) . describe ( "ID of the catalog item" ) ,
221- value : z . record ( z . string ( ) , z . any ( ) ) . describe ( "New value for the item" ) ,
288+ itemId : CatalogItemIdSchema . describe ( "ID of the catalog item" ) ,
289+ value : CatalogDocumentFieldsSchema . describe ( "New value for the item" ) ,
222290} ) ;
223291
224292export type ReplaceCatalogItemParams = z . infer <
0 commit comments