Skip to content

Commit

Permalink
Update local validation export and implementation (#60)
Browse files Browse the repository at this point in the history
Signed-off-by: Michiel Mulders <[email protected]>
  • Loading branch information
michielmulders authored Apr 25, 2024
1 parent d8dfc9f commit 75c4ff7
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ Import the package into your project and get the `localValidation` function.
const { localValidation } = require('@hashgraph/hedera-nft-sdk');
```

The `localValidation` expects an absolute path to your metadata files to verify them. The function prints the warnings and errors for all JSON files it finds in the provided folder path. It also returns the validation results as an object in case you want to use the results in your code.
The `localValidation` expects a relative path to your metadata files to verify them. The function prints the warnings and errors for all JSON files it finds in the provided folder path. It also returns the validation results as an object in case you want to use the results in your code.

```js
localValidation('/Users/projects/nft/files');
const issues = localValidation('/Users/projects/nft/files');
```

This package uses the `Validator` class explained in the [previous section](#token-metadata-validator).
Expand Down
11 changes: 5 additions & 6 deletions examples/local-metadata-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
* limitations under the License.
*
*/
const { localValidation } = require('../../local-validation');
const { localValidation } = require('../../dist/local-validation');

function main() {
// Replace with absolute path to files folder
const absolutePathToFiles = '/Users/myUser/hedera-nft-sdk/examples/local-metadata-validator/files';
// Replace with relative path to files folder
const relativePathToFiles = './examples/local-metadata-validator/files';

// Looks for JSON files and uses validator package to verify each metadata file (notjson.md file will be ignored)
// files/nft2.json has mistakes: no image and type properties
localValidation(absolutePathToFiles);
const issues = localValidation(relativePathToFiles);
console.log(issues);

/* Output:
Found 6 for directory: /Users/myUser/hedera-nft-sdk/examples/local-metadata-validator/files
Found 5 files with the .json extension
{
"nft1.json": {"errors":[],"warnings":[]},
"nft2.json":{"errors":[{"type":"schema","msg":"requires property 'image'","path":"instance"},{"type":"schema","msg":"requires property 'type'","path":"instance"}],"warnings":[]},
Expand Down
14 changes: 6 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hashgraph/hedera-nft-sdk",
"version": "3.0.7",
"version": "3.1.0",
"description": "NFT SDK for Hedera Hashgraph",
"author": "Michiel Mulders",
"license": "Apache License",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/
import { Validator, defaultSchemaVersion } from './validator';
import localValidation from './local-validation';
import { localValidation } from './local-validation';
import { defaultWeights, defaultRiskLevels, calculateRiskScoreFromData, calculateRiskScoreFromTokenId, calculateRiskLevel } from './risk';
import { calculateRarity, calculateRarityFromData, calculateTraitOccurrenceFromData, calculateRarityFromOnChainData } from './rarity';

Expand Down
16 changes: 7 additions & 9 deletions src/local-validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*
*/
import path from 'path';
import { Validator } from '../validator/index';
import { readFiles, getJSONFilesForDir } from '../helpers/files';

Expand Down Expand Up @@ -46,21 +47,18 @@ const validateFiles = (files: File[]): ValidationResults => {
/**
* Validate files locally
*
* @param {string} path Absolute path to folder containing files
* @param {string} relative Relative path to folder containing files
* @returns {Object<filename<string>, validationResults<Object>>}
*/
const localValidation = (path: string): ValidationResults => {
const filenames = getJSONFilesForDir(path);
const filedata = readFiles(path, filenames);
const localValidation = (relativePath: string): ValidationResults => {
const absolutePath = path.resolve(relativePath); // convert relative path to absolute path
const filenames = getJSONFilesForDir(absolutePath);
const filedata = readFiles(absolutePath, filenames);
const validationResults = validateFiles(filedata);

// Print results for the user
// eslint-disable-next-line no-console
console.log(JSON.stringify(validationResults));

return validationResults;
};

export default {
export {
localValidation,
};
10 changes: 5 additions & 5 deletions src/services/csv-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ function processHeader(
attributesIndex: number,
refToErrorArray: string[]
): {
result: string | null;
currentType: CurrentType;
propertyIndex: number;
attributesIndex: number;
} {
result: string | null;
currentType: CurrentType;
propertyIndex: number;
attributesIndex: number;
} {
let result: string | null = null;

// TODO: try to simplyfy this
Expand Down
10 changes: 5 additions & 5 deletions src/services/file-storages/pinata/pinata-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export class PinataService implements FileStorage {

const authorization = this.pinataJwtKey
? {
Authorization: `Bearer ${this.pinataJwtKey}`,
}
Authorization: `Bearer ${this.pinataJwtKey}`,
}
: {
pinata_api_key: this.pinataApiKey,
pinata_secret_api_key: this.pinataSecretApiKey,
};
pinata_api_key: this.pinataApiKey,
pinata_secret_api_key: this.pinataSecretApiKey,
};

const res = await this.instance.post(this.uploadUrl, formData, {
headers: {
Expand Down

0 comments on commit 75c4ff7

Please sign in to comment.