<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shivam Bharadwaj</title>
    <description>The latest articles on DEV Community by Shivam Bharadwaj (@shivamb).</description>
    <link>https://dev.to/shivamb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2532416%2F04dba7db-73ac-498d-91a9-9a716417b792.jpg</url>
      <title>DEV Community: Shivam Bharadwaj</title>
      <link>https://dev.to/shivamb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivamb"/>
    <language>en</language>
    <item>
      <title>Which is Better for Prompt Engineering: Deepseek R1 or OpenAI o1?</title>
      <dc:creator>Shivam Bharadwaj</dc:creator>
      <pubDate>Sun, 09 Feb 2025 11:52:21 +0000</pubDate>
      <link>https://dev.to/shivamb/which-is-better-for-prompt-engineering-deepseek-r1-or-openai-o1-46la</link>
      <guid>https://dev.to/shivamb/which-is-better-for-prompt-engineering-deepseek-r1-or-openai-o1-46la</guid>
      <description>&lt;h2&gt;
  
  
  In the rapidly evolving field of artificial intelligence, two models have recently garnered significant attention: &lt;strong&gt;DeepSeek’s R1&lt;/strong&gt; and &lt;strong&gt;OpenAI’s o1&lt;/strong&gt;.
&lt;/h2&gt;

&lt;p&gt;Both models aim to advance AI capabilities, but they differ in design philosophy, performance, and accessibility. This blog provides a comprehensive comparison between DeepSeek R1 and OpenAI o1, focusing on their accuracy, performance, and practical applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Overview of DeepSeek R1 and OpenAI o1&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Developed By&lt;/strong&gt; :  Chinese startup DeepSeek,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;open-source AI model&lt;/strong&gt; :  R1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It emphasizes cost efficiency and accessibility. It employs a “mixture of experts” technique, delegating specific tasks to specialized sub-models, thereby reducing computational requirements. Notably, DeepSeek R1’s model weights are publicly available under an MIT license, promoting transparency and community collaboration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://decrypt.co/302161/chinese-open-source-ai-deepseek-r1-openai-o1" rel="noopener noreferrer"&gt;Chinese Open-Source AI DeepSeek R1 Matches OpenAI's o1 at 98% Lower Cost&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OpenAI’s o1 is a proprietary AI model designed for a broad range of applications, from natural language understanding to complex problem-solving. While it offers robust performance across various tasks, access to o1 is restricted and typically requires a subscription or usage-based payment, limiting its availability to certain users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance Comparison
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Accuracy and Reasoning&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both models have been evaluated on their reasoning and problem-solving capabilities. In benchmark tests, &lt;strong&gt;DeepSeek R1&lt;/strong&gt; has demonstrated performance comparable to &lt;strong&gt;OpenAI’s o1&lt;/strong&gt;, effectively handling multi-step reasoning and complex decision-making tasks. However, in certain specific problem-solving scenarios, o1 has shown a slight edge over R1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://arstechnica.com/ai/2025/01/how-does-deepseek-r1-really-fare-against-openais-best-reasoning-models/?utm_source=chatgpt.com&amp;amp;source=post_page-----b79197ef4ac3--------------------------------" rel="noopener noreferrer"&gt;How does DeepSeek R1 really fare against OpenAI’s best reasoning models? &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost Efficiency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A significant distinction between the two models lies in their cost structures. DeepSeek R1 was developed with a focus on minimizing computational expenses, reportedly achieving performance parity with OpenAI’s models at just 3% of the cost. This cost efficiency is attributed to its innovative training methodologies and optimized resource utilization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://venturebeat.com/ai/deepseek-r1s-bold-bet-on-reinforcement-learning-how-it-outpaced-openai-at-3-of-the-cost/?utm_source=chatgpt.com&amp;amp;source=post_page-----b79197ef4ac3--------------------------------" rel="noopener noreferrer"&gt;DeepSeek-R1’s bold bet on reinforcement learning: How it outpaced OpenAI at 3% of the cost&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accessibility&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DeepSeek R1’s open-source nature, with publicly available model weights under an MIT license, makes it highly accessible to developers and organizations seeking customizable AI solutions without significant financial investment. In contrast, OpenAI’s o1, being proprietary, imposes access limitations and associated costs, which may be prohibitive for some users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Applications: Prompt and Response Examples
&lt;/h2&gt;

