Ensuring consistency in property formatting strategies

I'm curious if there exists a way to specify the format that an interface property should adhere to. For instance:

interface User {
  age?: number,
  name: string,
  birthdate: string    // must be in 'YYYY-MM-DD' format
}

I came across decorators, but it seems they are only applicable to classes, not interfaces.

I am working on developing an API using node/express and require input validation. Therefore, I am looking into Celebrate, which can utilize joi type Schema for input validation. However, I prefer to define my Schema / view model using TypeScript instead... As you can see, I am trying to use an Interface to outline what the input of a specific endpoint should resemble:

  • age: number, can be omitted
  • name: string
  • birthdate: string in the "YYYY-MM-DD" format

Any assistance or advice would be greatly appreciated :)

Answer №1

Appreciate any suggestions or guidance :)

Remember, writing code for validation is essential and won't happen automatically.

There are two different approaches to consider:

External Validation Method

Utilize the validate(obj) => {errors?} function. This involves creating a validate function that checks for errors in an object. You can easily create this type of function on your own.

Internal Validation Method

Rather than using {birthdate:string}, implement something like

{birthdate:FieldState<string>}
. Here, the FieldState manages validations and errors for a specific field. While this approach is used by , you have the flexibility to develop a similar system yourself.

Regarding Validators

I prefer simple validators like (value) => error? (value to optional error) as they are versatile and can be utilized across various frameworks. Formstate also utilizes this validator. Feel free to explore and find what works best for your individual requirements 🌹

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

Testing the MatDialog Component

Currently, I am in the process of creating a unit test for my confirmation modal that relies on MatDialog. The initial test I have set up is a simple one to ensure that the component is successfully created. Below is the code snippet from my spec file: im ...

I'm feeling a bit puzzled about how to read this stream properly

After going through the stream handbook, I found an interesting example: var Readable = require('stream').Readable; var rs = new Readable; rs.push('beep '); rs.push('boop\n'); rs.push(null); rs.pipe(process.stdout); T ...

employing express.json() post raw-body

Within my Express application, I am aiming to validate the length of the request body and restrict it irrespective of the content type. Additionally, I wish to parse the body only if the content type is JSON. How can I go about accomplishing this? Curren ...

Unable to execute the Vite project

I ran into an issue with my Vite project yesterday. I closed it and now that I have reopened it, the 'npm run dev' command is throwing an error. My project is built using Vite with React and TypeScript. Attached is a screenshot of the error mess ...

What could be the reason for passport.serializeUser function not being available?

After spending nearly an hour searching, I am still unable to pinpoint the issue in my code. Not only have I failed to find a solution online, but reaching out for help in coding communities on Discord has also been futile as no one seems to know why this ...

Leveraging multer for handling a FormData object in a node.js server

Having trouble with an HTML form that includes two buttons among other text input areas. The front-end javascript code is set up to handle the submit action by creating a FormData object to store the file and sending it via a jQuery AJAX request to a node. ...

Is there a problem with how the public directory is currently configured?

Recently, I completed a Webpack project and organized it in the following structure: static/ // Contains js and css files index.html // Main file I decided to integrate this setup into an Express environment by placing it inside the public/ folder. Here& ...

Mastering the security of sessions in express.js

I'm having trouble grasping some aspects of how express.js sessions function For instance, when I use the following code: server.use express.cookieParser() server.use express.cookieSession { secret: 'whatever' } and set up the login proce ...

What is the proper way to invoke express-validator within a middleware function?

I am facing a challenge in invoking the express-validator function from a middleware function. Although I can see that the execution is happening within the express-validator, validation does not seem to occur. The code snippet is provided below: router.g ...

Loading Angular2 components dynamically from a module is a powerful feature that allows for greater

Within my Angular application, I have the following setup: export class MyComponent { subcompPath = "path-to-subcomp#SubcompClassName"; @ViewChild("placeholder", { read: ViewComponentRef }) placeholderRef: ViewComponentRef; /* Constructor where C ...

Which method is more appropriate for my request - GET or POST? Or should I consider

Recently, I've been exploring the world of get and post methods and could use some guidance! Within my App.js file, there is a user text input field and a submit button. I have a couple of tasks in mind for handling this information: Retrieve a str ...

Tips for fixing the TypeError related to hasOwnProperty in your index.tsx file

I need assistance setting up a basic frame for my TypeScript project as I am having trouble compiling it. Any guidance would be greatly appreciated. The error I am encountering is: in ./app/index.tsx Module build failed: TypeError: Cannot convert undefin ...

ReactJS, ExpressJS, Node.js, and Python - the powerful

After successfully testing python integration with Node on an express server to process images, I decided to explore Multer for image uploads. In parallel, I started building a simple UI using create-react-app, which is nearly complete now. However, when ...

Step-by-step guide for setting up automatic Tslint in IntelliJ

When working on an Angular project in Intellij, I often encounter numerous tslint errors while coding. Is there a command within Intellij that can automatically fix all of these lint errors? ...

How do I specify a data type when using a function expression to declare a class?

const Foo: new () => unknown = class { constructor() {} bar(): string { return ‘Hello World!’; } }; const instance = new Foo(); I need to replace 'any' with 'unknown' below due to my configuration settings. new () =&g ...

Using jQuery's innerHTML method inside an EJS template

Can someone assist me in passing a dynamic value (ejs) from one page to another? I'm attempting to store the value in a variable and then console log it, but it's not working as expected. Below is the code snippet: <%- include('partials/h ...

What are the steps to confirm form submission with $pristine and $dirty in Angular?

I recently created a form using the resources available at https://github.com/nimbly/angular-formly and . While most of the validation is being handled by Angular, the user-friendliness of the form validation needs improvement. I am looking to implement va ...

What is preventing me from installing the "express" package?

After successfully installing Node.js, npm, and Express on my machine, I encountered an issue when trying to install Express in my project directory. lds-MacBook-Pro:contacts ldnwty$ npm install express npm WARN package.json <a href="/cdn-cgi/l/email-p ...

Dividing a JSON object into arrays containing keys and values within an Angular framework

I have a code snippet that involves receiving a JSON object of type Tenant from an API. I need to separate this object into keys and values within my function called tenantParser(). However, when I try to log displayedValues and displayedKeys, both show ...

Node.js Error: The requested URL cannot be found

I have encountered an issue in my Node project where I am getting a 'Cannot GET/' error when trying to open localhost on port 8081. I suspect that the problem lies in correctly reading the HTML file, but I'm not entirely sure. var express = ...