Merge pull request #82 from meienberger/feature/field-url
Feature/field url
This commit is contained in:
commit
a3c0109edd
3 changed files with 62 additions and 19 deletions
|
@ -5,8 +5,8 @@
|
|||
"id": "homarr",
|
||||
"description": "A homepage for your server.",
|
||||
"short_desc": "Homarr is a simple and lightweight homepage for your server, that helps you easily access all of your services in one place.",
|
||||
"author": "https://github.com/ajnart/",
|
||||
"source": "https://github.com/ajnart/homar",
|
||||
"author": "ajnart",
|
||||
"source": "https://github.com/ajnart/homarr",
|
||||
"website": "https://discord.gg/C2WTXkzkwK",
|
||||
"image": "https://raw.githubusercontent.com/ajnart/homarr/master/public/imgs/logo.png",
|
||||
"form_fields": {}
|
||||
|
|
|
@ -1,27 +1,69 @@
|
|||
import validator from 'validator';
|
||||
import { AppConfig, FieldTypes } from '../../core/types';
|
||||
|
||||
const validateField = (field: AppConfig['form_fields'][0], value: string): string | undefined => {
|
||||
if (field.required && !value) {
|
||||
return `${field.label} is required`;
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (field.type) {
|
||||
case FieldTypes.text:
|
||||
if (field.max && value.length > field.max) {
|
||||
return `${field.label} must be less than ${field.max} characters`;
|
||||
}
|
||||
if (field.min && value.length < field.min) {
|
||||
return `${field.label} must be at least ${field.min} characters`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.password:
|
||||
if (!validator.isLength(value, { min: field.min, max: field.max })) {
|
||||
return `${field.label} must be between ${field.min} and ${field.max} characters`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.email:
|
||||
if (!validator.isEmail(value)) {
|
||||
return `${field.label} must be a valid email address`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.number:
|
||||
if (!validator.isNumeric(value)) {
|
||||
return `${field.label} must be a number`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.fqdn:
|
||||
if (!validator.isFQDN(value)) {
|
||||
return `${field.label} must be a valid domain`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.ip:
|
||||
if (!validator.isIP(value)) {
|
||||
return `${field.label} must be a valid IP address`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.fqdnip:
|
||||
if (!validator.isFQDN(value || '') && !validator.isIP(value)) {
|
||||
return `${field.label} must be a valid domain or IP address`;
|
||||
}
|
||||
break;
|
||||
case FieldTypes.url:
|
||||
if (!validator.isURL(value)) {
|
||||
return `${field.label} must be a valid URL`;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
export const validateAppConfig = (values: Record<string, string>, fields: (AppConfig['form_fields'][0] & { id: string })[]) => {
|
||||
const errors: any = {};
|
||||
|
||||
fields.forEach((field) => {
|
||||
if (field.required && !values[field.id]) {
|
||||
errors[field.id] = 'Field required';
|
||||
} else if (values[field.id] && field.min && values[field.id].length < field.min) {
|
||||
errors[field.id] = `Field must be at least ${field.min} characters long`;
|
||||
} else if (values[field.id] && field.max && values[field.id].length > field.max) {
|
||||
errors[field.id] = `Field must be at most ${field.max} characters long`;
|
||||
} else if (values[field.id] && field.type === FieldTypes.number && !validator.isNumeric(values[field.id])) {
|
||||
errors[field.id] = 'Field must be a number';
|
||||
} else if (values[field.id] && field.type === FieldTypes.email && !validator.isEmail(values[field.id])) {
|
||||
errors[field.id] = 'Field must be a valid email';
|
||||
} else if (values[field.id] && field.type === FieldTypes.fqdn && !validator.isFQDN(values[field.id] || '')) {
|
||||
errors[field.id] = 'Field must be a valid domain';
|
||||
} else if (values[field.id] && field.type === FieldTypes.ip && !validator.isIP(values[field.id])) {
|
||||
errors[field.id] = 'Field must be a valid IP address';
|
||||
} else if (values[field.id] && field.type === FieldTypes.fqdnip && !validator.isFQDN(values[field.id] || '') && !validator.isIP(values[field.id])) {
|
||||
errors[field.id] = 'Field must be a valid domain or IP address';
|
||||
}
|
||||
errors[field.id] = validateField(field, values[field.id]);
|
||||
});
|
||||
|
||||
return errors;
|
||||
|
|
|
@ -6,6 +6,7 @@ export enum FieldTypes {
|
|||
fqdn = 'fqdn',
|
||||
ip = 'ip',
|
||||
fqdnip = 'fqdnip',
|
||||
url = 'url',
|
||||
}
|
||||
|
||||
interface FormField {
|
||||
|
|
Loading…
Add table
Reference in a new issue