chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { validateCustomFields } from "@planarchy/engine";
|
||||
import { BlueprintTarget, type BlueprintFieldDefinition } from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
interface BlueprintLookup {
|
||||
blueprint: {
|
||||
findUnique: (args: {
|
||||
where: { id: string };
|
||||
select: { fieldDefs: true; target: true };
|
||||
}) => Promise<{ fieldDefs: unknown; target: string } | null>;
|
||||
};
|
||||
}
|
||||
|
||||
interface AssertBlueprintDynamicFieldsInput {
|
||||
db: BlueprintLookup;
|
||||
blueprintId: string | undefined;
|
||||
dynamicFields: Record<string, unknown>;
|
||||
target: BlueprintTarget;
|
||||
}
|
||||
|
||||
export async function assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId,
|
||||
dynamicFields,
|
||||
target,
|
||||
}: AssertBlueprintDynamicFieldsInput): Promise<void> {
|
||||
if (!blueprintId) return;
|
||||
|
||||
const blueprint = await db.blueprint.findUnique({
|
||||
where: { id: blueprintId },
|
||||
select: { fieldDefs: true, target: true },
|
||||
});
|
||||
|
||||
if (!blueprint) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Blueprint not found" });
|
||||
}
|
||||
|
||||
if (blueprint.target !== target) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `${target} entities require a ${target.toLowerCase()} blueprint`,
|
||||
});
|
||||
}
|
||||
|
||||
const fieldDefs = blueprint.fieldDefs as BlueprintFieldDefinition[];
|
||||
const errors = validateCustomFields(fieldDefs, dynamicFields);
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new TRPCError({
|
||||
code: "UNPROCESSABLE_CONTENT",
|
||||
message: errors.map((error) => error.message).join("; "),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user