26-Nodejs Course 2023: Validation Part IV: Base Rule

hassanzohdy

Hasan Zohdy

Posted on November 2, 2022

26-Nodejs Course 2023: Validation Part IV: Base Rule

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.

// 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;
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

// 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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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

// 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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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

😍 Join our community

Join our community on Discord 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, 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

Packages & Libraries

React Js Packages

Courses (Articles)

💖 💪 🙅 🚩
hassanzohdy
Hasan Zohdy

Posted on November 2, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related