@@ -61,4 +61,148 @@ describe('webhooks', () => {
61
61
} )
62
62
} )
63
63
} )
64
+
65
+ describe ( 'validation' , ( ) => {
66
+ test ( 'allows metafields when API version is 2025-04' , ( ) => {
67
+ // Given
68
+ const object = {
69
+ webhooks : {
70
+ api_version : '2025-04' ,
71
+ subscriptions : [
72
+ {
73
+ topics : [ 'orders/create' ] ,
74
+ uri : 'https://example.com/webhooks' ,
75
+ metafields : [ { namespace : 'custom' , key : 'test' } ] ,
76
+ } ,
77
+ ] ,
78
+ } ,
79
+ }
80
+
81
+ // When
82
+ const result = spec . schema . safeParse ( object )
83
+
84
+ // Then
85
+ expect ( result . success ) . toBe ( true )
86
+ } )
87
+
88
+ test ( 'allows metafields when API version is unstable' , ( ) => {
89
+ // Given
90
+ const object = {
91
+ webhooks : {
92
+ api_version : 'unstable' ,
93
+ subscriptions : [
94
+ {
95
+ topics : [ 'orders/create' ] ,
96
+ uri : 'https://example.com/webhooks' ,
97
+ metafields : [ { namespace : 'custom' , key : 'test' } ] ,
98
+ } ,
99
+ ] ,
100
+ } ,
101
+ }
102
+
103
+ // When
104
+ const result = spec . schema . safeParse ( object )
105
+
106
+ // Then
107
+ expect ( result . success ) . toBe ( true )
108
+ } )
109
+
110
+ test ( 'rejects metafields when API version is before 2025-04' , ( ) => {
111
+ // Given
112
+ const object = {
113
+ webhooks : {
114
+ api_version : '2024-01' ,
115
+ subscriptions : [
116
+ {
117
+ topics : [ 'orders/create' ] ,
118
+ uri : 'https://example.com/webhooks' ,
119
+ metafields : [ { namespace : 'custom' , key : 'test' } ] ,
120
+ } ,
121
+ ] ,
122
+ } ,
123
+ }
124
+
125
+ // When
126
+ const result = spec . schema . safeParse ( object )
127
+
128
+ // Then
129
+ expect ( result . success ) . toBe ( false )
130
+ expect ( result . error . issues [ 0 ] . message ) . toBe (
131
+ 'Webhook metafields are only supported in API version 2025-04 or later, or with version "unstable"' ,
132
+ )
133
+ } )
134
+
135
+ test ( 'allows configuration without metafields in older API versions' , ( ) => {
136
+ // Given
137
+ const object = {
138
+ webhooks : {
139
+ api_version : '2024-01' ,
140
+ subscriptions : [
141
+ {
142
+ topics : [ 'orders/create' ] ,
143
+ uri : 'https://example.com/webhooks' ,
144
+ } ,
145
+ ] ,
146
+ } ,
147
+ }
148
+
149
+ // When
150
+ const result = spec . schema . safeParse ( object )
151
+
152
+ // Then
153
+ expect ( result . success ) . toBe ( true )
154
+ } )
155
+
156
+ test ( 'allows empty metafields array in supported API versions' , ( ) => {
157
+ // Given
158
+ const object = {
159
+ webhooks : {
160
+ api_version : '2025-04' ,
161
+ subscriptions : [
162
+ {
163
+ topics : [ 'orders/create' ] ,
164
+ uri : 'https://example.com/webhooks' ,
165
+ metafields : [ ] ,
166
+ } ,
167
+ ] ,
168
+ } ,
169
+ }
170
+
171
+ // When
172
+ const result = spec . schema . safeParse ( object )
173
+
174
+ // Then
175
+ expect ( result . success ) . toBe ( true )
176
+ } )
177
+
178
+ test ( 'rejects malformed metafields' , ( ) => {
179
+ // Given
180
+ const object = {
181
+ webhooks : {
182
+ api_version : '2025-04' ,
183
+ subscriptions : [
184
+ {
185
+ topics : [ 'orders/create' ] ,
186
+ uri : 'https://example.com/webhooks' ,
187
+ metafields : [
188
+ {
189
+ // Missing required 'key' property
190
+ namespace : 'custom' ,
191
+ // Invalid additional property
192
+ invalid_property : 'test' ,
193
+ } ,
194
+ ] ,
195
+ } ,
196
+ ] ,
197
+ } ,
198
+ }
199
+
200
+ // When
201
+ const result = spec . schema . safeParse ( object )
202
+
203
+ // Then
204
+ expect ( result . success ) . toBe ( false )
205
+ expect ( result . error . issues [ 0 ] . message ) . toMatch ( / R e q u i r e d / )
206
+ } )
207
+ } )
64
208
} )
0 commit comments