2021-08-23 19:01:22 -04:00
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-08-23 19:01:22 -04:00
|
|
|
*/
|
|
|
|
|
|
2021-08-24 19:47:05 -04:00
|
|
|
/*
|
|
|
|
|
* Frontend validators, less strict than backend validators
|
|
|
|
|
*
|
|
|
|
|
* TODO add nice validation errors for Profile page settings modal
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-05 17:48:15 -05:00
|
|
|
import { VALIDATE_EMAIL_REGEX } from '../constants/AccountPropertyConstants.ts'
|
2021-08-23 19:01:22 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate the email input
|
|
|
|
|
*
|
2021-12-02 12:32:57 -05:00
|
|
|
* Compliant with PHP core FILTER_VALIDATE_EMAIL validator*
|
2021-08-23 19:01:22 -04:00
|
|
|
*
|
2021-12-02 12:32:57 -05:00
|
|
|
* Reference implementation https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js/blob/71e62ca48841d2246a1b531e7e84f5a01f15e615/src/index.ts*
|
2021-08-23 19:01:22 -04:00
|
|
|
*
|
|
|
|
|
* @param {string} input the input
|
2021-12-02 12:32:57 -05:00
|
|
|
* @return {boolean}
|
2021-08-23 19:01:22 -04:00
|
|
|
*/
|
|
|
|
|
export function validateEmail(input) {
|
|
|
|
|
return typeof input === 'string'
|
|
|
|
|
&& VALIDATE_EMAIL_REGEX.test(input)
|
|
|
|
|
&& input.slice(-1) !== '\n'
|
|
|
|
|
&& input.length <= 320
|
|
|
|
|
&& encodeURIComponent(input).replace(/%../g, 'x').length <= 320
|
|
|
|
|
}
|
2021-08-24 19:47:05 -04:00
|
|
|
|
2022-07-21 15:42:53 -04:00
|
|
|
/**
|
|
|
|
|
* Validate the URL input
|
|
|
|
|
*
|
|
|
|
|
* @param {string} input the input
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
export function validateUrl(input) {
|
|
|
|
|
try {
|
|
|
|
|
new URL(input)
|
|
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 19:47:05 -04:00
|
|
|
/**
|
|
|
|
|
* Validate the language input
|
|
|
|
|
*
|
2021-08-31 17:58:31 -04:00
|
|
|
* @param {object} input the input
|
2021-12-02 12:32:57 -05:00
|
|
|
* @return {boolean}
|
2021-08-24 19:47:05 -04:00
|
|
|
*/
|
|
|
|
|
export function validateLanguage(input) {
|
|
|
|
|
return input.code !== ''
|
2021-08-31 17:58:31 -04:00
|
|
|
&& input.name !== ''
|
|
|
|
|
&& input.name !== undefined
|
2021-08-24 19:47:05 -04:00
|
|
|
}
|
2021-10-14 04:28:54 -04:00
|
|
|
|
2022-08-11 01:54:08 -04:00
|
|
|
/**
|
|
|
|
|
* Validate the locale input
|
|
|
|
|
*
|
|
|
|
|
* @param {object} input the input
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
export function validateLocale(input) {
|
|
|
|
|
return input.code !== ''
|
|
|
|
|
&& input.name !== ''
|
|
|
|
|
&& input.name !== undefined
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 04:28:54 -04:00
|
|
|
/**
|
|
|
|
|
* Validate boolean input
|
|
|
|
|
*
|
|
|
|
|
* @param {boolean} input the input
|
2021-12-02 12:32:57 -05:00
|
|
|
* @return {boolean}
|
2021-10-14 04:28:54 -04:00
|
|
|
*/
|
|
|
|
|
export function validateBoolean(input) {
|
|
|
|
|
return typeof input === 'boolean'
|
|
|
|
|
}
|