From cc5ea60b6820572416d189bb5b5a9da00cc2d4e4 Mon Sep 17 00:00:00 2001 From: Yuri Date: Tue, 6 Sep 2022 10:52:53 +0100 Subject: [PATCH 1/2] Make Metadata interface more loose, allow any arbitrary key value pairs besides the known keys --- src/rmrk2.0.0/tools/types.ts | 2 + src/rmrk2.0.0/tools/validate-metadata.ts | 12 ++---- test/2.0.0/utils/validate-metadata.test.ts | 46 ++++++++-------------- 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/rmrk2.0.0/tools/types.ts b/src/rmrk2.0.0/tools/types.ts index dc041499..6dcd5e5d 100644 --- a/src/rmrk2.0.0/tools/types.ts +++ b/src/rmrk2.0.0/tools/types.ts @@ -55,6 +55,8 @@ export interface Metadata { image_data?: string; /** @deprecated deprecated in favour of `mediaUri` field */ animation_url?: string; + /* Allow any other arbitrary key value pairs */ + [key: string]: any; } export type Options = { diff --git a/src/rmrk2.0.0/tools/validate-metadata.ts b/src/rmrk2.0.0/tools/validate-metadata.ts index b6320cde..c81b4664 100644 --- a/src/rmrk2.0.0/tools/validate-metadata.ts +++ b/src/rmrk2.0.0/tools/validate-metadata.ts @@ -17,10 +17,11 @@ import { OP_TYPES } from "./constants"; const MetadataStruct = type({ name: optional(string()), description: optional(string()), + mediaUri: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))), + thumbnailUri: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))), image: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))), - image_data: optional(string()), properties: any(), - external_url: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))), + externalUri: optional(pattern(string(), new RegExp("^(https?|ipfs)://.*$"))), }); export const PropertiesStruct = object({ @@ -108,17 +109,12 @@ export const validateAttributes = (properties?: IProperties) => { }; /** - * Validate Metadata according to OpenSea docs - * https://docs.opensea.io/docs/metadata-standards + * Validate Metadata * @param metadata */ export const validateMetadata = (metadata: Metadata) => { assert(metadata, MetadataStruct); - if (!metadata.image) { - throw new Error("image is missing"); - } - validateAttributes(metadata.properties); return true; }; diff --git a/test/2.0.0/utils/validate-metadata.test.ts b/test/2.0.0/utils/validate-metadata.test.ts index 515979b8..51a556e9 100644 --- a/test/2.0.0/utils/validate-metadata.test.ts +++ b/test/2.0.0/utils/validate-metadata.test.ts @@ -1,5 +1,4 @@ import { validateMetadata } from "../../../src/rmrk2.0.0/tools/validate-metadata"; -import { NFTMetadata } from "../../../src/rmrk2.0.0/classes/nft"; import { attributesMockBoostNumberValid, metadataMockAllValid, @@ -7,6 +6,7 @@ import { metadataMockAllValid4, metadataMockAllValid6, } from "../mocks/metadata-valid"; +import {Metadata} from "../../../src/rmrk2.0.0"; export const attributesMockDateInvalid = [ { @@ -27,25 +27,25 @@ export const metadataMockAllInvalid = { describe("validation: validateMetadata with valid mocks", () => { it("should be valid", () => { expect(() => - validateMetadata(metadataMockAllValid as NFTMetadata) + validateMetadata(metadataMockAllValid as Metadata) ).not.toThrow(); }); it("should be valid2", () => { expect(() => - validateMetadata(metadataMockAllValid2 as NFTMetadata) + validateMetadata(metadataMockAllValid2 as Metadata) ).not.toThrow(); }); it("should be valid4", () => { expect(() => - validateMetadata(metadataMockAllValid4 as NFTMetadata) + validateMetadata(metadataMockAllValid4 as Metadata) ).not.toThrow(); }); it("should be valid6", () => { expect(() => - validateMetadata(metadataMockAllValid6 as NFTMetadata) + validateMetadata(metadataMockAllValid6 as Metadata) ).not.toThrow(); }); }); @@ -54,45 +54,33 @@ describe("validation: validateMetadata with invalid mocks", () => { it("should be invalid", () => { expect(() => validateMetadata({ - image: "file://dfsdf", - } as NFTMetadata) + mediaUri: "file://dfsdf", + } as Metadata) ).toThrow(); expect(() => validateMetadata({ name: 1, - } as NFTMetadata) + } as Metadata) ).toThrow(); expect(() => validateMetadata({ description: 1, - } as NFTMetadata) - ).toThrow(); - - expect(() => - validateMetadata({ - background_color: 1, - } as NFTMetadata) + } as Metadata) ).toThrow(); expect(() => validateMetadata({ image: 1, - } as NFTMetadata) + } as Metadata) ).toThrow(); - expect(() => - validateMetadata({ - name: "Mock", - } as NFTMetadata) - ).toThrow(); - expect(() => validateMetadata({ image: "ipfs://dfsdf", - external_url: "Mock", - } as NFTMetadata) + externalUri: "Mock", + } as Metadata) ).toThrow(); }); @@ -101,7 +89,7 @@ describe("validation: validateMetadata with invalid mocks", () => { validateMetadata({ image: "ipfs://dfsdf", properties: { test: { value: 123, type: "string" } }, - } as NFTMetadata) + } as Metadata) ).toThrow(); expect(() => @@ -113,7 +101,7 @@ describe("validation: validateMetadata with invalid mocks", () => { value: "123", }, }, - } as NFTMetadata) + } as Metadata) ).toThrow(); expect(() => @@ -125,7 +113,7 @@ describe("validation: validateMetadata with invalid mocks", () => { value: 123, }, }, - } as NFTMetadata) + } as Metadata) ).toThrow(); expect(() => @@ -137,7 +125,7 @@ describe("validation: validateMetadata with invalid mocks", () => { value: 123, }, }, - } as NFTMetadata) + } as Metadata) ).toBeTruthy(); expect(() => @@ -149,7 +137,7 @@ describe("validation: validateMetadata with invalid mocks", () => { value: "123", }, }, - } as NFTMetadata) + } as Metadata) ).toThrow(); }); }); From 30b874f628cdef9e76c7f52facae14448b9abe4a Mon Sep 17 00:00:00 2001 From: Yuripetusko Date: Tue, 6 Sep 2022 10:14:32 +0000 Subject: [PATCH 2/2] v2.0.91 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5d86e80c..e74104b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rmrk-tools", - "version": "2.0.90", + "version": "2.0.91", "description": "Suite of libraries implementing RMRK.app specs", "repository": { "type": "git",