mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 05:11:50 +08:00

* Add new providers * Extract utils * Configurable workflow fields * translate fields * Add translations * Update cards * Add isGitProvider * i18n * Dynamic fields for ConfigForm * Use git fields * Remove type dropdown for edit * Display proper type groups * Display field errors * Improve error handling * Refactor data * Check for repositoryLister * Fix workflow * use state var * betterer * Prettier * Prettier[2] * i18n * Remove showDropdown * i18n * Update step validation * Add test * Update provider list * Cleanup * Add tokenUser field * Provider-specific source code link * Review comments --------- Co-authored-by: Roberto Jimenez Sanchez <roberto.jimenez@grafana.com>
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { ErrorDetails } from 'app/api/clients/provisioning/v0alpha1';
|
|
|
|
import { WizardFormData } from '../Wizard/types';
|
|
|
|
export type RepositoryField = keyof WizardFormData['repository'];
|
|
export type RepositoryFormPath = `repository.${RepositoryField}`;
|
|
export type FormErrorTuple = [RepositoryFormPath | null, { message: string } | null];
|
|
|
|
/**
|
|
* Maps API error details to form error fields for React Hook Form
|
|
*
|
|
* @param errors Array of error details from the API response
|
|
* @returns Tuple with form field path and error message
|
|
*/
|
|
export const getFormErrors = (errors: ErrorDetails[]): FormErrorTuple => {
|
|
const fieldsToValidate = [
|
|
'local.path',
|
|
'github.branch',
|
|
'github.url',
|
|
'github.token',
|
|
'gitlab.branch',
|
|
'gitlab.url',
|
|
'gitlab.token',
|
|
'bitbucket.branch',
|
|
'bitbucket.url',
|
|
'bitbucket.token',
|
|
'git.branch',
|
|
'git.url',
|
|
'git.token',
|
|
];
|
|
const fieldMap: Record<string, RepositoryFormPath> = {
|
|
path: 'repository.path',
|
|
branch: 'repository.branch',
|
|
url: 'repository.url',
|
|
token: 'repository.token',
|
|
};
|
|
|
|
for (const error of errors) {
|
|
if (error.field) {
|
|
const cleanField = error.field.replace('spec.', '');
|
|
if (fieldsToValidate.includes(cleanField)) {
|
|
const fieldParts = cleanField.split('.');
|
|
const lastPart = fieldParts[fieldParts.length - 1];
|
|
|
|
if (lastPart in fieldMap) {
|
|
return [fieldMap[lastPart], { message: error.detail || `Invalid ${lastPart}` }];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return [null, null];
|
|
};
|