0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

feat: astro:env allow schema keys to include numbers (#11437)

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
Ben 2024-07-10 12:32:56 +01:00 committed by GitHub
parent ea4bc04e94
commit 6ccb30e610
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a case where Astro's config `experimental.env.schema` keys did not allow numbers. Numbers are still not allowed as the first character to be able to generate valid JavaScript identifiers

View file

@ -81,12 +81,18 @@ const EnvFieldMetadata = z.union([
SecretServerEnvFieldMetadata, SecretServerEnvFieldMetadata,
]); ]);
const KEY_REGEX = /^[A-Z_]+$/; const EnvSchemaKey = z
.string()
.min(1)
.refine(([firstChar]) => isNaN(Number.parseInt(firstChar)), {
message: 'A valid variable name cannot start with a number.',
})
.refine((str) => /^[A-Z0-9_]+$/.test(str), {
message: 'A valid variable name can only contain uppercase letters, numbers and underscores.',
});
export const EnvSchema = z.record( export const EnvSchema = z.record(
z.string().regex(KEY_REGEX, { EnvSchemaKey,
message: 'A valid variable name can only contain uppercase letters and underscores.',
}),
z.intersection(EnvFieldMetadata, EnvFieldType) z.intersection(EnvFieldMetadata, EnvFieldType)
); );

View file

@ -3,6 +3,7 @@ import { describe, it } from 'node:test';
import stripAnsi from 'strip-ansi'; import stripAnsi from 'strip-ansi';
import { z } from 'zod'; import { z } from 'zod';
import { validateConfig } from '../../../dist/core/config/validate.js'; import { validateConfig } from '../../../dist/core/config/validate.js';
import { envField } from '../../../dist/env/config.js';
import { formatConfigErrorMessage } from '../../../dist/core/messages.js'; import { formatConfigErrorMessage } from '../../../dist/core/messages.js';
describe('Config Validation', () => { describe('Config Validation', () => {
@ -367,5 +368,42 @@ describe('Config Validation', () => {
).catch((err) => err) ).catch((err) => err)
); );
}); });
it('Should allow schema variables with numbers', () => {
assert.doesNotThrow(() =>
validateConfig(
{
experimental: {
env: {
schema: {
ABC123: envField.string({ access: 'public', context: 'server' }),
},
},
},
},
process.cwd()
).catch((err) => err)
);
});
it('Should not allow schema variables starting with a number', async () => {
const configError = await validateConfig(
{
experimental: {
env: {
schema: {
"123ABC": envField.string({ access: 'public', context: 'server' }),
},
},
},
},
process.cwd()
).catch((err) => err);
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'A valid variable name cannot start with a number.'
);
});
}); });
}); });