Files
leistungsbilanz-ts/src/shared/validation/protection-device.schemas.ts
T

96 lines
3.1 KiB
TypeScript

import { z } from "zod";
import {
breakerTripCharacteristics,
fuseProtectionDeviceTypes,
fuseUtilizationCategories,
isAllowedRatedCurrentA,
isAllowedRatedResidualCurrentMa,
protectionDeviceTypes,
rcdTypes,
} from "../constants/protection-device.js";
function hasValue(value: unknown): boolean {
return value !== undefined;
}
export const protectionDeviceConfigurationSchema = z
.object({
type: z.enum(protectionDeviceTypes),
ratedCurrentA: z.number().finite().positive(),
fuseUtilizationCategory: z.enum(fuseUtilizationCategories).optional(),
tripCharacteristic: z.enum(breakerTripCharacteristics).optional(),
rcdType: z.enum(rcdTypes).optional(),
ratedResidualCurrentMa: z
.number()
.int()
.refine(isAllowedRatedResidualCurrentMa, {
message: "Der Bemessungsdifferenzstrom ist nicht zulässig.",
})
.optional(),
})
.strict()
.superRefine((value, context) => {
if (!isAllowedRatedCurrentA(value.type, value.ratedCurrentA)) {
context.addIssue({
code: "custom",
path: ["ratedCurrentA"],
message: "Der Bemessungsstrom ist für diesen Schutzgerätetyp nicht zulässig.",
});
}
const isFuse = (fuseProtectionDeviceTypes as readonly string[]).includes(
value.type
);
const hasBreakerFunction =
value.type === "LS" || value.type === "FI_LS" || value.type === "AFDD";
const hasResidualCurrentFunction =
value.type === "FI" || value.type === "FI_LS";
if (isFuse !== hasValue(value.fuseUtilizationCategory)) {
context.addIssue({
code: "custom",
path: ["fuseUtilizationCategory"],
message: isFuse
? "Für diesen Sicherungstyp ist eine Sicherungscharakteristik erforderlich."
: "Dieser Schutzgerätetyp darf keine Sicherungscharakteristik enthalten.",
});
}
if (hasBreakerFunction !== hasValue(value.tripCharacteristic)) {
context.addIssue({
code: "custom",
path: ["tripCharacteristic"],
message: hasBreakerFunction
? "Für diesen Schutzgerätetyp ist eine Auslösecharakteristik erforderlich."
: "Dieser Schutzgerätetyp darf keine Auslösecharakteristik enthalten.",
});
}
if (hasResidualCurrentFunction !== hasValue(value.rcdType)) {
context.addIssue({
code: "custom",
path: ["rcdType"],
message: hasResidualCurrentFunction
? "Für diesen Schutzgerätetyp ist ein FI-Typ erforderlich."
: "Dieser Schutzgerätetyp darf keinen FI-Typ enthalten.",
});
}
if (
hasResidualCurrentFunction !==
hasValue(value.ratedResidualCurrentMa)
) {
context.addIssue({
code: "custom",
path: ["ratedResidualCurrentMa"],
message: hasResidualCurrentFunction
? "Für diesen Schutzgerätetyp ist ein Bemessungsdifferenzstrom erforderlich."
: "Dieser Schutzgerätetyp darf keinen Bemessungsdifferenzstrom enthalten.",
});
}
});
export type ProtectionDeviceConfigurationInput = z.infer<
typeof protectionDeviceConfigurationSchema
>;