-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Cardano-Forge/feat/validator-asset-name
Duplicate Asset Name
- Loading branch information
Showing
11 changed files
with
196 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { assertEquals } from "@std/assert"; | ||
|
||
import { Validator } from "../src/core.ts"; | ||
|
||
import { DuplicateName } from "../src/rules/duplicate-name.ts"; | ||
import { DuplicateAssetName } from "../src/rules/duplicate-asset-name.ts"; | ||
|
||
Deno.test("DuplicateAssetName - withError", () => { | ||
const metadata = [ | ||
{ | ||
assetName: "asset_0000", | ||
metadata: { | ||
name: "asset_0000", | ||
image: "adibou.png", | ||
}, | ||
}, | ||
{ | ||
assetName: "asset_0001", | ||
metadata: { | ||
name: "asset_0001", | ||
image: "roller-coaster-tycoon.png", | ||
}, | ||
}, | ||
{ | ||
assetName: "asset_0000", | ||
metadata: { | ||
name: "asset_0002", | ||
image: "adibou.png", | ||
}, | ||
}, | ||
{ | ||
assetName: "asset_0003", | ||
metadata: { | ||
name: "asset_0003", | ||
image: "roller-coaster-tycoon.png", | ||
}, | ||
}, | ||
{ | ||
assetName: "asset_0000", | ||
metadata: { | ||
name: "asset_0004", | ||
image: "roller-coaster-tycoon.png", | ||
}, | ||
}, | ||
]; | ||
|
||
const mainValidator = new Validator("Main"); | ||
mainValidator.Enable(new DuplicateAssetName()); | ||
|
||
mainValidator.ExecuteOnce(metadata); | ||
|
||
const result = mainValidator.GetResults(); | ||
|
||
assertEquals(result, { | ||
asset_0000: { | ||
status: "error", | ||
warnings: [], | ||
errors: [ | ||
{ | ||
validatorId: "duplicate-asset-name", | ||
message: | ||
"AssetName: asset_0000 has been detected as a duplicate. (metadata.name = asset_0000)", | ||
}, | ||
{ | ||
validatorId: "duplicate-asset-name", | ||
message: | ||
"AssetName: asset_0000 has been detected as a duplicate. (metadata.name = asset_0002)", | ||
}, | ||
{ | ||
validatorId: "duplicate-asset-name", | ||
message: | ||
"AssetName: asset_0000 has been detected as a duplicate. (metadata.name = asset_0004)", | ||
}, | ||
], | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { BaseValidator } from "../core.ts"; | ||
|
||
import type { Metadata, StateOutput } from "../utils/types.ts"; | ||
import { logger } from "../utils/logger.ts"; | ||
|
||
/** | ||
* A validator that checks if there are any duplicate asset names in the provided metadatas. | ||
* | ||
* This validator counts the occurrences of each asset name and identifies duplicates based on the count. It assumes that the asset name is the top-level key in each metadata object. | ||
* | ||
* @class DuplicateAssetName | ||
* @module Rules | ||
* @extends BaseValidator | ||
*/ | ||
export class DuplicateAssetName extends BaseValidator { | ||
/** | ||
* Constructs a new instance of the `DuplicateAssetName` validator with an optional configuration object. | ||
* | ||
* @param {object} [options] - The options for the validator (not used in this validator). | ||
*/ | ||
constructor(options?: object) { | ||
const id = "duplicate-asset-name"; | ||
super(id, options, "once"); | ||
} | ||
|
||
/** | ||
* Executes the validation logic for a given asset and metadatas. | ||
* | ||
* @param {object[]} metadatas - An array of all metadatas being validated. | ||
* @param {Record<string, StateOutput>} validations - An object of all validations made. | ||
* @returns {Record<string, StateOutput>} An array containing the validation results. | ||
*/ | ||
ExecuteOnce( | ||
metadatas: object[], | ||
validations: Record<string, StateOutput> | ||
): Record<string, StateOutput> { | ||
logger(`Executing ${this.id} with: `, metadatas.length); | ||
return this.Logic(metadatas as Metadata[], validations); | ||
} | ||
|
||
/** | ||
* Logic method to check for duplicate asset names. | ||
* | ||
* @param {Metadata[]} metadatas - An array of all metadatas being validated. | ||
* @param {Record<string, StateOutput>} validations - An object of all validations made. | ||
* @returns {Record<string, StateOutput>} - Returns an array containing validation results. | ||
*/ | ||
Logic( | ||
metadatas: Metadata[], | ||
validations: Record<string, StateOutput> | ||
): Record<string, StateOutput> { | ||
const errorsMetadata = new Set<Metadata>(); | ||
|
||
for (const entry of metadatas) { | ||
const founds = metadatas.filter( | ||
(meta) => meta.assetName === entry.assetName | ||
); | ||
|
||
if (founds.length > 1) { | ||
founds.forEach((meta) => { | ||
errorsMetadata.add(meta); | ||
}); | ||
} | ||
} | ||
|
||
// ERRORS | ||
errorsMetadata.forEach(({ assetName, metadata }) => { | ||
if (!validations[assetName]) { | ||
validations[assetName] = { | ||
status: "error", | ||
warnings: [], | ||
errors: [], | ||
}; | ||
} | ||
|
||
validations[assetName].status = "error"; | ||
validations[assetName].errors.push({ | ||
validatorId: this.id, | ||
message: `AssetName: ${assetName} has been detected as a duplicate. (metadata.name = ${metadata.name})`, | ||
}); | ||
}); | ||
|
||
return validations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters