#Convert dymnamic object into zod type

3 messages · Page 1 of 1 (latest)

mighty fjord
#

aiming to convert a dynamic JavaScript object into a Zod schema, specifically focusing on the colors property to create an enum of its values. The challenge lies in the dynamic nature of the config object, where the colors property can have varying keys and values.

const config = {

        theme: {},

        extend: {

            colors: {

                emerald: '#ff00ff',

                lightred: '#f5424b'

            }

            // sizes:{


            // }

        },
        components: []

    };```
tawny bridge
#

i'm not a zod user, but if i'm understanding what you want i think you'd create a custom schema for hex colors (probably just implemented via a regex or something) and then use a record for the overall colors object (e.g. z.record(z.string(), HexColor))

mighty fjord