refactor(schema): simplify validation and OpenAPI generation (#2709)

This commit is contained in:
Hampus authored and GitHub committed 2026-09-11 23:24:26 +02:00
1 parent 4bc5593f9f
commit de1fd95a99
510 files changed
+29494 -36131

No files matched your search

+7 -37
View File
@@ -1,37 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {ValidationErrorCode} from '@fluxer/constants/src/ValidationErrorCodes';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {isValidationErrorCode, ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {z} from 'zod';
const validationErrorCodeSet = new Set<string>(Object.values(ValidationErrorCodes));
type FluxerZodErrorMapIssue = z.core.$ZodRawIssue;
type FluxerZodErrorMapResult =
| {
message: string;
}
| string
| undefined
| null;
function isValidationErrorCode(value: string): value is ValidationErrorCode {
return validationErrorCodeSet.has(value);
}
function getParamsProperty(obj: object): Record<string, unknown> | undefined {
if ('params' in obj) {
const value = (
obj as {
params?: unknown;
}
).params;
return value !== null && typeof value === 'object' ? (value as Record<string, unknown>) : undefined;
}
return undefined;
}
function fluxerZodErrorMap(issue: FluxerZodErrorMapIssue): FluxerZodErrorMapResult {
const fluxerZodErrorMap: z.core.$ZodErrorMap = (issue) => {
if (issue.message && isValidationErrorCode(issue.message)) {
return {message: issue.message};
}
@@ -58,8 +31,7 @@ function fluxerZodErrorMap(issue: FluxerZodErrorMapIssue): FluxerZodErrorMapResu
break;
}
case 'too_big': {
const origin = 'origin' in issue ? String(issue.origin) : undefined;
if (origin === 'string') {
if (issue.origin === 'string') {
errorCode = ValidationErrorCodes.CONTENT_EXCEEDS_MAX_LENGTH;
} else {
errorCode = ValidationErrorCodes.INVALID_FORMAT;
@@ -67,10 +39,9 @@ function fluxerZodErrorMap(issue: FluxerZodErrorMapIssue): FluxerZodErrorMapResu
break;
}
case 'invalid_format': {
const format = 'format' in issue ? String(issue.format) : undefined;
if (format === 'email') {
if (issue.format === 'email') {
errorCode = ValidationErrorCodes.INVALID_EMAIL_ADDRESS;
} else if (format === 'uuid') {
} else if (issue.format === 'uuid') {
errorCode = ValidationErrorCodes.INVALID_SNOWFLAKE;
} else {
errorCode = ValidationErrorCodes.INVALID_FORMAT;
@@ -82,8 +53,7 @@ function fluxerZodErrorMap(issue: FluxerZodErrorMapIssue): FluxerZodErrorMapResu
break;
}
case 'custom': {
const params = getParamsProperty(issue);
const customErrorCode = params?.['error_code'];
const customErrorCode = issue.params?.['error_code'];
errorCode =
typeof customErrorCode === 'string' && isValidationErrorCode(customErrorCode)
? customErrorCode
@@ -101,7 +71,7 @@ function fluxerZodErrorMap(issue: FluxerZodErrorMapIssue): FluxerZodErrorMapResu
}
}
return {message: errorCode};
}
};
export function initializeFluxerErrorMap(): void {
z.config({customError: fluxerZodErrorMap});