mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: enable optional fields (#1470)
This commit is contained in:
51
packages/lib/utils/advanced-fields-helpers.ts
Normal file
51
packages/lib/utils/advanced-fields-helpers.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { type Field, FieldType } from '@documenso/prisma/client';
|
||||
|
||||
import { ZFieldMetaSchema } from '../types/field-meta';
|
||||
|
||||
// Currently it seems that the majority of fields have advanced fields for font reasons.
|
||||
// This array should only contain fields that have an optional setting in the fieldMeta.
|
||||
const ADVANCED_FIELD_TYPES_WITH_OPTIONAL_SETTING: FieldType[] = [
|
||||
FieldType.NUMBER,
|
||||
FieldType.TEXT,
|
||||
FieldType.DROPDOWN,
|
||||
FieldType.RADIO,
|
||||
FieldType.CHECKBOX,
|
||||
];
|
||||
|
||||
/**
|
||||
* Whether a field is required to be inserted.
|
||||
*/
|
||||
export const isRequiredField = (field: Field) => {
|
||||
// All fields without the optional metadata are assumed to be required.
|
||||
if (!ADVANCED_FIELD_TYPES_WITH_OPTIONAL_SETTING.includes(field.type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Not sure why fieldMeta can be optional for advanced fields, but it is.
|
||||
// Therefore we must assume if there is no fieldMeta, then the field is optional.
|
||||
if (!field.fieldMeta) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const parsedData = ZFieldMetaSchema.safeParse(field.fieldMeta);
|
||||
|
||||
// If it fails, assume the field is optional.
|
||||
// This needs to be logged somewhere.
|
||||
if (!parsedData.success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parsedData.data?.required === true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the provided field is required and not inserted.
|
||||
*/
|
||||
export const isFieldUnsignedAndRequired = (field: Field) =>
|
||||
isRequiredField(field) && !field.inserted;
|
||||
|
||||
/**
|
||||
* Whether the provided fields contains a field that is required to be inserted.
|
||||
*/
|
||||
export const fieldsContainUnsignedRequiredField = (fields: Field[]) =>
|
||||
fields.some(isFieldUnsignedAndRequired);
|
||||
Reference in New Issue
Block a user