44
55open S
66
7- // ─── Item inputs ──── ─────────────────────────────────────────────────────────────
7+ // ─── Item schemas ─────────────────────────────────────────────────────────────
88
99// POST /rest/items single object body
1010let createItem = schema (s => ({
@@ -25,7 +25,7 @@ let createItemBody = schema(s =>
2525 ])
2626)
2727
28- // PUT /rest/items/:id
28+ // PUT /rest/items/:id — body carries fields only, id comes from URL param
2929let replaceItem = schema (s => ({
3030 name : s .field ("name" , s .string -> String .min (1 )),
3131 description : s .field ("description" , s .option (s .string )),
@@ -42,20 +42,79 @@ let replaceItemWithId = schema(s => ({
4242
4343let replaceItems = schema (s => s .array (replaceItemWithId ))
4444
45+ // PATCH /rest/items/:id — all fields optional
46+ let patchItem = schema (s => ({
47+ name : s .field ("name" , s .option (s .string -> String .min (1 ))),
48+ description : s .field ("description" , s .option (s .string )),
49+ categoryId : s .field ("categoryId" , s .option (s .int )),
50+ }: Schema .Items .patchRow ))
51+
4552// DELETE /rest/items bulk body: [{id}, {id}]
4653let deleteItemsBody = schema (s =>
4754 s .array (schema (s => s .field ("id" , s .int )))
4855)
4956
50- // ─── Parse helpers ──────────────────────────────────────────────────────────────
57+ // ─── Category schemas ──────────────────────────────────────────────────────────
58+
59+ // POST /rest/categories
60+ let createCategory = schema (s => ({
61+ name : s .field ("name" , s .string -> String .min (1 )),
62+ description : s .field ("description" , s .option (s .string )),
63+ }: Schema .Categories .insertRow ))
64+
65+ let createCategories = schema (s => s .array (createCategory ))
66+
67+ let createCategoryBody = schema (s =>
68+ s .union ([
69+ schema (s => [s .matches (createCategory )]),
70+ schema (s => s .matches (createCategories )),
71+ ])
72+ )
73+
74+ // PUT /rest/categories/:id
75+ let replaceCategory = schema (s => ({
76+ name : s .field ("name" , s .string -> String .min (1 )),
77+ description : s .field ("description" , s .option (s .string )),
78+ }: Schema .Categories .insertRow ))
79+
80+ // PUT /rest/categories bulk
81+ let replaceCategoryWithId = schema (s => ({
82+ id : s .field ("id" , s .int ),
83+ name : s .field ("name" , s .string -> String .min (1 )),
84+ description : s .field ("description" , s .option (s .string )),
85+ }: Schema .Categories .replaceRow ))
86+
87+ let replaceCategories = schema (s => s .array (replaceCategoryWithId ))
88+
89+ // PATCH /rest/categories/:id — all fields optional
90+ let patchCategory = schema (s => ({
91+ name : s .field ("name" , s .option (s .string -> String .min (1 ))),
92+ description : s .field ("description" , s .option (s .string )),
93+ }: Schema .Categories .patchRow ))
94+
95+ // DELETE /rest/categories bulk body
96+ let deleteCategoriesBody = schema (s =>
97+ s .array (schema (s => s .field ("id" , s .int )))
98+ )
99+
100+ // ─── Parse helpers ─────────────────────────────────────────────────────────────
51101
52102let parse = (schema , json ): result <'a , AppError .t > =>
53103 switch schema -> S .parseOrThrow (json ) {
54104 | v => Ok (v )
55105 | exception S .Error (e ) => Error (AppError .ValidationError ([S .Error .message (e )]))
56106 }
57107
58- let parseCreateBody = (json : Js .Json .t ) => parse (createItemBody , json )
59- let parseReplace = (json : Js .Json .t ) => parse (replaceItem , json )
60- let parseReplaceMany = (json : Js .Json .t ) => parse (replaceItems , json )
61- let parseDeleteMany = (json : Js .Json .t ) => parse (deleteItemsBody , json )
108+ // Items
109+ let parseCreateBody = (json : Js .Json .t ) => parse (createItemBody , json )
110+ let parseReplace = (json : Js .Json .t ) => parse (replaceItem , json )
111+ let parseReplaceMany = (json : Js .Json .t ) => parse (replaceItems , json )
112+ let parsePatch = (json : Js .Json .t ) => parse (patchItem , json )
113+ let parseDeleteMany = (json : Js .Json .t ) => parse (deleteItemsBody , json )
114+
115+ // Categories
116+ let parseCategoryBody = (json : Js .Json .t ) => parse (createCategoryBody , json )
117+ let parseCategoryReplace = (json : Js .Json .t ) => parse (replaceCategory , json )
118+ let parseCategoryReplaceMany = (json : Js .Json .t ) => parse (replaceCategories , json )
119+ let parseCategoryPatch = (json : Js .Json .t ) => parse (patchCategory , json )
120+ let parseCategoryDeleteMany = (json : Js .Json .t ) => parse (deleteCategoriesBody , json )
0 commit comments