1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-23 23:02:58 +00:00

Swapped validator arguments to meet the order on exporter function, fixing types

This commit is contained in:
2025-02-19 03:19:01 +04:00
parent 459b1fa779
commit c93c3c7bd5
2 changed files with 15 additions and 6 deletions

View File

@@ -12,7 +12,13 @@ export default class EntitiesTransporter<EntityType> {
*/
get #entityName() {
// How the hell should I even do this?
return ((this.#targetEntityConstructor as any) as typeof StorageEntity)._entityName;
const entityName = ((this.#targetEntityConstructor as any) as typeof StorageEntity)._entityName;
if (entityName === "entity") {
throw new Error("Generic entity name encountered!");
}
return entityName;
}
/**
@@ -34,8 +40,8 @@ export default class EntitiesTransporter<EntityType> {
}
validateImportedEntity(
this.#entityName,
importedObject,
this.#entityName
);
return new this.#targetEntityConstructor(
@@ -60,7 +66,7 @@ export default class EntitiesTransporter<EntityType> {
}
const exportableObject = exportEntityToObject(
this.#entityName as keyof App.EntityNamesMap,
this.#entityName,
entityObject
);

View File

@@ -40,15 +40,18 @@ const entitiesValidators: EntitiesValidationMap = {
/**
* Validate the structure of the entity.
* @param importedObject Object imported from JSON.
* @param entityName Name of the entity to validate. Should be loaded from the entity class.
* @param importedObject Object imported from JSON.
* @throws {Error} Error in case validation failed with the reason stored in the message.
*/
export function validateImportedEntity(importedObject: any, entityName: string) {
export function validateImportedEntity<EntityName extends keyof App.EntityNamesMap>(
entityName: EntityName,
importedObject: any
) {
if (!entitiesValidators.hasOwnProperty(entityName)) {
console.error(`Trying to validate entity without the validator present! Entity name: ${entityName}`);
return;
}
entitiesValidators[entityName as keyof EntitiesValidationMap]!.call(null, importedObject);
entitiesValidators[entityName]!.call(null, importedObject);
}