26-Nodejs Course 2023: Validation Part IV: Base Rule
Tags: node, typescript, mongodb, fastify
Published: 2022-11-02 08:12:11

---
title: 26-Nodejs Course 2023: Validation Part IV: Base Rule
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
#cover_image: 
# Use a ratio of 100:42 for best results.
---

We created two rules, the `required` and `string` rules which they both are very useful. But if we notice these rules classes contents are very similar. So we can create a base rule class which will contain the common logic and the other rules will extend from it.

## Base Rule

We will create a new file `rule.ts` in the `rules` folder. This file will contain the base rule class.

```ts
// src/core/validator/rules/rule.ts
export default abstract class Rule {
  /**
   * Rule name
   */
  public static ruleName = "rule";

  /**
   * Determine if rule is valid
   */
  protected isValid = true;

  /**
   * Constructor
   */
  public constructor(
    protected readonly input: string,
    protected readonly value: any,
  ) {
    //
  }

  /**
   * Validate the rule
   */
  public async validate() {
    //
  }

  /**
   * Determine if rule validation passes
   */
  public passes() {
    return this.isValid === true;
  }

  /**
   * Determine if rule validation fails
   */
  public fails() {
    return this.isValid === false;
  }
}
```

We duplicated the content of the `required` rule class but notice there is `abstract` keyword before the `Rule` class, this will ensure that the `Rule` class cannot be instantiated. Also, we added a static property `ruleName` which will be empty string that the child classes will override it.

Now let's update the `required` rule class to extend from the `Rule` class.

```ts
// src/core/validator/rules/required.ts
import Rule from "./rule";

export default class RequiredRule extends Rule {
  /**
   * Rule name
   */
  public static ruleName = "required";

  /**
   * Validate the rule
   */
  public async validate() {
    //
    this.isValid = Boolean(this.value) && this.value.length > 0;
  }

  /**
   * Get error message
   */
  public error() {
    return `${this.input} is required`;
  }
}
```

See now? the class is much cleaner and easier to maintain, let's do it also in our `string` rule class.

```ts
// src/core/validator/rules/string.ts
import Is from "@mongez/supportive-is";
import Rule from "./rule";

export default class StringRule extends Rule {
  /**
   * Rule name
   */
  public static ruleName = "string";

  /**
   * Validate the rule
   */
  public async validate() {
    //
    this.isValid = Is.string(this.value) && !Is.numeric(this.value);
  }

  /**
   * Get error message
   */
  public error() {
    return `${this.input} is not a string`;
  }
}
```

> If you notice i modified the `validate` method to ensure that the value is not a number using the `Is.numeric` method.

Now we can happily create any new rule freely without worrying about the common logic.

In our next article, we'll create the validator configuration file to manage these configurations.

## 🎨 Project Repository

You can find the latest updates of this project on [Github](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) to get help and support (Node Js 2023 Channel).

## 🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on [Youtube](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), the course is in Arabic language.

## 💰 Bonus Content 💰

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

- [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

- [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

- [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)