tethys.backend/start/rules/dependent_array_min_length.ts
Arno Kaimbacher f67b736a88
Some checks failed
CI Pipeline / japa-tests (push) Failing after 1m0s
feat: Enhance dataset management and improve frontend components
- Added preloads 'allowed_extensions_mimetypes' and 'dependent_array_min_length' in adonisrc.ts
- Updated @symfony/webpack-encore from ^4.6.1 to ^5.0.1
- AdminuserController: Implemented pagination for 10 records in index method
- Enabled reviewers to reject datasets to editors with email notifications (DatasetController.ts)
- Submitter DatasetController: Files now loaded in ascending order (sort_order) in edit mode
- file.ts: Removed serialization of fileData due to browser issues
- Modified FileUpload.vue to mark already uploaded files as deleted
- Improved keyword search in SearchCategoryAutocomplete.vue
- Started development on Category.vue for submitters to categorize DDC
- Added new route /dataset/categorize in routes.ts
- Introduced 2 new rules in start/rules: allowed_extensions_mimetypes.ts and dependent_array_min_length.ts
- Performed npm updates
2024-11-29 15:46:26 +01:00

62 lines
2.2 KiB
TypeScript

/*
|--------------------------------------------------------------------------
| Preloaded File - node ace make:preload rules/dependentArrayMinLength
|--------------------------------------------------------------------------
|*/
import { FieldContext } from '@vinejs/vine/types';
import vine, { VineArray } from '@vinejs/vine';
import { SchemaTypes } from '@vinejs/vine/types';
/**
* Options accepted by the dependentArrayMinLength rule
*/
type Options = {
min: number;
dependentArray: string;
};
async function dependentArrayMinLength(value: unknown, options: Options, field: FieldContext) {
const fileInputs = field.data[options.dependentArray]; // Access the dependent array
const isArrayValue = Array.isArray(value);
const isArrayFileInputs = Array.isArray(fileInputs);
if (isArrayValue && isArrayFileInputs) {
if (value.length >= options.min) {
return true; // Valid if the main array length meets the minimum
} else if (value.length === 0 && fileInputs.length >= options.min) {
return true; // Valid if the main array is empty and the dependent array meets the minimum
} else {
field.report(
`At least {{ min }} item for {{field}} field must be defined`,
'array.dependentArrayMinLength',
field,
options,
);
}
} else {
// Report if either value or dependentArray is not an array
field.report(
`Both the {{field}} field and dependent array {{dependentArray}} must be arrays.`,
'array.dependentArrayMinLength',
field,
options,
);
}
return false; // Invalid if none of the conditions are met
}
export const dependentArrayMinLengthRule = vine.createRule(dependentArrayMinLength);
// Extend the VineArray interface with the same type parameters
declare module '@vinejs/vine' {
interface VineArray<Schema extends SchemaTypes> {
dependentArrayMinLength(options: Options): this;
}
}
VineArray.macro('dependentArrayMinLength', function <Schema extends SchemaTypes>(this: VineArray<Schema>, options: Options) {
return this.use(dependentArrayMinLengthRule(options));
});