Environment Variable that is a JSON object

Hi I have been trying to load a environment variable that is a JSON object (ie to load Google Cloud Service Account). In my env.mjs I added the following
const server = z.object({

GCS_KEY: z.preprocess(
(str) => JSON.parse(String(str)),
z.object({
type: z.string().min(1),
project_id: z.string().min(1),
private_key_id: z.string().min(1),
private_key: z.string().min(1),
client_email: z.string().min(1),
client_id: z.string().min(1),
auth_uri: z.string().min(1),
token_uri: z.string().min(1),
auth_provider_x509_cert_url: z.string().min(1),
client_x509_cert_url: z.string().min(1),
})
)

})

const processEnv = {
GCS_KEY: process.env.GCS_KEY
}
const server = z.object({

GCS_KEY: z.preprocess(
(str) => JSON.parse(String(str)),
z.object({
type: z.string().min(1),
project_id: z.string().min(1),
private_key_id: z.string().min(1),
private_key: z.string().min(1),
client_email: z.string().min(1),
client_id: z.string().min(1),
auth_uri: z.string().min(1),
token_uri: z.string().min(1),
auth_provider_x509_cert_url: z.string().min(1),
client_x509_cert_url: z.string().min(1),
})
)

})

const processEnv = {
GCS_KEY: process.env.GCS_KEY
}
but I am getting an error in the MergedOuput type, saying that some fields are missing. Any help would be greatly appreciated. Thanks
4 Replies
Sybatron
Sybatron2y ago
Stack Overflow
Is it possible to store a JSON file to an ENV variable with dotenv?
I am using Google Drive API with my Rails application. The API is working fine. I have the following client_secret.json file: { "type": "service_account", "project_id": "gobirdie-landing-page"...
sweetleche
sweetleche2y ago
yes - i am doing that - but the MJS file is complaining about a type issue - seen in the screenshot
Dosha
Dosha2y ago
stringify it and store it as a string, then json parse it during load
sweetleche
sweetleche2y ago
is that not what i am doing with preprocess?
Want results from more Discord servers?
Add your server