fastest-validator: ensure that two fields are not the same

My current validation process involves using fastest-validator. Here is an example of the object being validated:

{
    "_id":"619e00c177f6ea2eccffd09f",
    "parent_id": "619e00c177f6ea2eccffd09f",
}

I need to ensure that _id and parent_id are not equal. How can I perform this check using fastest-validator? The library provides a method for validating equality, but I require the opposite behavior.

const schema = {
    password: { type: "string", min: 6 },
    confirmPassword: { type: "equal", field: "password" }
}
const check = v.compile(schema);

check({ password: "123456", confirmPassword: "123456" }); // Valid
check({ password: "123456", confirmPassword: "pass1234" }); // Fail

Any suggestions or insights regarding this matter would be greatly appreciated. Thank you in advance!

Answer №1

Exploring the concept of Custom Validator Meta Information:

Utilizing additional meta information for custom validators can be accessed through context.meta.

Therefore, I have the ability to compare any pair of fields as needed. For instance, to compare _id and parent_id, the following approach can be implemented:

const v = new Validator({
    useNewCustomCheckerFunction: true,
    messages: {
        notEqual: "_id and parent_id cannot be identical"
    }
});

const editSchema = {
    _id: { type: "string" },
    parent_id: {
        type: "string",
        custom: (value, errors, schema, name, parent, context) =>{
            if (context.data._id === value) errors.push({type: "notEqual"})
            return value
        }
    }
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CompareValidator causing Button_Click Event to not trigger

I am dealing with a situation where I have an <asp:Button/> on my page to navigate away, along with a <asp:TextBox/> for inputting data and another <asp:TextBox/> for confirming that data. The setup of the confirm validator is as follows ...

I am attempting to display only the description of the button that I click on

How can I ensure that only the container pressed will display its description when the description button is clicked, instead of all mapped containers showing their descriptions? `import { useState } from "react"; export default function Se ...

How to pass the ng-repeat $index value as a parameter in AngularJS

Within the code snippet provided, there is a shell page index.html and a partial view currently utilized by two different controllers. The static data in AppController is connected to the list.html partial view and displayed as a table. In the JavaScript p ...

Exploring the process of defining directives with Vue 3's composition API

How can directives be defined and used in Vue3's composition API using the new syntactic sugar in SFC <script setup> format? In the past, with the options API, it looked something like this: import click_outside from "@/directives/click-out ...

Does an inverted v-cloak exist?

As stated in the VueJS documentation, the v-cloak directive serves to conceal uncompiled mustache bindings until the Vue instance is fully prepared. This means that a particular element, such as a div, can be hidden and only displayed once Vue is ready. I ...

Encountering an unhandled promise rejection issue with Knex's batchInsert function when attempting to insert arrays larger than 3 elements

I am currently working on an express app and utilizing Knex as the query string builder. During batch insert operations with an array of 1000+ objects, I encountered an error when the array exceeded a certain length. The specific error message is provided ...

Using jQuery to solve a linear equation

I am having trouble figuring out how to find the solution for this linear equation using jQuery. 12.5x = 20 + x + (20 + x)/10 (Please note that 12.5, 20, and 10 are user-provided numbers) Is there a method in jQuery that can help me determine the value o ...

Is there a way to determine if a collapsed navbar is currently open?

My Bootstrap 4 navbar collapses on mobile, and I'm looking to detect when it opens and closes. Is there a way to achieve this? Thank you! ...

Guide to Making GraphQL API Calls from a Node.js/Express Server

After successfully implementing a schema and resolvers for my Express server, I tested them through /graphql. Now, I am looking to utilize the queries from a REST API by calling them in GET handlers like this: //[...] //schema and root are properly implem ...

Fetch search results dynamically in Wordpress through AJAX

I'm struggling to implement AJAX on my WordPress site to display search results without refreshing the page. Despite trying various solutions found through research, none seem to be working effectively for me. Currently, here is the progress I have ma ...

Is there a way to make the submit button navigate to the next tab, updating both the URL and the tab's content as well?

I am encountering an issue with my tabs for Step1 and Step2. After pressing the submit button in Step1, the URL updates but the component remains on tab1. How can I resolve this so that the user is directed to the Step2 tab once they click the submit butto ...

Could it be possible that my understanding of hoverIntent is incorrect?

I have a div that holds a series of li elements to construct a menu. Adjacent to the container ul, there is another div intended to appear only when an item in the original menu is hovered over. While I grasp the concept of mouseout and mouseover effects ...

How can I create a hover effect for a span element using html and css?

I have encountered an issue with hover color in span classes. I managed to successfully create two classes on this page, but the hover effect is not working on other classes. To troubleshoot, I included a link to my test page below. Can you please review i ...

Could integrating Firebase as the bridge between AngularJS and NodeJS be considered a less than optimal choice?

Seeking input on the design of a system I am constructing. Currently, I have several Node.JS scripts updating a Firebase DB, with front-end AngularJS apps retrieving data from the Firebase DB. This setup is functioning effectively. Now, I am looking to ...

Improving elements of a .php webpage

Is it possible to update a page and proceed with the next function without refreshing it? I have a page where clicking on the Save button submits data to earnings_amendment_account_add.php and refreshes the page. Now, my goal with AJAX is to submit the d ...

Incorporating dynamic images from local sources into a listview in a React Native

I'm currently having an issue with loading local images in a React Native app that I'm developing. As per the instructions provided in the documentation at https://facebook.github.io/react-native/docs/images.html It's mentioned in the guid ...

Is p-queue designed to utilize multiple threads?

Have you heard of a nodejs module that allows you to limit the number of concurrent promises? Check out this link I'm curious, does this module utilize multiple threads? Here's an example taken from the official page: const {default: PQueue} ...

Is it possible to debug JavaScript functions using Visual Studio?

I'm intrigued about debugging javascript functions in Visual Studio. If it's feasible, kindly share with me the process. I am currently involved in an asp.net web application project. ...

Struggling to locate a straightforward sidebar solution without relying on Bootstrap. Finding an easy-to-use answer seems

I have a lightweight and easy-to-understand code for a website project with two panels. The first panel on the left is a large navigation (270px width, top to bottom, similar to a wiki) with approximately 30 unordered list items. Next to it is the second ...

Unable to find the view titled "/landing.ejs" in the specified views directory

While working in Cloud9, I encountered an error when attempting to run the application. The error message states: "Failed to lookup view "/landing.ejs" in views directory." Here is my code snippet: var express = require("express"); var app = express(); a ...