Fix validation error of container id

This commit is contained in:
YinDongfang committed 2025-02-01 22:24:15 +08:00
1 parent 89abb02338
commit 067667da1e
2 files changed
+18 -6

No files matched your search

+11 -5
View File
@@ -88,7 +88,7 @@ const CreateMonitor = () => {
? `http${https ? "s" : ""}://` + monitor.url ? `http${https ? "s" : ""}://` + monitor.url
: monitor.url, : monitor.url,
port: monitor.type === "port" ? monitor.port : undefined, port: monitor.type === "port" ? monitor.port : undefined,
name: monitor.name === "" ? monitor.url : monitor.name, name: monitor.name || monitor.url.substring(0, 50),
type: monitor.type, type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE, interval: monitor.interval * MS_PER_MINUTE,
}; };
@@ -122,7 +122,7 @@ const CreateMonitor = () => {
form = { form = {
...form, ...form,
description: form.name, description: monitor.name || monitor.url,
teamId: user.teamId, teamId: user.teamId,
userId: user._id, userId: user._id,
notifications: monitor.notifications, notifications: monitor.notifications,
@@ -138,17 +138,23 @@ const CreateMonitor = () => {
const handleChange = (event, formName) => { const handleChange = (event, formName) => {
const { value } = event.target; const { value } = event.target;
setMonitor({
const newMonitor = {
...monitor, ...monitor,
[formName]: value, [formName]: value,
}); };
if (formName === 'type') {
newMonitor.url = '';
}
setMonitor(newMonitor);
const { error } = monitorValidation.validate( const { error } = monitorValidation.validate(
{ [formName]: value }, { type: monitor.type, [formName]: value },
{ abortEarly: false } { abortEarly: false }
); );
setErrors((prev) => ({ setErrors((prev) => ({
...prev, ...prev,
url: undefined,
...(error ? { [formName]: error.details[0].message } : { [formName]: undefined }), ...(error ? { [formName]: error.details[0].message } : { [formName]: undefined }),
})); }));
}; };
+7 -1
View File
@@ -91,7 +91,11 @@ const credentials = joi.object({
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
url: joi url: joi.when('type', {
is: 'docker',
then: joi.string()
.trim().regex(/^[a-z0-9]{64}$/),
otherwise: joi
.string() .string()
.trim() .trim()
.custom((value, helpers) => { .custom((value, helpers) => {
@@ -138,10 +142,12 @@ const monitorValidation = joi.object({
return value; return value;
}) })
})
.messages({ .messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
"string.invalidUrl": "Please enter a valid URL with optional port", "string.invalidUrl": "Please enter a valid URL with optional port",
"string.pattern.base": "Please enter a valid container ID.",
}), }),
port: joi.number(), port: joi.number(),
name: joi.string().trim().max(50).allow("").messages({ name: joi.string().trim().max(50).allow("").messages({