Can someone guide me on how to begin creating a promised-based validation system using JavaScript and TypeScript?

In the process of creating a promised-based validation, I initially came up with the following approach:

export namespace Constraints {
  function required(value: any, vm: any, customParams: RequiredParams);
  function minLength(value: any, vm: any, customParams: LengthParams);
  function maxLength(value: any, vm: any, customParams: LengthParams);
}


 const ConstraintsInForm = {
  fields: {
    login: [
      { validator: Constraints .required },      
    ],
    password: [
      { validator: Constraints .required },
events: { onChange: true, onBlur: true },     
    ]
  }
};

I am in need of an example demonstrating how to create my own promised based validation using the provided constraints and field values.

The objective is to ensure flexibility within constraintsInForm so that with just a method call, I can obtain the corresponding errors dynamically.

Answer №1

Assuming that the functions required, minLength, and maxLength return a boolean value

function required(value: any, vm: any, customParams: RequiredParams); 
function minLength(value: any, vm: any, customParams: LengthParams);
function maxLength(value: any, vm: any, customParams: LengthParams);

One way to create a promise in JavaScript is as follows:

const ConstraintsInForm = {
  fields: {
    login,
    password,
  }
  events,     
};

const FieldPromise = (ConstraintsInForm) => {
  const fields = ConstraintsInForm.fields;

  return new Promise((resolve, reject) => {
    if(required(fields.login) && required(fields.password)){
      // The user has provided login and password
      if(minLength(fields.login) && maxLength(fields.password)){
        // Password meets length constraints
        // Proceed with using the data
        resolve(fields);
      } else {
        reject();
      }
    }
    else {
      reject();
    }
})}

This serves as a clue for implementing something similar in TypeScript

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

Change the way a button interacts with a different element

Currently, I am utilizing SlickSlider from http://kenwheeler.github.io/slick/ and attempting to integrate the Next and Prev buttons with other elements (specifically spans within another container). I have attempted the following: $('button.next&apo ...

After the form submission, my Next.js component keeps rendering repeatedly in a cumulative manner

I am currently working on a memory game application using Next.js, Node.js, and Express.js. I seem to be encountering an issue specifically with the login page. Initially, there are no issues when submitting the form for the first time. However, after the ...

issue with visibility of Angular Component

After following a straightforward YouTube tutorial for beginners on Angular, I encountered an issue. The tutorial covers how to use components, and even though I understand the concept well, the component simply does not appear no matter what I try. Here i ...

How to customize the color of Navbar pills in Bootstrap 4 to fill the entire height

Is there a way to ensure that the background of nav-items is completely filled with color rather than just partially? I attempted to use Bootstrap pills, but it did not achieve the desired effect. I also experimented with my own CSS, but encountered simil ...

Incorporating a JavaScript code into my SharePoint master page to automatically deselect a checkbox resulted in updating the page title

I've integrated the following JavaScript code into my SharePoint master page: <script type="text/javascript> function DefaultUploadOverwriteOff() { if (document.title== "Upload a document") { var input=document.querySelectorAll("input"); ...

Vue js version 2.5.16 will automatically detect an available port

Every time I run the npm run dev command in Vue.js, a new port is automatically selected for the development build. It seems to ignore the port specified in the config/index.js file. port: 8080, // can be overwritten by process.env.PORT, if port is in u ...

JSP page displaying a table with a text input field named "code" enclosed within <TD> tags

Recently, I designed a JSP page that features a table with two columns: Code and Description. The table data is an input type of "text" with the name attribute set to "code". The main functionality I need to implement is the automatic generation of the d ...

displaying a confirmation alert upon unchecking a checkbox using javascript

I am trying to implement a feature where a message saying "are you sure?" pops up when the user tries to uncheck a checkbox. If they choose "yes", then the checkbox should be unchecked, and if they choose "no" it should remain checked. I'm new to Java ...

Service in Angular2 designed to retrieve JSON data and extract specific properties or nodes from the JSON object

Currently, I am trying to teach myself Angular2 and facing some difficulties along the way. I am working on creating a service that loads a static JSON file. Right now, I am using i18n files as they are structured in JSON format. The service will include a ...

What are the steps necessary to "release" a proprietary Typescript npm package on git?

It seems like a common issue, but I haven't been able to find a solution anywhere... I have two projects written in TypeScript - LibraryA and WebserverB. They are stored in separate git repositories and are set as private projects to keep them from b ...

When making an Ajax post request, the loading indicator may not appear

I've implemented the jQuery code below for an autocomplete input field, but I'd like to have a spinner display while waiting for the response from the server. In my code, I'm using search: to show the loading div and open: to hide it. The s ...

What other intentions am I forgetting?

Encountering an error regarding missing intents but unable to determine which intents are missing. Various attempts have been made to rectify the issue without success. What specific element is absent here? The following code snippet represents only a po ...

What other methods are available to verify null and assign a value?

Is there a more efficient approach for accomplishing this task? theTitle = responsesToUse[i]["Title"]; if(theTitle == null) theTitle = ""; ...

Angular does not always interpret the value returned from a Promise.all call

One of the challenges I'm facing is related to the structure of my controller: controller.things = ['a', 'b', 'c']; controller.loading = true; controller.responses = []; controller.handlePromises = function(){ var pr ...

Leveraging nodemailer for automated email delivery

Hey there! I'm looking for some advice on how to set up scheduling with emailing using nodemailer. Ideally, I'd love to be able to schedule emails to send every day at 9am within a web app using nodemailer. However, I'm not sure where to st ...

Error message received when calling a function within a Vue watcher states "function is not defined"

My goal is to trigger a function in a Vue component when a prop changes using a watcher: props: [ 'mediaUrl' ], watch: { mediaUrl: function() { this.attemptToLoadImage(); } }, medthods: { attemptToLoadImage: function() { console ...

A guide on downloading and hosting Next.js documentation on your local machine for offline access

Can someone guide me on how to download and host the Next.js documentation locally for offline reference? I attempted to clone the Next.js repo containing the docs but all the files in the docs folder are markdown files, which cannot create and serve a l ...

Receiving no communication from Express Router

Having trouble receiving a response from the server after making get/post requests. I've tried adjusting the order of functions in index.js without success. I also attempted to send a post request using Postman to localhost:8080/register, but the requ ...

What is the best way to keep a checkbox unchecked after clicking cancel?

I'm working on a bootbox script that triggers a customized alert. I need the checkbox that triggers the alert to be unchecked when the user clicks cancel. Here is the checkbox when it's checked <div class="checkbox"> <label> ...

React: Managing various browsers and browser tabs to prevent duplicate operations

On my webpage, I have a button labeled Submit that, when clicked, triggers an operation by calling an API. After the button is clicked, it should be disabled or changed to reflect that the operation has started. I am facing an issue where multiple browser ...