(schema: string | object)
| 864 | * @returns {ICommonObject} |
| 865 | */ |
| 866 | export const convertSchemaToZod = (schema: string | object): ICommonObject => { |
| 867 | try { |
| 868 | const parsedSchema = typeof schema === 'string' ? JSON.parse(schema) : schema |
| 869 | const zodObj: ICommonObject = {} |
| 870 | for (const sch of parsedSchema) { |
| 871 | if (sch.type === 'string') { |
| 872 | if (sch.required) { |
| 873 | zodObj[sch.property] = z.string({ required_error: `${sch.property} required` }).describe(sch.description) |
| 874 | } else { |
| 875 | zodObj[sch.property] = z.string().describe(sch.description).optional() |
| 876 | } |
| 877 | } else if (sch.type === 'number') { |
| 878 | if (sch.required) { |
| 879 | zodObj[sch.property] = z.number({ required_error: `${sch.property} required` }).describe(sch.description) |
| 880 | } else { |
| 881 | zodObj[sch.property] = z.number().describe(sch.description).optional() |
| 882 | } |
| 883 | } else if (sch.type === 'boolean') { |
| 884 | if (sch.required) { |
| 885 | zodObj[sch.property] = z.boolean({ required_error: `${sch.property} required` }).describe(sch.description) |
| 886 | } else { |
| 887 | zodObj[sch.property] = z.boolean().describe(sch.description).optional() |
| 888 | } |
| 889 | } else if (sch.type === 'date') { |
| 890 | if (sch.required) { |
| 891 | zodObj[sch.property] = z.date({ required_error: `${sch.property} required` }).describe(sch.description) |
| 892 | } else { |
| 893 | zodObj[sch.property] = z.date().describe(sch.description).optional() |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | return zodObj |
| 898 | } catch (e) { |
| 899 | throw new Error(e) |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Flatten nested object |
no test coverage detected