|
| 1 | +--- |
| 2 | +sidebar: false |
| 3 | +editLink: true |
| 4 | +outline: false |
| 5 | +prev: false |
| 6 | +next: false |
| 7 | +description: Learn how to create your own custom validation rules in JavaScript using Robust Validator. A simple step-by-step guide with examples. |
| 8 | +--- |
| 9 | + |
| 10 | +# How to Build a Custom Validation Rule in JavaScript with Robust Validator |
| 11 | + |
| 12 | +## Intro |
| 13 | + |
| 14 | +Sometimes the built-in validation rules are not enough. You might need to check something unique to your app. For example, maybe a username must start with an uppercase letter. Or maybe a field needs to match a very specific format. |
| 15 | + |
| 16 | +Good news is that with [Robust Validator](https://validator.axe-api.com), you can easily create your own validation rules and use them just like the default ones. |
| 17 | + |
| 18 | +In this post, you’ll learn how to write a custom rule from scratch and plug it into your validation flow. |
| 19 | + |
| 20 | +## What We’ll Build |
| 21 | + |
| 22 | +Let’s build a custom rule called `startsWithUppercase`. It will check if a string starts with a capital letter. |
| 23 | + |
| 24 | +This rule is not included by default, but it’s easy to add your own. |
| 25 | + |
| 26 | +## Step 1: Define the Rule Function |
| 27 | + |
| 28 | +Your rule function is where you write the logic. It receives the value and returns true or false. |
| 29 | + |
| 30 | +```ts |
| 31 | +const ruleFunction = (value) => { |
| 32 | + if (typeof value !== "string") return false; |
| 33 | + return /^[A-Z]/.test(value); |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +This checks if the first character of the value is an uppercase letter. |
| 38 | + |
| 39 | +## Step 2: Register the Rule |
| 40 | + |
| 41 | +Use the `register()` function to add your rule to the validator. You also provide a custom error message for each language. |
| 42 | + |
| 43 | +```ts |
| 44 | +import { register } from "robust-validator"; |
| 45 | + |
| 46 | +register("startsWithUppercase", ruleFunction, { |
| 47 | + en: "The field must start with an uppercase letter.", |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +## Step 3: Write the Validation Schema |
| 52 | + |
| 53 | +Now you can use your custom rule in a string-based schema, just like a built-in rule. |
| 54 | + |
| 55 | +```ts |
| 56 | +import { validate } from "robust-validator"; |
| 57 | + |
| 58 | +const data = { |
| 59 | + name: "john", |
| 60 | +}; |
| 61 | + |
| 62 | +const schema = { |
| 63 | + name: "required|startsWithUppercase", |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +## Step 4: Run the Validation |
| 68 | + |
| 69 | +The `validate()` function is asynchronous, so make sure to await it. |
| 70 | + |
| 71 | +```ts |
| 72 | +const result = await validate(data, schema); |
| 73 | + |
| 74 | +console.log(result); |
| 75 | +``` |
| 76 | + |
| 77 | +## Example Output |
| 78 | + |
| 79 | +If the name does not start with a capital letter, you’ll get this: |
| 80 | + |
| 81 | +```json |
| 82 | +{ |
| 83 | + "isValid": false, |
| 84 | + "isInvalid": true, |
| 85 | + "fields": { |
| 86 | + "name": false |
| 87 | + }, |
| 88 | + "errors": { |
| 89 | + "name": [ |
| 90 | + { |
| 91 | + "rule": "startsWithUppercase", |
| 92 | + "message": "The field must start with an uppercase letter." |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Use Case: Exists Rule for Database |
| 100 | + |
| 101 | +You can also create rules that connect to external sources like a database. For example: |
| 102 | + |
| 103 | +```ts |
| 104 | +const existsRule = async (value: any, tableName: string) => { |
| 105 | + const fakeDatabase = { |
| 106 | + users: ["user@example.com", "admin@example.com"], |
| 107 | + }; |
| 108 | + |
| 109 | + return fakeDatabase[tableName]?.includes(value); |
| 110 | +}; |
| 111 | + |
| 112 | +register("exists", existsRule, { |
| 113 | + en: "The record doesn't exist in {0}.", |
| 114 | +}); |
| 115 | + |
| 116 | +const result = await validate( |
| 117 | + { email: "notfound@example.com" }, |
| 118 | + { email: "required|exists:users" }, |
| 119 | +); |
| 120 | +``` |
| 121 | + |
| 122 | +This checks if the email exists in a fake "users" table. |
| 123 | + |
| 124 | +## Why Custom Rules Are Useful |
| 125 | + |
| 126 | +Custom rules help when: |
| 127 | + |
| 128 | +- You need app-specific logic |
| 129 | +- You want to reuse complex checks |
| 130 | +- You need to integrate with your database or API |
| 131 | +- Built-in rules are not enough for your case |
| 132 | + |
| 133 | +## Wrap-Up |
| 134 | + |
| 135 | +Robust Validator makes it super easy to create and use your own validation rules. All you need is a rule function, a call to `register()`, and a schema. You can also customize the error messages for any language you need. |
| 136 | + |
| 137 | +Try creating your own rule today and make your validation logic match your app’s exact needs. |
| 138 | + |
| 139 | +## Related Links |
| 140 | + |
| 141 | +- [Getting Started](https://validator.axe-api.com/getting-started) |
| 142 | +- [All Validation Rules](https://validator.axe-api.com/rules) |
| 143 | +- [Customization](https://validator.axe-api.com/customization) |
0 commit comments