Skip to content

Commit a3129b9

Browse files
committed
Merge branch 'validate-metadata-dynamically' of Arnei/opencast-admin-interface into main
Pull request #800 Fixes #317 Fix required metadata not being required
2 parents 7ef6da7 + 36c967a commit a3129b9

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

src/components/events/partials/wizards/NewEventWizard.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import NewMetadataPage from "../ModalTabsAndPages/NewMetadataPage";
77
import NewAccessPage from "../ModalTabsAndPages/NewAccessPage";
88
import NewProcessingPage from "../ModalTabsAndPages/NewProcessingPage";
99
import NewSourcePage from "../ModalTabsAndPages/NewSourcePage";
10-
import { NewEventSchema } from "../../../../utils/validate";
10+
import { NewEventSchema, MetadataSchema } from "../../../../utils/validate";
1111
import WizardStepperEvent from "../../../shared/wizard/WizardStepperEvent";
1212
import { getInitialMetadataFieldValues } from "../../../../utils/resourceUtils";
1313
import { sourceMetadata } from "../../../../configs/sourceConfig";
@@ -98,7 +98,12 @@ const NewEventWizard: React.FC<{
9898
];
9999

100100
// Validation schema of current page
101-
const currentValidationSchema = NewEventSchema[page];
101+
let currentValidationSchema;
102+
if (page === 0 || page === 1) {
103+
currentValidationSchema = MetadataSchema(metadataFields.fields);
104+
} else {
105+
currentValidationSchema = NewEventSchema[page];
106+
}
102107

103108
const nextPage = (values: typeof initialValues) => {
104109
setSnapshot(values);

src/components/events/partials/wizards/NewSeriesWizard.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import NewMetadataExtendedPage from "../ModalTabsAndPages/NewMetadataExtendedPag
1212
import NewAccessPage from "../ModalTabsAndPages/NewAccessPage";
1313
import WizardStepper from "../../../shared/wizard/WizardStepper";
1414
import { initialFormValuesNewSeries } from "../../../../configs/modalConfig";
15-
import { NewSeriesSchema } from "../../../../utils/validate";
15+
import { MetadataSchema, NewSeriesSchema } from "../../../../utils/validate";
1616
import { getInitialMetadataFieldValues } from "../../../../utils/resourceUtils";
1717
import { useAppDispatch, useAppSelector } from "../../../../store";
1818
import { TobiraPage, postNewSeries } from "../../../../slices/seriesSlice";
@@ -81,7 +81,12 @@ const NewSeriesWizard: React.FC<{
8181
];
8282

8383
// Validation schema of current page
84-
const currentValidationSchema = NewSeriesSchema[page];
84+
let currentValidationSchema;
85+
if (page === 0 || page === 1) {
86+
currentValidationSchema = MetadataSchema(metadataFields.fields);
87+
} else {
88+
currentValidationSchema = NewSeriesSchema[page];
89+
}
8590

8691
const nextPage = (
8792
values: {

src/utils/validate.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Yup from "yup";
2+
import { MetadataField } from "../slices/eventSlice";
23

34
/**
45
* This File contains all schemas used for validation with yup in the context of events and series
@@ -7,11 +8,70 @@ import * as Yup from "yup";
78
const today = new Date();
89
today.setHours(0, 0, 0, 0);
910

11+
/**
12+
* Dynamically create a schema for a required metadata field
13+
*/
14+
export function createMetadataSchema(
15+
schema: { [key: string]: any; },
16+
config: { id: string; required: boolean; type: string; })
17+
{
18+
const { id, required, type } = config;
19+
if (!required) return schema;
20+
21+
let validationType: "string" | "array" | "date" = "string";
22+
const validations: {
23+
type: string,
24+
params: any[],
25+
}[] = [
26+
{
27+
type: "required",
28+
params: ["this field is required"]
29+
},
30+
]
31+
32+
if (type === "mixed_text") {
33+
validationType = "array";
34+
validations.push({
35+
type: "min",
36+
params: [1, "there should be atleast one entry"]
37+
})
38+
}
39+
40+
if (type === "date" || type === "start_date") {
41+
validationType = "date";
42+
}
43+
44+
if (!Yup[validationType as keyof typeof Yup]) {
45+
return schema;
46+
}
47+
let validator = Yup[validationType as "string"]();
48+
validations.forEach(validation => {
49+
const { params, type } = validation;
50+
// @ts-expect-error
51+
if (!validator[type]) {
52+
return;
53+
}
54+
// @ts-expect-error
55+
validator = validator[type](...params);
56+
});
57+
schema[id] = validator;
58+
return schema;
59+
}
60+
61+
/**
62+
* Dynamically create a schema for required metadata fields
63+
*/
64+
export const MetadataSchema = (fields: MetadataField[]) => {
65+
const schema = fields.reduce(createMetadataSchema, {});
66+
const validateSchema = Yup.object().shape(schema);
67+
68+
return validateSchema;
69+
}
70+
71+
1072
// Validation Schema used in new event wizard (each step has its own yup validation object)
1173
export const NewEventSchema = [
12-
Yup.object().shape({
13-
title: Yup.string().required("Required"),
14-
}),
74+
Yup.object().shape({}),
1575
Yup.object().shape({}),
1676
Yup.object().shape({
1777
uploadAssetsTrack: Yup.array().when("sourceMode", {

0 commit comments

Comments
 (0)