Migrate away from legacy component and piece models
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { normalizeComponentModelStructure } from '../../component-models/structure.normalizer';
|
||||
import type { ComponentModelStructure } from '../types/inventory';
|
||||
import type {
|
||||
ComponentModelStructure,
|
||||
PieceModelCustomField,
|
||||
PieceModelStructure,
|
||||
} from '../types/inventory';
|
||||
|
||||
export class ComponentModelStructureValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
@@ -150,3 +154,109 @@ export const ComponentModelStructureSchema = {
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export class PieceModelStructureValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'PieceModelStructureValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
function toStringOrNull(value: unknown): string | null {
|
||||
if (value === undefined || value === null) {
|
||||
return null;
|
||||
}
|
||||
const trimmed = String(value).trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
function normalizePieceModelCustomFields(
|
||||
customFields: unknown,
|
||||
): PieceModelCustomField[] {
|
||||
if (!Array.isArray(customFields)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const normalized: PieceModelCustomField[] = [];
|
||||
|
||||
customFields.forEach((entry, index) => {
|
||||
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const record = entry as Record<string, unknown>;
|
||||
const rawName =
|
||||
(typeof record.name === 'string' ? record.name : undefined) ??
|
||||
(typeof record.key === 'string' ? record.key : undefined) ??
|
||||
undefined;
|
||||
|
||||
const name = rawName ? rawName.trim() : '';
|
||||
|
||||
if (!name) {
|
||||
throw new PieceModelStructureValidationError(
|
||||
`customFields[${index}].name doit être une chaîne non vide`,
|
||||
);
|
||||
}
|
||||
|
||||
const field: PieceModelCustomField = { name };
|
||||
|
||||
if ('value' in record) {
|
||||
field.value = record.value;
|
||||
}
|
||||
|
||||
if (typeof record.type === 'string') {
|
||||
field.type = record.type;
|
||||
}
|
||||
|
||||
if ('required' in record) {
|
||||
field.required = Boolean(record.required);
|
||||
}
|
||||
|
||||
if (Array.isArray(record.options)) {
|
||||
field.options = record.options;
|
||||
} else if (typeof record.optionsText === 'string') {
|
||||
const options = record.optionsText
|
||||
.split(/\r?\n/)
|
||||
.map((option) => option.trim())
|
||||
.filter((option) => option.length > 0);
|
||||
if (options.length > 0) {
|
||||
field.options = options;
|
||||
}
|
||||
}
|
||||
|
||||
normalized.push(field);
|
||||
});
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export const PieceModelStructureSchema = {
|
||||
parse(input: unknown): PieceModelStructure {
|
||||
if (input === undefined || input === null) {
|
||||
return { customFields: [] };
|
||||
}
|
||||
|
||||
if (typeof input !== 'object' || Array.isArray(input)) {
|
||||
throw new PieceModelStructureValidationError(
|
||||
'La structure de pièce doit être un objet JSON.',
|
||||
);
|
||||
}
|
||||
|
||||
const record = input as Record<string, unknown>;
|
||||
|
||||
const structure: PieceModelStructure = { ...record };
|
||||
const customFields = normalizePieceModelCustomFields(record.customFields);
|
||||
if (customFields.length > 0 || 'customFields' in record) {
|
||||
structure.customFields = customFields;
|
||||
}
|
||||
|
||||
const normalizedTypePiece = toStringOrNull(record.typePieceId);
|
||||
if (normalizedTypePiece) {
|
||||
structure.typePieceId = normalizedTypePiece;
|
||||
} else if ('typePieceId' in record) {
|
||||
delete (structure as Record<string, unknown>).typePieceId;
|
||||
}
|
||||
|
||||
return structure;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user