&lt;p&gt;To illustrate the practical differences between DeepSeek R1 and OpenAI o1, let’s examine their responses to a series of prompts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt 1: Explain the concept of reinforcement learning in one paragraph.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt;: &lt;em&gt;“Reinforcement learning (RL) is a type of machine learning where an agent learns to make decisions by interacting with an environment to maximize cumulative rewards. The agent takes actions based on its current state, and the environment responds with a new state and a reward signal, which indicates the success or failure of the action. Through trial and error, the agent develops a policy — a strategy for choosing actions — that optimizes long-term rewards. RL is unique because it focuses on learning from delayed feedback, balancing exploration (trying new actions) and exploitation (using known effective actions). It is widely used in areas like robotics, game playing, and autonomous systems, where sequential decision-making is critical.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D9239496325908240783" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D9239496325908240783" width="900" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1&lt;/strong&gt;: &lt;em&gt;“Reinforcement Learning (RL) is a type of machine learning where an agent learns to make decisions by interacting with an environment to maximize cumulative rewards. The agent takes actions, receives feedback in the form of rewards or penalties, and adjusts its strategy based on this experience. RL is based on trial and error, leveraging concepts like exploration (trying new actions) and exploitation (choosing the best-known action). It often uses algorithms like Q-learning, Deep Q Networks (DQN), and Policy Gradient methods. RL is widely applied in robotics, game playing (e.g., AlphaGo), self-driving cars, and financial trading.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D2368510187753664767" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D2368510187753664767" width="900" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt 2: Translate the following English sentence into French: “Artificial intelligence is transforming the world.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt;: &lt;em&gt;“L’intelligence artificielle transforme le monde.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D7989722042807134809" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D7989722042807134809" width="900" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1&lt;/strong&gt;: &lt;em&gt;“L’intelligence artificielle transforme le monde.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D7340532740193924886" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D7340532740193924886" width="900" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt 3: Solve the mathematical equation: What is the integral of x²?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt;: &lt;em&gt;“To find the integral of x2x2, we can use the power rule for integration. The power rule states that: ∫x^n * dx = (x^(n+1)/n+1)+C where n is any real number except −1, and C is the constant of integration. Applying the power rule to x^2: ∫x^2 dx = (x^(2+1)/2+1)+C = (x^3)/3+C So, the integral of x^2 is: ((x^3)/3)+C”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D10027980657435399845" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D10027980657435399845" width="900" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1&lt;/strong&gt;: &lt;em&gt;“∫x² dx = (1/3)x³ + C, where C represents the constant of integration.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D8001990258558858225" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D8001990258558858225" width="900" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt 4: Generate a short poem about the ocean.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Beneath the sky’s vast, endless dome,
The ocean whispers, a restless poem.
Its waves, like verses, rise and fall,
A timeless song that calls to all.

In depths where shadows softly gleam,
Lies a world of wonder, a liquid dream.
With every tide, it breathes anew,
A boundless blue, forever true.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D123675115979598694" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D123675115979598694" width="900" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The ocean hums a melody deep,
A song of secrets waves do keep.
Golden sun and silver moon,
Pull its tides in gentle tune.

