Skip to content

Commit c200a2a

Browse files
Add configurable excluded options for unsupported query parameters
1 parent 9cb24c9 commit c200a2a

5 files changed

Lines changed: 53 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ npm start
7373
| `AVIF_EXIF` | `1` | Enable EXIF metadata for AVIF (1 = on, 0 = off). |
7474
| `JSON` | `1` | Enable the JSON endpoint (1 = on, 0 = off). |
7575
| `QUERY_STRING_ARRAY_LIMIT_MIN` | `20` | Minimum number of values allowed per array parameter. |
76+
| `EXCLUDED_OPTIONS` | `idRandomization,fontFamily,fontWeight,title` | Comma-separated list of option names to exclude. |
7677
| `QUERY_STRING_PARAMETER_LIMIT_MIN` | `100` | Minimum number of query string parameters allowed. |
7778

7879
> [!NOTE]

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const config: Config = {
2828
cacheControl: {
2929
avatar: Number(process.env.CACHE_CONTROL_AVATARS ?? 60 * 60 * 24 * 365),
3030
},
31+
excludedOptions: (
32+
process.env.EXCLUDED_OPTIONS ?? 'idRandomization,fontFamily,fontWeight,title'
33+
).split(',').filter(Boolean),
3134
queryString: {
3235
arrayLimitMin: Number(process.env.QUERY_STRING_ARRAY_LIMIT_MIN ?? 20),
3336
parameterLimitMin: Number(

src/routes/style.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { createRequire } from 'node:module';
1111
const require = createRequire(import.meta.url);
1212
const optionsSchema = require('@dicebear/schema/options.json');
1313

14+
for (const key of config.excludedOptions) {
15+
delete optionsSchema.properties[key];
16+
}
17+
1418
const paramsSchema = {
1519
type: 'object' as const,
1620
properties: {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type Config = {
4646
logger: boolean;
4747
workers: number;
4848
versions: number[];
49+
excludedOptions: string[];
4950
png: ImageFormatConfig;
5051
jpeg: ImageFormatConfig;
5152
webp: ImageFormatConfig;

tests/excluded-options.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { test, describe } from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import { createRequire } from 'node:module';
4+
5+
import { config } from '../dist/config.js';
6+
7+
const require = createRequire(import.meta.url);
8+
9+
describe('excludedOptions', () => {
10+
test('default config excludes idRandomization, fontFamily, fontWeight, title', () => {
11+
assert.deepEqual(config.excludedOptions, [
12+
'idRandomization',
13+
'fontFamily',
14+
'fontWeight',
15+
'title',
16+
]);
17+
});
18+
19+
test('excluded options are removed from options schema', () => {
20+
// Load a fresh copy of the schema to compare
21+
const originalSchema = JSON.parse(
22+
JSON.stringify(require('@dicebear/schema/options.json')),
23+
);
24+
25+
for (const key of config.excludedOptions) {
26+
assert.ok(
27+
key in originalSchema.properties,
28+
`"${key}" should exist in the original schema`,
29+
);
30+
}
31+
32+
// Simulate the filtering logic from routes/style.ts
33+
for (const key of config.excludedOptions) {
34+
delete originalSchema.properties[key];
35+
}
36+
37+
for (const key of config.excludedOptions) {
38+
assert.ok(
39+
!(key in originalSchema.properties),
40+
`"${key}" should be removed after filtering`,
41+
);
42+
}
43+
});
44+
});

0 commit comments

Comments
 (0)