mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
chore: add more field types (#1141)
Adds a number of new field types and capabilities to existing fields. A massive change with far too many moving pieces to document in a single commit.
This commit is contained in:
82
packages/lib/advanced-fields-validation/validate-checkbox.ts
Normal file
82
packages/lib/advanced-fields-validation/validate-checkbox.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import { checkboxValidationSigns } from '@documenso/ui/primitives/document-flow/field-items-advanced-settings/constants';
|
||||
|
||||
interface CheckboxFieldMeta {
|
||||
readOnly?: boolean;
|
||||
required?: boolean;
|
||||
validationRule?: string;
|
||||
validationLength?: number;
|
||||
}
|
||||
|
||||
export const validateCheckboxField = (
|
||||
values: string[],
|
||||
fieldMeta: CheckboxFieldMeta,
|
||||
isSigningPage: boolean = false,
|
||||
): string[] => {
|
||||
const errors = [];
|
||||
|
||||
const { readOnly, required, validationRule, validationLength } = fieldMeta;
|
||||
|
||||
if (readOnly && required) {
|
||||
errors.push('A field cannot be both read-only and required');
|
||||
}
|
||||
|
||||
if (values.length === 0) {
|
||||
errors.push('At least one option must be added');
|
||||
}
|
||||
|
||||
if (readOnly && values.length === 0) {
|
||||
errors.push('A read-only field must have at least one value');
|
||||
}
|
||||
|
||||
if (isSigningPage && required && values.length === 0) {
|
||||
errors.push('Selecting an option is required');
|
||||
}
|
||||
|
||||
if (validationRule && !validationLength) {
|
||||
errors.push('You need to specify the number of options for validation');
|
||||
}
|
||||
|
||||
if (validationLength && !validationRule) {
|
||||
errors.push('You need to specify the validation rule');
|
||||
}
|
||||
|
||||
if (validationRule && validationLength) {
|
||||
const validation = checkboxValidationSigns.find((sign) => sign.label === validationRule);
|
||||
|
||||
if (validation) {
|
||||
let lengthCondition = false;
|
||||
|
||||
switch (validation.value) {
|
||||
case '=':
|
||||
lengthCondition = isSigningPage
|
||||
? values.length !== validationLength
|
||||
: values.length < validationLength;
|
||||
break;
|
||||
case '>=':
|
||||
lengthCondition = values.length < validationLength;
|
||||
break;
|
||||
case '<=':
|
||||
lengthCondition = isSigningPage
|
||||
? values.length > validationLength
|
||||
: values.length < validationLength;
|
||||
break;
|
||||
}
|
||||
|
||||
if (lengthCondition) {
|
||||
let errorMessage;
|
||||
if (isSigningPage) {
|
||||
errorMessage = `You need to ${validationRule.toLowerCase()} ${validationLength} options`;
|
||||
} else {
|
||||
errorMessage =
|
||||
validation.value === '<='
|
||||
? `You need to select at least ${validationLength} options`
|
||||
: `You need to add at least ${validationLength} options`;
|
||||
}
|
||||
|
||||
errors.push(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
};
|
||||
Reference in New Issue
Block a user