Foam-kissed shores and winds that roam,
Carve the sand and shape the stone.
Endless blue, so wild and free,
A whispered call — come dance with me.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D16398016164184526562" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D16398016164184526562" width="900" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt 5: Describe the process of photosynthesis in one paragraph.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt;: &lt;em&gt;“Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy, usually from the sun, into chemical energy stored in glucose. It occurs primarily in the chloroplasts of plant cells, which contain the pigment chlorophyll. The process involves two main stages: the light-dependent reactions and the Calvin cycle. In the light-dependent reactions, light energy is absorbed by chlorophyll, driving the splitting of water molecules into oxygen, protons, and electrons. This process generates ATP and NADPH, which are energy carriers. In the Calvin cycle (light-independent reactions), these energy carriers are used to fix carbon dioxide from the atmosphere into organic molecules, ultimately producing glucose. Oxygen is released as a byproduct, making photosynthesis crucial for maintaining atmospheric oxygen levels and supporting life on Earth.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D8117989594579094643" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D8117989594579094643" width="900" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI o1&lt;/strong&gt;: &lt;em&gt;“Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy into chemical energy to produce food. It occurs in the chloroplasts of plant cells, where chlorophyll absorbs sunlight. The process involves two main stages: the light-dependent reactions and the Calvin cycle (light-independent reactions). In the light-dependent reactions, sunlight splits water molecules into oxygen, protons, and electrons, generating ATP and NADPH. In the Calvin cycle, carbon dioxide is fixed and combined with these energy carriers to synthesize glucose. Oxygen is released as a byproduct, making photosynthesis essential for sustaining life on Earth.”&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D12591873850076306866" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderlegion.com%2F%3Fqa%3Dblob%26qa_blobid%3D12591873850076306866" width="900" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In these examples, both models provide accurate and coherent responses, demonstrating their proficiency in understanding and generating human-like text. However, subtle differences in phrasing and detail may influence user preference based on specific requirements.&lt;br&gt;
Conclusion&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeepSeek R1&lt;/strong&gt; and &lt;strong&gt;OpenAI o1&lt;/strong&gt; represent significant advancements in AI language models, each with its unique strengths. DeepSeek R1 offers a cost-effective, open-source alternative with performance comparable to proprietary models, making it an attractive option for developers and organizations prioritizing accessibility and customization. Conversely, OpenAI o1 provides robust performance across a wide range of applications but comes with access limitations and associated costs.&lt;/p&gt;

&lt;p&gt;The choice between DeepSeek R1 and OpenAI o1 ultimately depends on specific use cases, resource availability, and the importance of open-source accessibility to the user.&lt;/p&gt;

</description>
      <category>deepseek</category>
      <category>openai</category>
      <category>chatgpt</category>
      <category>ai</category>
    </item>
    <item>
      <title>Schema Validation In JavaScript 🔥</title>
      <dc:creator>Shivam Bharadwaj</dc:creator>
      <pubDate>Fri, 17 Jan 2025 15:57:03 +0000</pubDate>
      <link>https://dev.to/shivamb/schema-validation-in-javascript-mb2</link>
      <guid>https://dev.to/shivamb/schema-validation-in-javascript-mb2</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jbuwro9xor195m9jkf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jbuwro9xor195m9jkf6.png" alt=" " width="800" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever found yourself in a situation where you're unsure if the data your application receives is correct? Perhaps you've built a captivating form, only to discover that the responses are cringe-worthy and inconsistent, leading to errors in your application later on.&lt;br&gt;
It's frustrating, isn't it?&lt;/p&gt;

&lt;p&gt;This is a common problem for many developers. Luckily, I'm here to offer you a practical solution: schema validation with the AJV (Another JSON Schema Validator) library.&lt;/p&gt;

&lt;p&gt;In this article, we'll dive into what schema validation is, why it's essential, and how you can leverage the AJV library to ensure that the data flowing into your application is valid and trustworthy.&lt;br&gt;
Don't worry; I'll guide you through every step of the process. By the end of this article, you will be ready to implement schema validation like a pro!&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Use AJV for Schema Validation?
&lt;/h2&gt;

&lt;p&gt;AJV is a popular lightweight JSON schema validator that's not only fast but also offers a rich set of features. It's widely used by developers who need to ensure that their data structures adhere to a specific format.&lt;br&gt;
In short, using AJV can enhance data integrity, reduce errors, and simplify debugging in your applications. And believe me, I've faced countless debugging sessions where timely schema validation could have saved me hours.&lt;br&gt;
You can get started with AJV by installing it via npm:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install ajv&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Let's look into some common questions people have when it comes to schema validation using AJV.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What is schema validation?
&lt;/h3&gt;

&lt;p&gt;Schema validation involves defining a structure (or schema) that your data should adhere to and checking incoming data against this standard. For example, if your API endpoint expects an object with specific properties, a schema definition will ensure that the data received meets these requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. How do I use AJV in my project?
&lt;/h3&gt;

&lt;p&gt;Using AJV is straightforward. Here's a step-by-step example for you. First, define a JSON schema. Let's say you need to validate user profiles with the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name (string, required)
age (integer, required)
email (string, must match email format)

Here's how you could define that schema:
const userSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "integer", minimum: 0 },
    email: { type: "string", format: "email" },
  },
  required: ["name", "age", "email"],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to validate an incoming user object, follow these steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Ajv = require("ajv");
