Skip to content

Commit 15393c9

Browse files
committed
feat(react-sdk): return discriminated union from safeSchemaToJsonSchema
Change `safeSchemaToJsonSchema` to return a discriminated union result object instead of `JSONSchema7 | undefined`. This makes error handling explicit and allows callers to decide how strictly to handle failures. - Introduce `SafeSchemaToJsonSchemaResult` type with `success` discriminator - Return a structured error when no schema is provided - Preserve console error logging while surfacing the error message - Keep `schemaToJsonSchema` behavior unchanged, still delegating to `toJsonSchema.sync` for standard schemas
1 parent 8bdf46f commit 15393c9

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

react-sdk/src/util/schema.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,48 @@ export function schemaToJsonSchema(schema: Schema): JSONSchema7 {
177177
return toJsonSchema.sync(schema) as JSONSchema7;
178178
}
179179

180+
export type SafeSchemaToJsonSchemaResult =
181+
| { schema: JSONSchema7; success: true }
182+
| { schema: undefined; success: false; error: string };
183+
180184
/**
181-
* Safely converts a schema to JSON Schema, returning undefined for invalid inputs.
185+
* Safely converts a schema to JSON Schema.
186+
*
187+
* Instead of throwing on conversion errors, this returns a discriminated union
188+
* so callers can decide how strictly to handle failures.
182189
* @param schema - The schema to convert (may be undefined)
183-
* @returns The JSON Schema representation, or undefined if conversion fails
190+
* @returns A result object containing the converted schema on success, or an
191+
* error message on failure
184192
*/
185193
export function safeSchemaToJsonSchema(
186194
schema: Schema | undefined | null,
187-
): JSONSchema7 | undefined {
195+
): SafeSchemaToJsonSchemaResult {
188196
if (!schema) {
189-
return undefined;
197+
return {
198+
schema: undefined,
199+
success: false,
200+
error: "No schema provided",
201+
};
190202
}
191203

192204
try {
193-
return schemaToJsonSchema(schema);
205+
const jsonSchema = schemaToJsonSchema(schema);
206+
return {
207+
schema: jsonSchema,
208+
success: true,
209+
};
194210
} catch (error) {
211+
const message =
212+
error instanceof Error
213+
? error.message
214+
: "Unknown error converting schema to JSON Schema";
215+
195216
console.error("Error converting schema to JSON Schema:", error);
196-
return undefined;
217+
218+
return {
219+
schema: undefined,
220+
success: false,
221+
error: message,
222+
};
197223
}
198224
}

0 commit comments

Comments
 (0)