-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Ability to add schema to Validator in-memory (#17)
Signed-off-by: Michiel Mulders <[email protected]> Signed-off-by: Iliya Savov <[email protected]> Co-authored-by: isavov <[email protected]>
- Loading branch information
1 parent
83f679b
commit 5c91932
Showing
10 changed files
with
219 additions
and
53 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
72 changes: 72 additions & 0 deletions
72
examples/token-metadata-validator/custom-schema-valid-metadata.js
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,72 @@ | ||
/*- | ||
* | ||
* Hedera NFT Utilities | ||
* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
const { Validator, defaultVersion } = require('../..'); | ||
|
||
function main() { | ||
// Define your JSON schema | ||
const customSchema = { | ||
"title": "Token Metadata", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"version": { | ||
"type": "string", | ||
"description": "Semantic version for the metadata JSON format." | ||
}, | ||
"name": { | ||
"type": "string", | ||
"description": "Identifies the asset to which this token represents." | ||
} | ||
}, | ||
"required": [ | ||
"version", | ||
"name" | ||
] | ||
} | ||
|
||
// Create Validator instance with custom schema | ||
const validator = new Validator([{ schemaObject: customSchema, tag: "custom-v1" }]); | ||
|
||
// Define metadata | ||
const metadataInstance = { | ||
"version": "v3.0.0", | ||
"name": "HANGRY BARBOON #2343", | ||
"image": "ipfs://QmaHVnnp7qAmGADa3tQfWVNxxZDRmTL5r6jKrAo16mSd5y/2343.png" | ||
} | ||
|
||
// Verify metadata against custom schema | ||
const results = validator.validate(metadataInstance, "custom-v1"); | ||
console.log(results); | ||
|
||
/* Output: | ||
{ | ||
errors: [], | ||
warnings: [ | ||
{ | ||
type: 'schema', | ||
msg: "is not allowed to have the additional property 'image'", | ||
path: 'instance' | ||
} | ||
] | ||
} | ||
*/ | ||
} | ||
|
||
main(); |
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
* limitations under the License. | ||
* | ||
*/ | ||
const { validator, defaultVersion } = require('../..'); | ||
const { Validator, defaultVersion } = require('../..'); | ||
|
||
function main() { | ||
const metadataInstance = { | ||
|
@@ -37,7 +37,8 @@ function main() { | |
] | ||
} | ||
|
||
const results = validator(metadataInstance); // by default: verifies metadata against [email protected] | ||
const validator = new Validator(); | ||
const results = validator.validate(metadataInstance); // by default: verifies metadata against [email protected] | ||
console.log(results); | ||
|
||
/* Output: | ||
|
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 |
---|---|---|
|
@@ -17,18 +17,18 @@ | |
* limitations under the License. | ||
* | ||
*/ | ||
const { validator } = require('../../validator/index'); | ||
const { defaultVersion } = require('../../validator/schemas'); | ||
const { Validator, defaultSchemaVersion } = require('../../validator/index'); | ||
const validMetadata = require('./data/valid-HIP412.json'); | ||
|
||
describe("Validator function tests", () => { | ||
describe("Schema version tests", () => { | ||
test("it should not return errors for a valid metadata JSON using default schema", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadata = JSON.parse(JSON.stringify(validMetadata)); | ||
|
||
// Act | ||
const schemaProblems = validator(metadata, defaultVersion); | ||
const schemaProblems = validator.validate(metadata, defaultSchemaVersion); | ||
|
||
// Assert | ||
expect(Array.isArray(schemaProblems.errors)).toBe(true); | ||
|
@@ -39,10 +39,11 @@ describe("Validator function tests", () => { | |
|
||
test("it should not return errors for a valid metadata JSON using schema version v1.0.0", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadata = JSON.parse(JSON.stringify(validMetadata)); | ||
|
||
// Act | ||
const schemaProblems = validator(metadata, "1.0.0"); | ||
const schemaProblems = validator.validate(metadata, "1.0.0"); | ||
|
||
// Assert | ||
expect(schemaProblems.warnings.length).toBe(0); | ||
|
@@ -51,10 +52,11 @@ describe("Validator function tests", () => { | |
|
||
test("it should not return errors for a valid metadata JSON not passing a schema version (using default version)", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadata = JSON.parse(JSON.stringify(validMetadata)); | ||
|
||
// Act | ||
const schemaProblems = validator(metadata); | ||
const schemaProblems = validator.validate(metadata); | ||
|
||
// Assert | ||
expect(schemaProblems.warnings.length).toBe(0); | ||
|
@@ -65,6 +67,7 @@ describe("Validator function tests", () => { | |
describe("Validator errors", () => { | ||
test("it should only return schema errors when the metadata contains schema errors and also other types of errors like attribute and localization", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadataCopy = JSON.parse(JSON.stringify(validMetadata)); | ||
let metadata = { | ||
// missing name, image, and type for [email protected] | ||
|
@@ -78,7 +81,7 @@ describe("Validator function tests", () => { | |
} | ||
|
||
// Act | ||
const validationResults = validator(metadata, defaultVersion); | ||
const validationResults = validator.validate(metadata, defaultSchemaVersion); | ||
|
||
// Assert | ||
expect(validationResults.errors.length).toBe(3); | ||
|
@@ -89,6 +92,7 @@ describe("Validator function tests", () => { | |
|
||
test("it should return all types of errors when there are no schema errors", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadataCopy = JSON.parse(JSON.stringify(validMetadata)); | ||
let metadata = { | ||
name: "myname", | ||
|
@@ -104,7 +108,7 @@ describe("Validator function tests", () => { | |
} | ||
|
||
// Act | ||
const validationResults = validator(metadata, defaultVersion); | ||
const validationResults = validator.validate(metadata, defaultSchemaVersion); | ||
|
||
// Assert | ||
expect(validationResults.errors.length).toBe(1); | ||
|
@@ -113,6 +117,7 @@ describe("Validator function tests", () => { | |
|
||
test("it should return all types of errors even when there are additional property warnings and no schema errors", () => { | ||
// Arrange | ||
const validator = new Validator(); | ||
let metadataCopy = JSON.parse(JSON.stringify(validMetadata)); | ||
let metadata = { | ||
name: "myname", | ||
|
@@ -128,7 +133,7 @@ describe("Validator function tests", () => { | |
} | ||
|
||
// Act | ||
const validationResults = validator(metadata, defaultVersion); | ||
const validationResults = validator.validate(metadata, defaultSchemaVersion); | ||
|
||
// Assert | ||
expect(validationResults.warnings.length).toBe(1); | ||
|
Oops, something went wrong.