mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-05-24 18:50:06 +02:00
improve validation schema (#3984)
This commit is contained in:
parent
0826b035a2
commit
e3bc7cc747
@ -111,9 +111,15 @@ export default function CreateCredential({
|
|||||||
|
|
||||||
const { name, is_public, groups, ...credentialValues } = values;
|
const { name, is_public, groups, ...credentialValues } = values;
|
||||||
|
|
||||||
|
const filteredCredentialValues = Object.fromEntries(
|
||||||
|
Object.entries(credentialValues).filter(
|
||||||
|
([_, value]) => value !== null && value !== ""
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await submitCredential({
|
const response = await submitCredential({
|
||||||
credential_json: credentialValues,
|
credential_json: filteredCredentialValues,
|
||||||
admin_public: true,
|
admin_public: true,
|
||||||
curator_public: is_public,
|
curator_public: is_public,
|
||||||
groups: groups,
|
groups: groups,
|
||||||
@ -194,7 +200,7 @@ export default function CreateCredential({
|
|||||||
for information on setting up this connector.
|
for information on setting up this connector.
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
<CardSection className="w-full items-start dark:bg-neutral-900 mt-4 flex flex-col gap-y-6">
|
<CardSection className="w-full items-start dark:bg-neutral-900 mt-4 flex flex-col gap-y-6">
|
||||||
<TextFormField
|
<TextFormField
|
||||||
name="name"
|
name="name"
|
||||||
placeholder="(Optional) credential name.."
|
placeholder="(Optional) credential name.."
|
||||||
|
@ -6,18 +6,32 @@ import {
|
|||||||
getDisplayNameForCredentialKey,
|
getDisplayNameForCredentialKey,
|
||||||
} from "@/lib/connectors/credentials";
|
} from "@/lib/connectors/credentials";
|
||||||
|
|
||||||
export function createValidationSchema(json_values: dictionaryType) {
|
export function createValidationSchema(json_values: Record<string, any>) {
|
||||||
const schemaFields: { [key: string]: Yup.StringSchema } = {};
|
const schemaFields: Record<string, Yup.AnySchema> = {};
|
||||||
|
|
||||||
for (const key in json_values) {
|
for (const key in json_values) {
|
||||||
if (Object.prototype.hasOwnProperty.call(json_values, key)) {
|
if (!Object.prototype.hasOwnProperty.call(json_values, key)) {
|
||||||
if (json_values[key] === null) {
|
continue;
|
||||||
schemaFields[key] = Yup.string().optional();
|
}
|
||||||
} else {
|
|
||||||
schemaFields[key] = Yup.string().required(
|
const displayName = getDisplayNameForCredentialKey(key);
|
||||||
`Please enter your ${getDisplayNameForCredentialKey(key)}`
|
|
||||||
);
|
if (json_values[key] === null) {
|
||||||
}
|
// Field is optional:
|
||||||
|
schemaFields[key] = Yup.string()
|
||||||
|
.trim()
|
||||||
|
// Transform empty strings to null
|
||||||
|
.transform((value) => (value === "" ? null : value))
|
||||||
|
.nullable()
|
||||||
|
.notRequired();
|
||||||
|
} else {
|
||||||
|
// Field is required:
|
||||||
|
schemaFields[key] = Yup.string()
|
||||||
|
.trim()
|
||||||
|
// This ensures user cannot enter an empty string:
|
||||||
|
.min(1, `${displayName} cannot be empty`)
|
||||||
|
// The required message is shown if the field is missing
|
||||||
|
.required(`Please enter your ${displayName}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user