release: v0.20.0
This commit is contained in:
@@ -7,22 +7,37 @@
|
||||
* an input for, that meant a save of `accent: rot` or nine columns in a 1–6
|
||||
* field was reported as "Saved" while the site showed something else.
|
||||
*
|
||||
* This checks exactly those fields — the site URL, the accent, the grid
|
||||
* columns and gap — with the resolvers' own rules and bounds, so it can never
|
||||
* be stricter than what renders. Kept apart from settingsSchema.ts so the
|
||||
* This checks the values a resolver bends — the site URL, the accent, the
|
||||
* corner radius, the grid columns and gap, the message retention and the
|
||||
* header links — with the resolvers' own rules and bounds, so it can never be
|
||||
* stricter than what renders. Kept apart from settingsSchema.ts so the
|
||||
* settings form can run it as you type without bundling zod; the save route
|
||||
* runs it before writing, and the doctor over settings.yaml as it is.
|
||||
* runs it before writing, and both doctors over settings.yaml as it is.
|
||||
*
|
||||
* `npm run doctor` imports this file under Node's own type stripping
|
||||
* (scripts/doctor.mts), which resolves no extensionless paths: every relative
|
||||
* import here carries its `.ts`, and the modules it pulls in may only
|
||||
* `import type` from elsewhere. scripts/__tests__/doctor-cli.test.ts loads the
|
||||
* graph in a real Node process and fails when that breaks.
|
||||
*/
|
||||
|
||||
import { normaliseSiteUrl } from '../siteUrl';
|
||||
import { isAccentHex } from './theme';
|
||||
import { PHOTO_GRID_COLUMNS_MAX, PHOTO_GRID_COLUMNS_MIN, PHOTO_GRID_GAP_MAX } from './schema';
|
||||
import { normaliseSiteUrl } from '../siteUrl.ts';
|
||||
import { isAccentHex, THEME_RADIUS_MAX } from './theme.ts';
|
||||
import {
|
||||
CONTACT_RETENTION_MAX,
|
||||
PHOTO_GRID_COLUMNS_MAX,
|
||||
PHOTO_GRID_COLUMNS_MIN,
|
||||
PHOTO_GRID_GAP_MAX,
|
||||
} from './schema.ts';
|
||||
import type { SettingsFieldError } from './settingsSchema';
|
||||
|
||||
function inRange(value: unknown, min: number, max: number): boolean {
|
||||
return typeof value === 'number' && Number.isInteger(value) && value >= min && value <= max;
|
||||
}
|
||||
|
||||
/** What sanitizeNavLinks (lib/config/index.ts) keeps as a link target. */
|
||||
const NAV_LINK_URL = /^https?:\/\//i;
|
||||
|
||||
/** One error per offending field; an empty string counts as unset, as in the resolvers. */
|
||||
export function validateSettingValues(input: unknown): SettingsFieldError[] {
|
||||
if (!input || typeof input !== 'object') return [];
|
||||
@@ -30,6 +45,8 @@ export function validateSettingValues(input: unknown): SettingsFieldError[] {
|
||||
url?: unknown;
|
||||
theme?: unknown;
|
||||
grid?: { columns?: unknown; gap?: unknown } | null;
|
||||
contact?: { retentionDays?: unknown } | null;
|
||||
navLinks?: unknown;
|
||||
};
|
||||
const errors: SettingsFieldError[] = [];
|
||||
|
||||
@@ -42,13 +59,33 @@ export function validateSettingValues(input: unknown): SettingsFieldError[] {
|
||||
});
|
||||
}
|
||||
|
||||
const theme =
|
||||
s.theme && typeof s.theme === 'object'
|
||||
? (s.theme as { accent?: unknown; radius?: unknown })
|
||||
: null;
|
||||
|
||||
// resolveTheme: anything but hex falls back to the preset's accent.
|
||||
const accent =
|
||||
s.theme && typeof s.theme === 'object' ? (s.theme as { accent?: unknown }).accent : undefined;
|
||||
const accent = theme?.accent;
|
||||
if (typeof accent === 'string' && accent.trim() !== '' && !isAccentHex(accent)) {
|
||||
errors.push({ field: 'theme.accent', message: 'Must be a hex colour such as #e60012.' });
|
||||
}
|
||||
|
||||
// resolveRadius clamps into 0–THEME_RADIUS_MAX. The panel has no input for
|
||||
// it, so the message says where the value lives.
|
||||
const radius = theme?.radius;
|
||||
if (
|
||||
radius != null &&
|
||||
(typeof radius !== 'number' ||
|
||||
!Number.isFinite(radius) ||
|
||||
radius < 0 ||
|
||||
radius > THEME_RADIUS_MAX)
|
||||
) {
|
||||
errors.push({
|
||||
field: 'theme.radius',
|
||||
message: `Must be a number from 0 to ${THEME_RADIUS_MAX} (theme.radius in settings.yaml).`,
|
||||
});
|
||||
}
|
||||
|
||||
// getConfig clamps both into these bounds (lib/config/index.ts).
|
||||
const grid = s.grid && typeof s.grid === 'object' ? s.grid : null;
|
||||
if (
|
||||
@@ -67,5 +104,36 @@ export function validateSettingValues(input: unknown): SettingsFieldError[] {
|
||||
});
|
||||
}
|
||||
|
||||
// resolveContact rounds and clamps into 1–CONTACT_RETENTION_MAX.
|
||||
const contact = s.contact && typeof s.contact === 'object' ? s.contact : null;
|
||||
if (contact?.retentionDays != null && !inRange(contact.retentionDays, 1, CONTACT_RETENTION_MAX)) {
|
||||
errors.push({
|
||||
field: 'contact.retentionDays',
|
||||
message: `Must be a whole number of days from 1 to ${CONTACT_RETENTION_MAX}.`,
|
||||
});
|
||||
}
|
||||
|
||||
// sanitizeNavLinks drops an entry without a label or without an http(s)
|
||||
// URL, whole — so each half is reported against its own input.
|
||||
if (Array.isArray(s.navLinks)) {
|
||||
s.navLinks.forEach((entry: unknown, i) => {
|
||||
const link = entry && typeof entry === 'object' ? (entry as Record<string, unknown>) : {};
|
||||
const label = typeof link.label === 'string' ? link.label.trim() : '';
|
||||
const url = typeof link.url === 'string' ? link.url.trim() : '';
|
||||
if (!label) {
|
||||
errors.push({
|
||||
field: `navLinks.${i}.label`,
|
||||
message: 'Needs a label, or the link is left out.',
|
||||
});
|
||||
}
|
||||
if (!NAV_LINK_URL.test(url)) {
|
||||
errors.push({
|
||||
field: `navLinks.${i}.url`,
|
||||
message: 'Must be a full http(s) address, or the link is left out.',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user