141
});
142
}
143
});
144
+
145
+describe('schema defects fail at construction', () => {
146
+ // `@cfworker/json-schema` resolves references and compiles regexes inside
147
+ // `validate`, so each of these used to reach the caller once per call, as an
148
+ // input validation failure. The consumer wraps construction in its own
149
+ // `Effect.try` and reports a compile failure against the cached schema path,
150
+ // which is the accurate diagnosis for all of them.
151
+ const defects: ReadonlyArray<readonly [string, JsonSchema, RegExp]> = [
152
+ [
153
+ 'an invalid patternProperties regular expression',
154
+ { type: 'object', patternProperties: { '[': { type: 'string' } } },
155
+ /Invalid patternProperties regular expression/,
156
+ ],
157
+ [
158
+ 'an invalid patternProperties regular expression nested under a property',
159
+ {
160
+ type: 'object',
161
+ properties: { a: { type: 'object', patternProperties: { '(': { type: 'string' } } } },
162
+ },
163
+ /Invalid patternProperties regular expression/,
164
+ ],
165
+ [
166
+ 'an invalid patternProperties regular expression inside array items',
167
+ {
168
+ type: 'object',
169
+ properties: {
170
+ a: { type: 'array', items: { type: 'object', patternProperties: { '(?<': {} } } },
171
+ },
172
+ },
173
+ /Invalid patternProperties regular expression/,
174
+ ],
175
+ [
176
+ 'an unresolved local $ref in a patternProperties subschema',
177
+ { type: 'object', patternProperties: { '^s_': { $ref: '#/$defs/Missing' } } },
178
+ /Unresolved \$ref .* in a dynamic-key schema/,
179
+ ],
180
+ [
181
+ 'an external $ref in a patternProperties subschema',
182
+ { type: 'object', patternProperties: { '^s_': { $ref: 'https://example.com/s.json' } } },
183
+ /Unresolved \$ref .* in a dynamic-key schema/,
184
+ ],
185
+ [
186
+ 'an unresolved local $ref in a schema-valued additionalProperties',
187
+ { type: 'object', additionalProperties: { $ref: '#/$defs/Missing' } },
188
+ /Unresolved \$ref .* in a dynamic-key schema/,
189
+ ],
190
+ [
191
+ 'a dynamic-key reference whose target carries a dangling reference',
192
+ {
193
+ type: 'object',
194
+ additionalProperties: { $ref: '#/$defs/Hop' },
195
+ $defs: { Hop: { type: 'object', properties: { a: { $ref: '#/$defs/Missing' } } } },
196
+ },
197
+ /Unresolved \$ref .* in a dynamic-key schema/,
198
+ ],
199
+ [
200
+ 'a duplicate schema URI',
201
+ {
202
+ $id: 'https://example.com/a',
203
+ type: 'object',
204
+ $defs: { x: { $id: 'https://example.com/a', type: 'string' } },
205
+ },
206
+ /Duplicate schema URI/,
207
+ ],
208
+ ];
209
+
210
+ // Deliberately outside the scope above: matching the Python SDK, which only
211
+ // rejects dynamic-key schemas while wrapping a tool. Widening to every `$ref`
212
+ // would reject tool schemas whose dangling reference sits in a branch the
213
+ // interpreter never descends into, and which validate today.
214
+ const toleratedOutsideDynamicKeys: ReadonlyArray<readonly [string, JsonSchema]> = [
215
+ [
216
+ 'an unresolved $ref under a declared property',
217
+ { type: 'object', properties: { a: { $ref: '#/$defs/Missing' } } },
218
+ ],
219
+ [
220
+ 'an unresolved $ref inside array items',
221
+ { type: 'object', properties: { a: { type: 'array', items: { $ref: '#/$defs/Gone' } } } },
222
+ ],
223
+ [
224
+ 'an invalid pattern regular expression',
225
+ { type: 'object', properties: { a: { type: 'string', pattern: '[' } } },
226
+ ],
227
+ ];
228
+
229
+ for (const [label, schema] of toleratedOutsideDynamicKeys) {
230
+ it(`leaves ${label} to the interpreter`, () => {
231
+ expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow();
232
+ });
233
+ }
234
+
235
+ for (const [label, schema, message] of defects) {
236
+ it(`rejects ${label}`, () => {
237
+ expect(() => jsonSchemaToEffectSchema(schema)).toThrow(message);
238
+ });
239
+ }
240
+
241
+ it('resolves a local $ref that does exist', () => {
242
+ const schema = {
243
+ type: 'object',
244
+ properties: { a: { $ref: '#/$defs/Name' } },
245
+ required: ['a'],
246
+ $defs: { Name: { type: 'string' } },
247
+ } satisfies JsonSchema;
248
+
249
+ expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow();
250
+ expect(effectAccepts(schema, { a: 'Ada' })).toBe(true);
251
+ expect(effectAccepts(schema, { a: 1 })).toBe(false);
252
+ });
253
+
254
+ it('accepts a self-referential $ref without re-entering it', () => {
255
+ const schema = {
256
+ type: 'object',
257
+ properties: { next: { $ref: '#' } },
258
+ } satisfies JsonSchema;
259
+
260
+ expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow();
261
+ expect(effectAccepts(schema, { next: { next: {} } })).toBe(true);
262
+ });
263
+});
264
+
265
+describe('input the interpreter cannot represent stays an input failure', () => {
266
+ // The one throw in `validate` that depends on the instance rather than the
267
+ // schema. It must stay inside the filter: throwing here would escape
268
+ // `Schema.decodeUnknown` as a defect instead of a typed parse error.
269
+ const unrepresentable: ReadonlyArray<readonly [string, unknown]> = [
270
+ ['a bigint', BigInt(1)],
271
+ ['a function', () => 1],
272
+ ['a symbol', Symbol('x')],
273
+ ['undefined', undefined],
274
+ ];
275
+
276
+ for (const [label, input] of unrepresentable) {
277
+ it(`rejects ${label} without throwing`, () => {
278
+ const schema = jsonSchemaToEffectSchema({ type: 'object' });
279
+ expect(() => Schema.decodeUnknownOption(schema)(input)).not.toThrow();
280
+ expect(Option.isSome(Schema.decodeUnknownOption(schema)(input))).toBe(false);
281
+ });
282
+ }
283
+});