Skip to content

Commit

Permalink
Merge pull request #3 from rmrk-team/master
Browse files Browse the repository at this point in the history
Sync with upstream
  • Loading branch information
spenweb authored Sep 22, 2022
2 parents e6be598 + 30b874f commit 9e2a28c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/rmrk2.0.0/tools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
12 changes: 4 additions & 8 deletions src/rmrk2.0.0/tools/validate-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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;
};
46 changes: 17 additions & 29 deletions test/2.0.0/utils/validate-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { validateMetadata } from "../../../src/rmrk2.0.0/tools/validate-metadata";
import { NFTMetadata } from "../../../src/rmrk2.0.0/classes/nft";
import {
attributesMockBoostNumberValid,
metadataMockAllValid,
metadataMockAllValid2,
metadataMockAllValid4,
metadataMockAllValid6,
} from "../mocks/metadata-valid";
import {Metadata} from "../../../src/rmrk2.0.0";

export const attributesMockDateInvalid = [
{
Expand All @@ -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();
});
});
Expand All @@ -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();
});

Expand All @@ -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(() =>
Expand All @@ -113,7 +101,7 @@ describe("validation: validateMetadata with invalid mocks", () => {
value: "123",
},
},
} as NFTMetadata)
} as Metadata)
).toThrow();

expect(() =>
Expand All @@ -125,7 +113,7 @@ describe("validation: validateMetadata with invalid mocks", () => {
value: 123,
},
},
} as NFTMetadata)
} as Metadata)
).toThrow();

expect(() =>
Expand All @@ -137,7 +125,7 @@ describe("validation: validateMetadata with invalid mocks", () => {
value: 123,
},
},
} as NFTMetadata)
} as Metadata)
).toBeTruthy();

expect(() =>
Expand All @@ -149,7 +137,7 @@ describe("validation: validateMetadata with invalid mocks", () => {
value: "123",
},
},
} as NFTMetadata)
} as Metadata)
).toThrow();
});
});

0 comments on commit 9e2a28c

Please sign in to comment.