const ajv = new Ajv();
const validate = ajv.compile(userSchema);

// Example user data
const userData = {
  name: "John Doe",
  age: 30,
  email: "john.doe@example.com",
};

const valid = validate(userData);

if (!valid) {
  console.log(validate.errors);
} else {
  console.log("User data is valid!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. What happens if the data doesn't match the schema?
&lt;/h3&gt;

&lt;p&gt;If the data doesn't match the defined schema, AJV provides detailed error messages.&lt;br&gt;
This is where the magic happens! You'll see information such as which property was incorrect and what values were expected. Here's an example object that fails validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const invalidUserData = {
  name: "Jane Doe",
  age: -10, // Invalid age
};

const validInvalid = validate(invalidUserData);

if (!validInvalid) {
  console.error(validate.errors); // This will show the specific error messages
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Can I use AJV for complex validations?
&lt;/h3&gt;

&lt;p&gt;Absolutely! AJV supports many advanced features, including arrays, nested objects, and custom validation keywords. You can even define your own keywords for specific needs. It's incredibly powerful! For example, to define an array of items, you could do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shoppingListSchema = {
  type: "object",
  properties: {
    items: {
      type: "array",
      items: { type: "string" },
      minItems: 1,
    },
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Example - Function to Generate/Validate JSON Schema
&lt;/h3&gt;

&lt;p&gt;Here is a function that takes 2 arguments { response: APIResponse, fileName: string } and generate the equivalent JSON schema.&lt;br&gt;
json-schema-generator - &lt;a href="https://www.npmjs.com/package/json-schema-generator" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/json-schema-generator&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { APIResponse} from '@playwright/test'
import { promises as fs } from 'fs'
import generateSchema from 'json-schema-generator'
import path from 'path'

async generateJsonSchema(options: { response: APIResponse, fileName: string }): Promise&amp;lt;APIHelper&amp;gt; {
        const responseBody: APIResponse = await options.response.json()
        const jsonSchema = generateSchema(responseBody)

        const schemaFolderPath = path.resolve(__dirname, '../../test/schema')
        const filePath = path.join(schemaFolderPath, `${options.fileName}.json`)

        await fs.mkdir(schemaFolderPath, { recursive: true })
        await fs.writeFile(filePath, JSON.stringify(jsonSchema, null, 2))

        return this
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Function to validate JSON Schema -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { APIResponse} from '@playwright/test'
import Ajv from "ajv"
import AjvDraft04 from 'ajv-draft-04'
import addFormats from 'ajv-formats'

async validateJsonSchema(response: APIResponse, schema: Record&amp;lt;string, any&amp;gt;): Promise&amp;lt;APIHelper&amp;gt; {
        const ajv: Ajv = new AjvDraft04()
        addFormats(ajv)
        const validate = ajv.compile(schema)
        const valid = validate(await response.json())
        if (!valid) {
            throw new Error(`JSON Schema validation failed: ${JSON.stringify(validate.errors)}`)
        }

        return this
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Where can I learn more about AJV?
&lt;/h3&gt;

&lt;p&gt;The official AJV documentation is an excellent resource that covers everything from installation to advanced validation scenarios. Additionally, community forums and platforms like Stack Overflow often have discussions that can be beneficial.&lt;br&gt;
Empower Your Applications!&lt;/p&gt;

&lt;p&gt;In the realm of development, ensuring that your data is valid is like putting on your seatbelt before driving - it's a safety measure that can save you from serious accidents down the road. Implementing schema validation with AJV not only brings structure to your code but also saves you time by catching errors early in the development process.&lt;/p&gt;

&lt;p&gt;I hope you found this guide helpful! If you try out AJV, don't be discouraged if you encounter some hiccups along the way. Learning is a journey, and the more you practice, the better you'll get. So go ahead, implement schema validation in your projects, and watch your applications thrive! And remember, every mistake is just a stepping stone to mastery.&lt;/p&gt;

&lt;p&gt;Interested in enhancing your technical stack further?&lt;br&gt;
Check out the AJV GitHub repository or look into other great validation libraries to see what fits your needs.&lt;br&gt;
Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>schema</category>
      <category>api</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
