Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate ODP Schema project types on publish #3386

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api.planx.uk/modules/flows/validate/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Node } from "@opensystemslab/planx-core/types";
import { ValidatedRequestHandler } from "../../../shared/middleware/validate";
import { z } from "zod";
import { validateAndDiffFlow } from "./service";
import { validateAndDiffFlow } from "./service/index";
import { ServerError } from "../../../errors";

interface ValidateAndDiffResponse {
Expand Down
278 changes: 0 additions & 278 deletions api.planx.uk/modules/flows/validate/service.ts

This file was deleted.

76 changes: 76 additions & 0 deletions api.planx.uk/modules/flows/validate/service/fileTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getValidSchemaValues } from "@opensystemslab/planx-core";
import {
ComponentType,
FlowGraph,
Node,
} from "@opensystemslab/planx-core/types";
import countBy from "lodash/countBy";

import { isComponentType } from "../helpers";
import { FlowValidationResponse } from "./index";

const validateFileTypes = (flowGraph: FlowGraph): FlowValidationResponse => {
// Get all passport variables set by FileUpload and/or FileUploadAndLabel
const allFileFns = [
...getFileUploadNodeFns(flowGraph),
...getFileUploadAndLabelNodeFns(flowGraph),
];
if (allFileFns.length < 1) {
return {
title: "File types",
status: "Not applicable",
message: "Your flow is not using FileUpload or UploadAndLabel",
};
}

// Get all file types supported by current release of ODP Schema & compare
const validFileTypes = getValidSchemaValues("FileType");
const invalidFileFns: string[] = [];
allFileFns.forEach((fn) => {
if (!validFileTypes?.includes(fn)) {
invalidFileFns.push(fn);
}
});
if (invalidFileFns.length > 0) {
// Get unique fns with count of occurances
const countInvalidFileFns = countBy(invalidFileFns);
const summarisedInvalidFileFns: string[] = [];
Object.entries(countInvalidFileFns).map(([k, v]: [string, number]) => {
summarisedInvalidFileFns.push(`${k} (${v})`);
});
return {
title: "File types",
status: "Warn",
message: `Your FileUpload or UploadAndLabel are setting data fields that are not supported by the current release of the ODP Schema: ${summarisedInvalidFileFns.join(", ")}`,
};
}

return {
title: "File types",
status: "Pass",
message:
"Files collected via FileUpload or UploadAndLabel are all supported by the ODP Schema",
};
};

const getFileUploadNodeFns = (flowGraph: FlowGraph): string[] => {
const fileUploadNodes = Object.entries(flowGraph).filter((entry) =>
isComponentType(entry, ComponentType.FileUpload),
);
return fileUploadNodes.map(([_nodeId, node]) => node.data?.fn as string);
};

const getFileUploadAndLabelNodeFns = (flowGraph: FlowGraph): string[] => {
// Exclude Upload & Label nodes used in "info-only" mode with a hidden dropzone
const uploadAndLabelNodes = Object.entries(flowGraph).filter(
(entry) =>
isComponentType(entry, ComponentType.FileUploadAndLabel) &&
entry[1].data?.hideDropZone !== true,
);
const uploadAndLabelFileTypes = uploadAndLabelNodes
.map(([_nodeId, node]: [string, Node]) => node.data?.fileTypes)
.flat();
return uploadAndLabelFileTypes?.map((file: any) => file?.fn as string);

Check warning on line 73 in api.planx.uk/modules/flows/validate/service/fileTypes.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

Unexpected any. Specify a different type
};

export { validateFileTypes };
Loading
Loading