mattermost-mobile/app/mm-redux/utils/integration_utils.ts
Miguel Alatzar 2d81b497cf [MM-23520] Port mattermost-redux (#4088)
* Remove mattermost-redux

* Move mm-redux files into app/redux

* Add @redux path to tsconfig.json

* Fix imports

* Install missing dependencies

* Fix tsc errors

* Fix i18n_utils test

* Fix more imports

* Remove redux websocket

* Fix tests

* Rename @redux

* Apply changes from mattermost-redux PR 1103

* Remove mattermost-redux mention in template

* Add missing imports

* Rename app/redux/ to app/mm-redux/

* Remove test file

* Fix fetching Sidebar GM profiles

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2020-04-17 21:12:09 -07:00

87 lines
2.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {DialogElement} from '@mm-redux/types/integrations';
type DialogError = {
id: string;
defaultMessage: string;
values?: any;
};
export function checkDialogElementForError(elem: DialogElement, value: any): DialogError | undefined | null {
if (!value && !elem.optional) {
return {
id: 'interactive_dialog.error.required',
defaultMessage: 'This field is required.',
};
}
const type = elem.type;
if (type === 'text' || type === 'textarea') {
if (value && value.length < elem.min_length) {
return {
id: 'interactive_dialog.error.too_short',
defaultMessage: 'Minimum input length is {minLength}.',
values: {minLength: elem.min_length},
};
}
if (elem.subtype === 'email') {
if (value && !value.includes('@')) {
return {
id: 'interactive_dialog.error.bad_email',
defaultMessage: 'Must be a valid email address.',
};
}
}
if (elem.subtype === 'number') {
if (value && isNaN(value)) {
return {
id: 'interactive_dialog.error.bad_number',
defaultMessage: 'Must be a number.',
};
}
}
if (elem.subtype === 'url') {
if (value && !value.includes('http://') && !value.includes('https://')) {
return {
id: 'interactive_dialog.error.bad_url',
defaultMessage: 'URL must include http:// or https://.',
};
}
}
} else if (type === 'radio') {
const options = elem.options;
if (typeof value !== 'undefined' && Array.isArray(options) && !options.some((e) => e.value === value)) {
return {
id: 'interactive_dialog.error.invalid_option',
defaultMessage: 'Must be a valid option',
};
}
}
return null;
}
// If we're returned errors that don't match any of the elements we have,
// ignore them and complete the dialog
export function checkIfErrorsMatchElements(errors: {
[x: string]: DialogError;
} = {}, elements: Array<DialogElement> = []) {
for (const name in errors) {
if (!errors.hasOwnProperty(name)) {
continue;
}
for (const elem of elements) {
if (elem.name === name) {
return true;
}
}
}
return false;
}