MCPcopy Create free account
hub / github.com/FlowiseAI/Flowise / convertSchemaToZod

Function convertSchemaToZod

packages/components/src/utils.ts:866–901  ·  view source on GitHub ↗
(schema: string | object)

Source from the content-addressed store, hash-verified

864 * @returns {ICommonObject}
865 */
866export 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

Callers 3

initMethod · 0.90
initMethod · 0.90
runLLMEvaluatorsMethod · 0.85

Calls 1

parseMethod · 0.65

Tested by

no test coverage detected