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
+31 -19

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 }),
})); }));
}; };
+20 -14
View File
@@ -91,13 +91,17 @@ const credentials = joi.object({
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
url: joi url: joi.when('type', {
.string() is: 'docker',
.trim() then: joi.string()
.custom((value, helpers) => { .trim().regex(/^[a-z0-9]{64}$/),
// Regex from https://gist.github.com/dperini/729294 otherwise: joi
var urlRegex = new RegExp( .string()
"^" + .trim()
.custom((value, helpers) => {
// Regex from https://gist.github.com/dperini/729294
var urlRegex = new RegExp(
"^" +
// protocol identifier (optional) // protocol identifier (optional)
// short syntax // still required // short syntax // still required
"(?:(?:https?|ftp):\\/\\/)?" + "(?:(?:https?|ftp):\\/\\/)?" +
@@ -130,18 +134,20 @@ const monitorValidation = joi.object({
// resource path (optional) // resource path (optional)
"(?:[/?#]\\S*)?" + "(?:[/?#]\\S*)?" +
"$", "$",
"i" "i"
); );
if (!urlRegex.test(value)) { if (!urlRegex.test(value)) {
return helpers.error("string.invalidUrl"); return helpers.error("string.invalidUrl");
} }
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({