From 86a1327259da5c1ed573bd4a3fd29c3acb5ef018 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 29 Feb 2020 00:31:56 +0000 Subject: [PATCH 1/3] don't show "This alias is available to use" if an alias is not provided Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/elements/RoomAliasField.js | 1 + src/components/views/elements/Validation.js | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/views/elements/RoomAliasField.js b/src/components/views/elements/RoomAliasField.js index 007bd07a47..0139a9596b 100644 --- a/src/components/views/elements/RoomAliasField.js +++ b/src/components/views/elements/RoomAliasField.js @@ -92,6 +92,7 @@ export default class RoomAliasField extends React.PureComponent { invalid: () => _t("Please provide a room alias"), }, { key: "taken", + skip: ({value}) => !value, test: async ({value}) => { if (!value) { return true; diff --git a/src/components/views/elements/Validation.js b/src/components/views/elements/Validation.js index 31363b87c8..3454ab3aec 100644 --- a/src/components/views/elements/Validation.js +++ b/src/components/views/elements/Validation.js @@ -51,9 +51,16 @@ export default function withValidation({ description, rules }) { if (!rule.key || !rule.test) { continue; } + + const data = { value, allowEmpty }; + + if (rule.skip && rule.skip.call(this, data)) { + continue; + } + // We're setting `this` to whichever component holds the validation // function. That allows rules to access the state of the component. - const ruleValid = await rule.test.call(this, { value, allowEmpty }); + const ruleValid = await rule.test.call(this, data); valid = valid && ruleValid; if (ruleValid && rule.valid) { // If the rule's result is valid and has text to show for From 3e85f65d9e7a5b3a64091e061ff1137a9b58fcab Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 29 Feb 2020 00:36:01 +0000 Subject: [PATCH 2/3] Also skip alias availability check if alias looks invalid Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/elements/RoomAliasField.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/components/views/elements/RoomAliasField.js b/src/components/views/elements/RoomAliasField.js index 0139a9596b..06e608b14c 100644 --- a/src/components/views/elements/RoomAliasField.js +++ b/src/components/views/elements/RoomAliasField.js @@ -38,6 +38,13 @@ export default class RoomAliasField extends React.PureComponent { return `#${localpart}:${this.props.domain}`; } + _isValid(value) { + const fullAlias = this._asFullAlias(value); + // XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668 + return !value.includes("#") && !value.includes(":") && !value.includes(",") && + encodeURI(fullAlias) === fullAlias; + } + render() { const Field = sdk.getComponent('views.elements.Field'); const poundSign = (#); @@ -80,10 +87,7 @@ export default class RoomAliasField extends React.PureComponent { if (!value) { return true; } - const fullAlias = this._asFullAlias(value); - // XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668 - return !value.includes("#") && !value.includes(":") && !value.includes(",") && - encodeURI(fullAlias) === fullAlias; + return this._isValid(value); }, invalid: () => _t("Some characters not allowed"), }, { @@ -92,7 +96,7 @@ export default class RoomAliasField extends React.PureComponent { invalid: () => _t("Please provide a room alias"), }, { key: "taken", - skip: ({value}) => !value, + skip: ({value}) => !value || !this._isValid(value), test: async ({value}) => { if (!value) { return true; From 8aaab0a3baa7efd812df05a401c1c7138fdd97ff Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 29 Feb 2020 01:00:57 +0000 Subject: [PATCH 3/3] refactor to add Validation::final Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/elements/RoomAliasField.js | 14 +++++--------- src/components/views/elements/Validation.js | 6 ++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/components/views/elements/RoomAliasField.js b/src/components/views/elements/RoomAliasField.js index 06e608b14c..b38047cd3b 100644 --- a/src/components/views/elements/RoomAliasField.js +++ b/src/components/views/elements/RoomAliasField.js @@ -38,13 +38,6 @@ export default class RoomAliasField extends React.PureComponent { return `#${localpart}:${this.props.domain}`; } - _isValid(value) { - const fullAlias = this._asFullAlias(value); - // XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668 - return !value.includes("#") && !value.includes(":") && !value.includes(",") && - encodeURI(fullAlias) === fullAlias; - } - render() { const Field = sdk.getComponent('views.elements.Field'); const poundSign = (#); @@ -87,7 +80,10 @@ export default class RoomAliasField extends React.PureComponent { if (!value) { return true; } - return this._isValid(value); + const fullAlias = this._asFullAlias(value); + // XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668 + return !value.includes("#") && !value.includes(":") && !value.includes(",") && + encodeURI(fullAlias) === fullAlias; }, invalid: () => _t("Some characters not allowed"), }, { @@ -96,7 +92,7 @@ export default class RoomAliasField extends React.PureComponent { invalid: () => _t("Please provide a room alias"), }, { key: "taken", - skip: ({value}) => !value || !this._isValid(value), + final: true, test: async ({value}) => { if (!value) { return true; diff --git a/src/components/views/elements/Validation.js b/src/components/views/elements/Validation.js index 3454ab3aec..2be526a3c3 100644 --- a/src/components/views/elements/Validation.js +++ b/src/components/views/elements/Validation.js @@ -28,9 +28,11 @@ import classNames from 'classnames'; * An array of rules describing how to check to input value. Each rule in an object * and may have the following properties: * - `key`: A unique ID for the rule. Required. + * - `skip`: A function used to determine whether the rule should even be evaluated. * - `test`: A function used to determine the rule's current validity. Required. * - `valid`: Function returning text to show when the rule is valid. Only shown if set. * - `invalid`: Function returning text to show when the rule is invalid. Only shown if set. + * - `final`: A Boolean if true states that this rule will only be considered if all rules before it returned valid. * @returns {Function} * A validation function that takes in the current input value and returns * the overall validity and a feedback UI that can be rendered for more detail. @@ -52,6 +54,10 @@ export default function withValidation({ description, rules }) { continue; } + if (!valid && rule.final) { + continue; + } + const data = { value, allowEmpty }; if (rule.skip && rule.skip.call(this, data)) {