mirror of
https://github.com/logto-io/logto.git
synced 2025-02-10 21:58:23 -05:00
refactor(console): refactor uriValidator
usage (#1051)
This commit is contained in:
parent
2ba11215ed
commit
bbb80c819a
6 changed files with 27 additions and 59 deletions
|
@ -42,7 +42,7 @@ const MultiTextInputField = ({ name, title, onError }: Props) => {
|
|||
validate: createValidatorForRhf({
|
||||
required: t('errors.required_field_missing_plural', { field: title }),
|
||||
pattern: {
|
||||
verify: uriValidator({ verifyBlank: false }),
|
||||
verify: (value) => !value || uriValidator(value),
|
||||
message: t('errors.invalid_uri_format'),
|
||||
},
|
||||
}),
|
||||
|
|
|
@ -27,7 +27,7 @@ const Settings = ({ oidcConfig }: Props) => {
|
|||
|
||||
const uriPatternRules: MultiTextInputRule = {
|
||||
pattern: {
|
||||
verify: uriValidator({ verifyBlank: false }),
|
||||
verify: (value) => !value || uriValidator(value),
|
||||
message: t('errors.invalid_uri_format'),
|
||||
},
|
||||
};
|
||||
|
@ -111,7 +111,7 @@ const Settings = ({ oidcConfig }: Props) => {
|
|||
rules={{
|
||||
validate: createValidatorForRhf({
|
||||
pattern: {
|
||||
verify: uriOriginValidator({ verifyBlank: false }),
|
||||
verify: (value) => !value || uriOriginValidator(value),
|
||||
message: t('errors.invalid_origin_format'),
|
||||
},
|
||||
}),
|
||||
|
|
|
@ -75,13 +75,7 @@ const BrandingForm = () => {
|
|||
<TextInput
|
||||
{...register('branding.logoUrl', {
|
||||
required: true,
|
||||
validate: (value) => {
|
||||
if (uriValidator({ verifyBlank: false })(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return t('errors.invalid_uri_format');
|
||||
},
|
||||
validate: (value) => !value || uriValidator(value) || t('errors.invalid_uri_format'),
|
||||
})}
|
||||
hasError={Boolean(errors.branding?.logoUrl)}
|
||||
errorMessage={errors.branding?.logoUrl?.message}
|
||||
|
@ -91,17 +85,7 @@ const BrandingForm = () => {
|
|||
<FormField title="admin_console.sign_in_exp.branding.dark_logo_image_url">
|
||||
<TextInput
|
||||
{...register('branding.darkLogoUrl', {
|
||||
validate: (value) => {
|
||||
if (!value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (uriValidator({ verifyBlank: false })(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return t('errors.invalid_uri_format');
|
||||
},
|
||||
validate: (value) => !value || uriValidator(value) || t('errors.invalid_uri_format'),
|
||||
})}
|
||||
hasError={Boolean(errors.branding?.darkLogoUrl)}
|
||||
errorMessage={errors.branding?.darkLogoUrl?.message}
|
||||
|
|
|
@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next';
|
|||
import FormField from '@/components/FormField';
|
||||
import Switch from '@/components/Switch';
|
||||
import TextInput from '@/components/TextInput';
|
||||
import { uriValidator } from '@/utilities/validator';
|
||||
|
||||
import { SignInExperienceForm } from '../types';
|
||||
import * as styles from './index.module.scss';
|
||||
|
@ -33,8 +34,12 @@ const TermsForm = () => {
|
|||
tooltip="admin_console.sign_in_exp.terms_of_use.terms_of_use_tip"
|
||||
>
|
||||
<TextInput
|
||||
{...register('termsOfUse.contentUrl', { required: enabled })}
|
||||
{...register('termsOfUse.contentUrl', {
|
||||
required: enabled,
|
||||
validate: (value) => !value || uriValidator(value) || t('errors.invalid_uri_format'),
|
||||
})}
|
||||
hasError={Boolean(errors.termsOfUse)}
|
||||
errorMessage={errors.termsOfUse?.contentUrl?.message}
|
||||
/>
|
||||
</FormField>
|
||||
</>
|
||||
|
|
|
@ -224,17 +224,8 @@ const UserDetails = () => {
|
|||
>
|
||||
<TextInput
|
||||
{...register('avatar', {
|
||||
validate: (value) => {
|
||||
if (!value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (uriValidator({ verifyBlank: true })(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return t('errors.invalid_uri_format');
|
||||
},
|
||||
validate: (value) =>
|
||||
!value || uriValidator(value) || t('errors.invalid_uri_format'),
|
||||
})}
|
||||
hasError={Boolean(errors.avatar)}
|
||||
errorMessage={errors.avatar?.message}
|
||||
|
|
|
@ -1,30 +1,18 @@
|
|||
export const uriValidator = ({ verifyBlank = true }) => {
|
||||
return (value: string) => {
|
||||
if (!verifyBlank && value.trim().length === 0) {
|
||||
return true;
|
||||
}
|
||||
export const uriValidator = (value: string) => {
|
||||
try {
|
||||
// eslint-disable-next-line no-new
|
||||
new URL(value);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line no-new
|
||||
new URL(value);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
export const uriOriginValidator = ({ verifyBlank = true }) => {
|
||||
return (value: string) => {
|
||||
if (!verifyBlank && value.trim().length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return new URL(value).origin === value;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
export const uriOriginValidator = (value: string) => {
|
||||
try {
|
||||
return new URL(value).origin === value;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue