From b0d1be7276b50ce8a9847e73cce1139b0f23c30a Mon Sep 17 00:00:00 2001 From: Jakob Ankarhem Date: Tue, 31 May 2022 19:13:03 +0200 Subject: [PATCH] feat(oidc): add form validation for oidc settings --- .../Settings/SettingsUsers/index.tsx | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/components/Settings/SettingsUsers/index.tsx b/src/components/Settings/SettingsUsers/index.tsx index d3a805878..8ea1ca4d7 100644 --- a/src/components/Settings/SettingsUsers/index.tsx +++ b/src/components/Settings/SettingsUsers/index.tsx @@ -12,6 +12,7 @@ import { Field, Form, Formik } from 'formik'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR, { mutate } from 'swr'; +import * as yup from 'yup'; const messages = defineMessages({ users: 'Users', @@ -37,7 +38,36 @@ const messages = defineMessages({ oidcDomain: 'OIDC Domain', }); -const SettingsUsers = () => { +const validationSchema = yup.object().shape({ + oidcLogin: yup.boolean(), + oidcClientId: yup.string().when('oidcLogin', { + is: true, + then: yup.string().required(), + }), + oidcClientSecret: yup.string().when('oidcLogin', { + is: true, + then: yup.string().required(), + }), + oidcDomain: yup.string().when('oidcLogin', { + is: true, + then: yup + .string() + .required() + .test({ + message: 'Must be a valid domain', + test: (val) => { + return ( + !!val && + /^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?$/.test( + val + ) + ); + }, + }), + }), +}); + +const SettingsUsers: React.FC = () => { const { addToast } = useToasts(); const intl = useIntl(); const { @@ -80,6 +110,7 @@ const SettingsUsers = () => { tvQuotaDays: data?.defaultQuotas.tv.quotaDays ?? 7, defaultPermissions: data?.defaultPermissions ?? 0, }} + validationSchema={validationSchema} enableReinitialize onSubmit={async (values) => { try { @@ -119,7 +150,7 @@ const SettingsUsers = () => { } }} > - {({ isSubmitting, values, setFieldValue }) => { + {({ isSubmitting, values, setFieldValue, errors, touched }) => { return (
@@ -203,6 +234,9 @@ const SettingsUsers = () => { type="text" />
+ {errors.oidcDomain && touched.oidcDomain && ( +
{errors.oidcDomain}
+ )}
@@ -217,6 +251,9 @@ const SettingsUsers = () => { type="text" />
+ {errors.oidcClientId && touched.oidcDomain && ( +
{errors.oidcClientId}
+ )}
@@ -236,6 +273,12 @@ const SettingsUsers = () => { autoComplete="off" />
+ {errors.oidcClientSecret && + touched.oidcClientSecret && ( +
+ {errors.oidcClientSecret} +
+ )}