26-Nodejs Course 2023: Validation Part IV: Base Rule
Hasan Zohdy
Posted on November 2, 2022
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;
}
}
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`;
}
}
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`;
}
}
If you notice i modified the
validate
method to ensure that the value is not a number using theIs.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
- Event Driven Architecture: A Practical Guide in Javascript
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
Packages & Libraries
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
React Js Packages
Courses (Articles)
Posted on November 2, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.