-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathschema.ts
224 lines (208 loc) · 6.24 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as yup from "yup";
import {
Application,
IReadFile,
FileLoadError,
TargetLabel,
Target,
} from "@app/api/models";
import { useTranslation } from "react-i18next";
import { useAnalyzableApplicationsByMode } from "./utils";
import { customURLValidation } from "@app/utils/utils";
export const ANALYSIS_MODES = [
"binary",
"source-code",
"source-code-deps",
"binary-upload",
] as const;
export type AnalysisMode = (typeof ANALYSIS_MODES)[number];
export type AnalysisScope = "app" | "app,oss" | "app,oss,select";
export interface ModeStepValues {
mode: AnalysisMode;
artifact: File | undefined | null;
}
const useModeStepSchema = ({
applications,
}: {
applications: Application[];
}): yup.SchemaOf<ModeStepValues> => {
const { t } = useTranslation();
const analyzableAppsByMode = useAnalyzableApplicationsByMode(applications);
return yup.object({
mode: yup
.mixed<AnalysisMode>()
.required(t("validation.required"))
.test("isModeCompatible", t("validation.unsupportedMode"), (mode) => {
const analyzableApplications = mode ? analyzableAppsByMode[mode] : [];
if (mode === "binary-upload") {
return analyzableApplications.length === 1;
}
return analyzableApplications.length > 0;
}),
artifact: yup.mixed<File>().when("mode", {
is: "binary-upload",
then: (schema) => schema.required(t("validation.requiredFile")),
}),
});
};
export interface TargetsStepValues {
formLabels: TargetLabel[];
selectedTargets: Target[];
targetFilters?: Record<string, string[]>;
}
const useTargetsStepSchema = (): yup.SchemaOf<TargetsStepValues> => {
return yup.object({
formLabels: yup.array(),
selectedTargets: yup.array(),
targetFilters: yup.object(),
});
};
export interface ScopeStepValues {
withKnownLibs: AnalysisScope;
includedPackages: string[];
hasExcludedPackages: boolean;
excludedPackages: string[];
}
const useScopeStepSchema = (): yup.SchemaOf<ScopeStepValues> => {
const { t } = useTranslation();
return yup.object({
withKnownLibs: yup
.mixed<AnalysisScope>()
.required(t("validation.required")),
includedPackages: yup
.array()
.of(yup.string().defined())
.when("withKnownLibs", {
is: (withKnownLibs: AnalysisScope) => withKnownLibs.includes("select"),
then: (schema) =>
schema.min(1, t("validation.minOneRequired", { type: "package" })),
otherwise: (schema) => schema,
}),
hasExcludedPackages: yup.bool().defined(),
excludedPackages: yup
.array()
.of(yup.string().defined())
.when("hasExcludedPackages", {
is: true,
then: (schema) =>
schema.min(
1,
t("validation.minOneRequired", { type: "excluded package" })
),
otherwise: (schema) => schema,
}),
});
};
export interface CustomRulesStepValues {
customRulesFiles: IReadFile[];
rulesKind: string;
repositoryType?: string;
sourceRepository?: string;
branch?: string;
rootPath?: string;
associatedCredentials?: string;
}
export const customRulesFilesSchema: yup.SchemaOf<IReadFile> = yup.object({
fileName: yup.string().required(),
fullFile: yup.mixed<File>(),
loadError: yup.mixed<FileLoadError>(),
loadPercentage: yup.number(),
loadResult: yup.mixed<"danger" | "success" | undefined>(),
data: yup.string(),
responseID: yup.number(),
});
const useCustomRulesStepSchema = (): yup.SchemaOf<CustomRulesStepValues> => {
const { t } = useTranslation();
return yup.object({
rulesKind: yup.string().defined(),
customRulesFiles: yup
.array()
.of(customRulesFilesSchema)
.when("rulesKind", {
is: "manual",
then: yup.array().of(customRulesFilesSchema),
otherwise: (schema) => schema,
})
.when(["formLabels", "rulesKind", "selectedTargets"], {
is: (
labels: TargetLabel[],
rulesKind: string,
selectedTargets: number
) =>
labels.length === 0 && rulesKind === "manual" && selectedTargets <= 0,
then: (schema) =>
schema.min(1, t("validation.minOneRequired", { type: "rule file" })),
}),
repositoryType: yup.mixed<string>().when("rulesKind", {
is: "repository",
then: yup.mixed<string>().required(),
}),
sourceRepository: yup.string().when("rulesKind", {
is: "repository",
then: (schema) =>
customURLValidation(schema).required(
t("validation.requiredRepositoryURL")
),
}),
branch: yup.mixed<string>().when("rulesKind", {
is: "repository",
then: yup.mixed<string>(),
}),
rootPath: yup.mixed<string>().when("rulesKind", {
is: "repository",
then: yup.mixed<string>(),
}),
associatedCredentials: yup.mixed<any>().when("rulesKind", {
is: "repository",
then: yup.mixed<any>(),
}),
});
};
export interface OptionsStepValues {
excludedRulesTags: string[];
autoTaggingEnabled: boolean;
advancedAnalysisEnabled: boolean;
selectedSourceLabels: TargetLabel[];
}
const useOptionsStepSchema = (): yup.SchemaOf<OptionsStepValues> => {
const { t } = useTranslation();
return yup.object({
excludedRulesTags: yup.array().of(yup.string().defined()),
autoTaggingEnabled: yup.bool().defined(),
advancedAnalysisEnabled: yup.bool().defined(),
selectedSourceLabels: yup.array().of(
yup.object().shape({
name: yup.string().defined(),
label: yup.string().defined(),
})
),
});
};
export type AnalysisWizardFormValues = ModeStepValues &
TargetsStepValues &
ScopeStepValues &
CustomRulesStepValues &
OptionsStepValues;
export const useAnalysisWizardFormValidationSchema = ({
applications,
}: {
applications: Application[];
}) => {
const schemas = {
modeStep: useModeStepSchema({ applications }),
targetsStep: useTargetsStepSchema(),
scopeStep: useScopeStepSchema(),
customRulesStep: useCustomRulesStepSchema(),
optionsStep: useOptionsStepSchema(),
};
const allFieldsSchema: yup.SchemaOf<AnalysisWizardFormValues> =
schemas.modeStep
.concat(schemas.targetsStep)
.concat(schemas.scopeStep)
.concat(schemas.customRulesStep)
.concat(schemas.optionsStep);
return {
schemas,
allFieldsSchema,
};
};