(schema: ZodObject<Shape>)
| 101 | |
| 102 | // Runtime impl |
| 103 | export function zodToFlags<Shape extends Record<string, ZodTypeAny>>(schema: ZodObject<Shape>): FlagsFromSchema<Shape> { |
| 104 | const flags: Record<string, Flag<any>> = {}; |
| 105 | |
| 106 | for (const [key, def] of Object.entries(schema.shape)) { |
| 107 | const description = def.description ?? ''; |
| 108 | |
| 109 | const {array, default: defaultValue, optional, type} = unwrap(def); |
| 110 | |
| 111 | if (type instanceof z.ZodString) { |
| 112 | flags[key] = Flags.string({ |
| 113 | description, |
| 114 | required: !optional, |
| 115 | default: defaultValue, |
| 116 | multiple: array as any, // Gets around a type issue |
| 117 | }); |
| 118 | } else if (type instanceof z.ZodNumber) { |
| 119 | flags[key] = Flags.integer({ |
| 120 | description, |
| 121 | required: !optional, |
| 122 | default: defaultValue, |
| 123 | multiple: array as any, // Gets around a type issue |
| 124 | }); |
| 125 | } else if (type instanceof z.ZodBoolean) { |
| 126 | flags[key] = Flags.boolean({ |
| 127 | description, |
| 128 | default: defaultValue, |
| 129 | }); |
| 130 | } else if (type instanceof z.ZodEnum) { |
| 131 | flags[key] = Flags.string({ |
| 132 | description, |
| 133 | required: !optional, |
| 134 | default: defaultValue, |
| 135 | options: type.options, |
| 136 | multiple: array as any, // Gets around a type issue |
| 137 | }); |
| 138 | } else { |
| 139 | throw new Error(`Unsupported Zod type for flag: ${key}`); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return flags as FlagsFromSchema<Shape>; |
| 144 | } |
| 145 | |
| 146 | export function zodToArgs<Shape extends Record<string, ZodTypeAny>>(schema: ZodObject<Shape>): ArgsFromSchema<Shape> { |
| 147 | const args: Record<string, Arg<any>> = {}; |
no test coverage detected