Skip to content

Node.js based forum software built for the modern web

refactor: one owner for the custom user field type rules (#14860)

palmoni53757db29a094
diff --git a/src/controllers/accounts/helpers.js b/src/controllers/accounts/helpers.js
index cfd78a20a9..3af83bbfd8 100644
--- a/src/controllers/accounts/helpers.js
+++ b/src/controllers/accounts/helpers.js
@@ -199,7 +199,7 @@ helpers.getCustomUserFields = async function (callerUID, userData) {
userValue = utils.isSafeHref(userValue) ? userValue : '';
f.linkValue = String(userValue.replace('http://', '').replace('https://', ''));
}
- f['select-options'] = (f['select-options'] || '').split('\n').filter(Boolean);
+ f['select-options'] = user.customFields.getOptions(f);
if (f.type === 'select') {
f['select-options'].unshift('');
}
diff --git a/src/controllers/admin/users.js b/src/controllers/admin/users.js
index b2818421c9..4ca219ed8f 100644
--- a/src/controllers/admin/users.js
+++ b/src/controllers/admin/users.js
@@ -255,7 +255,7 @@ usersController.customFields = async function (req, res) {
const fields = await user.customFields.getFields();
fields.forEach((field) => {
if (field['select-options']) {
- field.selectOptionsFormatted = field['select-options'].trim().split('\n').join(', ');
+ field.selectOptionsFormatted = user.customFields.getOptions(field).join(', ');
}
field['min:rep'] = field['min:rep'] || 0;
field.visibility = field.visibility || 'all';
diff --git a/src/user/custom-fields.js b/src/user/custom-fields.js
index 684c0979cf..631cd938a0 100644
--- a/src/user/custom-fields.js
+++ b/src/user/custom-fields.js
@@ -1,6 +1,10 @@
'use strict';
+const validator = require('validator');
+
const db = require('../database');
+const utils = require('../utils');
+const tx = require('../translator');
const CustomFields = module.exports;
@@ -27,3 +31,61 @@ CustomFields.setFields = async function (fields) {
fields.map(field => [`user-custom-field:${field.key}`, field])
);
};
+
+CustomFields.getOptions = function (field) {
+ return (field['select-options'] || '').split('\n').filter(Boolean);
+};
+
+CustomFields.validate = function (field, value) {
+ const { type } = field;
+
+ if (typeof value === 'string' && value.length > 255) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-value-too-long', field.name
+ ));
+ }
+
+ const isUrl = value && validator.isURL(String(value).trim(), {
+ require_protocol: true,
+ require_valid_protocol: true,
+ require_tld: true,
+ });
+
+ if (value && type === 'input-number' && !utils.isNumber(value)) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-invalid-number', field.name
+ ));
+ } else if (value && type === 'input-text' && isUrl) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-invalid-text', field.name
+ ));
+ } else if (value && type === 'input-date' && !validator.isDate(value)) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-invalid-date', field.name
+ ));
+ } else if (value && type === 'input-link' && !isUrl) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-invalid-link', field.name
+ ));
+ } else if (type === 'select') {
+ const opts = CustomFields.getOptions(field);
+ if (!opts.includes(value) && value !== '') {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-select-value-invalid', field.name
+ ));
+ }
+ } else if (type === 'select-multi') {
+ const opts = CustomFields.getOptions(field);
+ let values;
+ try {
+ values = JSON.parse(value || '[]');
+ } catch (err) {
+ values = null;
+ }
+ if (!Array.isArray(values) || !values.every(value => opts.includes(value))) {
+ throw new Error(tx.compile(
+ 'error:custom-user-field-select-value-invalid', field.name
+ ));
+ }
+ }
+};
diff --git a/src/user/profile.js b/src/user/profile.js
index 8a36d61012..6ac455f6f4 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -93,66 +93,18 @@ module.exports = function (User) {
const reputation = await User.getUserField(data.uid, 'reputation');
fields.forEach((field) => {
- const { key, type } = field;
- if (data.hasOwnProperty(key)) {
- const value = data[key];
- const minRep = field['min:rep'] || 0;
- if (reputation < minRep && !meta.config['reputation:disabled']) {
- throw new Error(tx.compile(
- 'error:not-enough-reputation-custom-field', minRep, field.name
- ));
- }
-
- if (typeof value === 'string' && value.length > 255) {
- throw new Error(tx.compile(
- 'error:custom-user-field-value-too-long', field.name
- ));
- }
-
- const isUrl = value && validator.isURL(String(value).trim(), {
- require_protocol: true,
- require_valid_protocol: true,
- require_tld: true,
- });
-
- if (value && type === 'input-number' && !utils.isNumber(value)) {
- throw new Error(tx.compile(
- 'error:custom-user-field-invalid-number', field.name
- ));
- } else if (value && type === 'input-text' && isUrl) {
- throw new Error(tx.compile(
- 'error:custom-user-field-invalid-text', field.name
- ));
- } else if (value && type === 'input-date' && !validator.isDate(value)) {
- throw new Error(tx.compile(
- 'error:custom-user-field-invalid-date', field.name
- ));
- } else if (value && field.type === 'input-link' && !isUrl) {
- throw new Error(tx.compile(
- 'error:custom-user-field-invalid-link', field.name
- ));
- } else if (field.type === 'select') {
- const opts = (field['select-options'] || '').split('\n').filter(Boolean);
- if (!opts.includes(value) && value !== '') {
- throw new Error(tx.compile(
- 'error:custom-user-field-select-value-invalid', field.name
- ));
- }
- } else if (field.type === 'select-multi') {
- const opts = (field['select-options'] || '').split('\n').filter(Boolean);
- let values;
- try {
- values = JSON.parse(value || '[]');
- } catch (err) {
- values = null;
- }
- if (!Array.isArray(values) || !values.every(value => opts.includes(value))) {
- throw new Error(tx.compile(
- 'error:custom-user-field-select-value-invalid', field.name
- ));
- }
- }
+ if (!data.hasOwnProperty(field.key)) {
+ return;
+ }
+
+ const minRep = field['min:rep'] || 0;
+ if (reputation < minRep && !meta.config['reputation:disabled']) {
+ throw new Error(tx.compile(
+ 'error:not-enough-reputation-custom-field', minRep, field.name
+ ));
}
+
+ User.customFields.validate(field, data[field.key]);
});
}