Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
40 lines
1004 B
TypeScript
40 lines
1004 B
TypeScript
import { z } from 'zod';
|
|
|
|
export const convertValueBySchema = (value: unknown, schema: any): unknown => {
|
|
if (!schema || !value) return value;
|
|
|
|
if (typeof value === 'string') {
|
|
if (schema instanceof z.ZodNumber) {
|
|
return Number(value);
|
|
} else if (schema instanceof z.ZodBoolean) {
|
|
return value.toLowerCase() === 'true';
|
|
} else if (schema instanceof z.ZodObject) {
|
|
try {
|
|
const parsed = JSON.parse(value);
|
|
return convertValueBySchema(parsed, schema);
|
|
} catch {
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (schema instanceof z.ZodObject && typeof value === 'object' && value !== null) {
|
|
const result: any = {};
|
|
for (const [key, val] of Object.entries(value)) {
|
|
const fieldSchema = schema.shape[key];
|
|
if (fieldSchema) {
|
|
result[key] = convertValueBySchema(val, fieldSchema);
|
|
} else {
|
|
result[key] = val;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return value;
|
|
};
|
|
|
|
export const convertObjectBySchema = (obj: any, schema: any): any => {
|
|
return convertValueBySchema(obj, schema);
|
|
};
|