# Validate Metadata

Metadata is user-generated. Wallets have to confirm their correctness. Before performing any further action on metadata, wallets have to validate metadata.

### *What is JSON Schema?* 🧐

JSON Schema is **a JSON media type for defining the structure of the JSON data**. JSON Schema provides a contract for what JSON data is required and how to interact with it.

JSON schemas can be created and used to validate received data against a predefined data model. \
\
There are many validator NPM packages available to validate metadata and provide the functionality to:

* Generate Schema
* Compile Schema
* Validate received data for that particular schema

[***Zod***](https://www.npmjs.com/package/zod)  NPM package is one of the validators used to validate JSON and object schema. The best thing about this package is that it has zero external dependencies involved.

**Validator function to validate metadata against predefined schema generated using the Zod NPM package**

* Schema Created using Zod NPM package

```
 import { z } from "zod";

 export const audioObjectSchema = z.object({
   type: z.string(),
   name: z.string().optional(),
   creator: z.string().optional(),
   image: z.string().optional(),
 });
```

* Custom and Reusable Validator function to validate metadata

```
import { audioObjectSchema } from "./audioObjectSchema";

export function isBase64(str: string) {
  if (str === "" || str.trim() === "") {
    return false;
  }
  try {
    return btoa(atob(str)) == str;
  } catch (err) {
    return false;
  }
}

export function MetadataValidator(metadata: object) {
  let hasValidType: boolean = false;
  let hasValidImage: boolean = true;
  for (const [key, value] of Object.entries(metadata)) {
    if (key == "type") {
      if (value == "AudioObject") {
        const parsed = audioObjectSchema.safeParse(metadata);
        if (parsed.success) {
          hasValidType = true;
        }
      }
    }
    if (key == "image") {
      hasValidImage = isBase64(value);
    }
  }

  return hasValidType && hasValidImage;
}
```

* To Validate metadata, call the function by passing metadata.

```
const isMetadataValid = MetadataValidator(metadata);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pavan-joshi.gitbook.io/making-payment-transactions-smart/working-architecture-of-spec/validate-metadata.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
