Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve astro:env config error #12912

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-readers-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improves the config error for invalid combinations of `context` and `access` properties under `env.schema`
19 changes: 18 additions & 1 deletion packages/astro/src/env/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,28 @@ const SecretServerEnvFieldMetadata = z.object({
context: z.literal('server'),
access: z.literal('secret'),
});
const EnvFieldMetadata = z.union([
const _EnvFieldMetadata = z.union([
PublicClientEnvFieldMetadata,
PublicServerEnvFieldMetadata,
SecretServerEnvFieldMetadata,
]);
const EnvFieldMetadata = z.custom<z.input<typeof _EnvFieldMetadata>>().superRefine((data, ctx) => {
const result = _EnvFieldMetadata.safeParse(data);
if (result.success) {
return;
}
ematipico marked this conversation as resolved.
Show resolved Hide resolved
for (const issue of result.error.issues) {
if (issue.code === z.ZodIssueCode.invalid_union) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `**Invalid combination** of "access" and "context" options:\n Secret client variables are not supported. Please review the configuration of \`env.schema.${ctx.path.at(-1)}\`.\n Learn more at https://docs.astro.build/en/guides/environment-variables/#variable-types`,
path: ['context', 'access'],
});
} else {
ctx.addIssue(issue);
}
}
});

const EnvSchemaKey = z
.string()
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/test/units/config/config-validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,25 @@ describe('Config Validation', () => {
'A valid variable name cannot start with a number.',
);
});

it('Should provide a useful error for access/context invalid combinations', async () => {
const configError = await validateConfig(
{
env: {
schema: {
BAR: envField.string({ access: 'secret', context: 'client' }),
},
},
},
process.cwd(),
).catch((err) => err);
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message.includes(
'**Invalid combination** of "access" and "context" options',
),
true,
);
});
});
});
Loading