@traittech/traits-validator-2 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ ## @trevo-finance/traits-validator
2
+
3
+ Lightweight helper over the Trevo Traits Registry.
4
+
5
+ ### What it does
6
+
7
+ - bundles `traits/` JSON Schemas into a single `registry.json`;
8
+ - exposes `validateMetadata(payload)` and `validateTraits(payload, registry?, traitsToValidate?)`;
9
+ - ships both ESM and CJS builds so it can run in Token Studio, Trevo Wallet or backend tooling.
10
+
11
+ ### Development
12
+
13
+ ```
14
+ yarn install
15
+ yarn build
16
+ ```
17
+
18
+ `yarn build` will re-generate `schemes/registry.json` + `schemes/metadata_schema.json` before compiling TypeScript.
19
+
@@ -0,0 +1,7 @@
1
+ import type { JsonMetadata, Registry } from './types.js';
2
+ import metadataSchema from '../schemes/metadata_schema.json';
3
+ import registry from '../schemes/registry.json';
4
+ export declare const validateMetadata: (data: JsonMetadata) => boolean;
5
+ export declare const validateTraits: (data: JsonMetadata, bundle?: Registry, traitsToValidate?: string[]) => string[];
6
+ export { metadataSchema, registry };
7
+ export * from './types.js';
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.registry = exports.metadataSchema = exports.validateTraits = exports.validateMetadata = void 0;
21
+ const _2020_js_1 = __importDefault(require("ajv/dist/2020.js"));
22
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
23
+ const metadata_schema_json_1 = __importDefault(require("../schemes/metadata_schema.json"));
24
+ exports.metadataSchema = metadata_schema_json_1.default;
25
+ const registry_json_1 = __importDefault(require("../schemes/registry.json"));
26
+ exports.registry = registry_json_1.default;
27
+ const ajv = new _2020_js_1.default({ allErrors: true, strict: false });
28
+ (0, ajv_formats_1.default)(ajv);
29
+ const metadataValidate = ajv.compile(metadata_schema_json_1.default);
30
+ const validateMetadata = (data) => metadataValidate(data);
31
+ exports.validateMetadata = validateMetadata;
32
+ const validateTraits = (data, bundle = registry_json_1.default, traitsToValidate) => {
33
+ if (!(0, exports.validateMetadata)(data)) {
34
+ throw new Error('Metadata payload is invalid');
35
+ }
36
+ const traitIds = traitsToValidate ?? Object.keys(data.traits || {});
37
+ return traitIds.filter((traitId) => {
38
+ const traitSchema = bundle[traitId];
39
+ const traitValue = data.traits?.[traitId];
40
+ if (!traitSchema || !traitValue) {
41
+ return false;
42
+ }
43
+ const validate = ajv.compile(traitSchema);
44
+ return validate(traitValue);
45
+ });
46
+ };
47
+ exports.validateTraits = validateTraits;
48
+ __exportStar(require("./types.js"), exports);
@@ -0,0 +1,8 @@
1
+ export type JsonSchema = Record<string, unknown>;
2
+ export type Registry = Record<string, JsonSchema>;
3
+ export type JsonMetadata = {
4
+ metadata_id: string;
5
+ title: string;
6
+ description?: string;
7
+ traits: Record<string, Record<string, unknown>>;
8
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import type { JsonMetadata, Registry } from './types.js';
2
+ import metadataSchema from '../schemes/metadata_schema.json';
3
+ import registry from '../schemes/registry.json';
4
+ export declare const validateMetadata: (data: JsonMetadata) => boolean;
5
+ export declare const validateTraits: (data: JsonMetadata, bundle?: Registry, traitsToValidate?: string[]) => string[];
6
+ export { metadataSchema, registry };
7
+ export * from './types.js';
@@ -0,0 +1,25 @@
1
+ import Ajv2020 from 'ajv/dist/2020.js';
2
+ import addFormats from 'ajv-formats';
3
+ import metadataSchema from '../schemes/metadata_schema.json';
4
+ import registry from '../schemes/registry.json';
5
+ const ajv = new Ajv2020({ allErrors: true, strict: false });
6
+ addFormats(ajv);
7
+ const metadataValidate = ajv.compile(metadataSchema);
8
+ export const validateMetadata = (data) => metadataValidate(data);
9
+ export const validateTraits = (data, bundle = registry, traitsToValidate) => {
10
+ if (!validateMetadata(data)) {
11
+ throw new Error('Metadata payload is invalid');
12
+ }
13
+ const traitIds = traitsToValidate ?? Object.keys(data.traits || {});
14
+ return traitIds.filter((traitId) => {
15
+ const traitSchema = bundle[traitId];
16
+ const traitValue = data.traits?.[traitId];
17
+ if (!traitSchema || !traitValue) {
18
+ return false;
19
+ }
20
+ const validate = ajv.compile(traitSchema);
21
+ return validate(traitValue);
22
+ });
23
+ };
24
+ export { metadataSchema, registry };
25
+ export * from './types.js';
@@ -0,0 +1,8 @@
1
+ export type JsonSchema = Record<string, unknown>;
2
+ export type Registry = Record<string, JsonSchema>;
3
+ export type JsonMetadata = {
4
+ metadata_id: string;
5
+ title: string;
6
+ description?: string;
7
+ traits: Record<string, Record<string, unknown>>;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ {
2
+ "$id": "https://trevo-finance.github.io/trevo-traits-registry/traits/metadata_schema.json",
3
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4
+ "title": "Metadata of on-chain entity",
5
+ "description": "Metadata of an on-chain entity, such as token, NFT, AppAgent etc.",
6
+ "type": "object",
7
+ "properties": {
8
+ "metadata_id": {
9
+ "description": "The identifier of the document with metadata. Depending on the context, this can be an identifier of on-chain entity, that metadata annotates. Or it can be an application-specific identifier.",
10
+ "type": "string",
11
+ "minLength": 5,
12
+ "maxLength": 128
13
+ },
14
+ "title": {
15
+ "description": "The human readable title of the document with metadata.",
16
+ "type": "string",
17
+ "minLength": 5,
18
+ "maxLength": 128
19
+ },
20
+ "description": {
21
+ "description": "The human readable description of the metadata document.",
22
+ "type": "string",
23
+ "maxLength": 1024
24
+ },
25
+ "traits": {
26
+ "description": "The list of traits supported by on-chain entity, that is annotated with this metadata document.",
27
+ "type": "object",
28
+ "minProperties": 1,
29
+ "propertyNames": {
30
+ "type": "string",
31
+ "minLength": 5,
32
+ "maxLength": 128
33
+ },
34
+ "patternProperties": {
35
+ "^.*$": { "type": "object" }
36
+ }
37
+ }
38
+ },
39
+ "required": [ "metadata_id", "title", "traits" ],
40
+ "additionalProperties": false
41
+ }