|
| 1 | +--- |
| 2 | +sidebar: false |
| 3 | +editLink: true |
| 4 | +outline: false |
| 5 | +prev: false |
| 6 | +next: false |
| 7 | +description: Learn how to validate nested objects in JavaScript using Robust Validator. A simple and practical guide for forms and API data. |
| 8 | +--- |
| 9 | + |
| 10 | +# How to Validate Nested Objects in JavaScript with Robust Validator |
| 11 | + |
| 12 | +## Intro |
| 13 | + |
| 14 | +When you're working with forms or APIs in JavaScript, you often deal with data that's nested. For example, a user profile might have contact info inside it, and each field needs to be validated. |
| 15 | + |
| 16 | +If you've ever tried to manually check `user.contact.email` or `settings.preferences.notifications`, you know how messy it can get. |
| 17 | + |
| 18 | +In this post, you'll learn how to validate nested objects easily using Robust Validator, a simple and powerful validation library inspired by Laravel. |
| 19 | + |
| 20 | +## Why Nested Validation Matters |
| 21 | + |
| 22 | +Most real-world apps deal with nested data. Things like: |
| 23 | + |
| 24 | +- User profiles |
| 25 | +- Signup forms |
| 26 | +- JSON payloads for APIs |
| 27 | + |
| 28 | +If you skip validation or only check top-level fields, bugs and bad data will sneak in. That leads to broken features, support tickets, and angry users. Not fun. |
| 29 | + |
| 30 | +## Step 1: Install Robust Validator |
| 31 | + |
| 32 | +First, install the package: |
| 33 | + |
| 34 | +```bash |
| 35 | +npm install --save robust-validator |
| 36 | +``` |
| 37 | + |
| 38 | +Then import the validator like this: |
| 39 | + |
| 40 | +```ts |
| 41 | +import { validate } from "robust-validator"; |
| 42 | +``` |
| 43 | + |
| 44 | +## Step 2: Example Data |
| 45 | + |
| 46 | +Here's a sample data object you might get from a form or API: |
| 47 | + |
| 48 | +```ts |
| 49 | +const data = { |
| 50 | + user: { |
| 51 | + name: "Alice", |
| 52 | + contact: { |
| 53 | + email: "alice@example.com", |
| 54 | + phone: "", |
| 55 | + }, |
| 56 | + }, |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +Now let’s say we want to check: |
| 61 | + |
| 62 | +- `user.name` is required and should be at least 3 characters |
| 63 | +- `user.contact.email` is required and should be a valid email |
| 64 | +- `user.contact.phone` is required |
| 65 | + |
| 66 | +## Step 3: Write the Schema |
| 67 | + |
| 68 | +Robust Validator supports dot notation for nested fields. |
| 69 | + |
| 70 | +### Option 1: String-based rules |
| 71 | + |
| 72 | +```ts |
| 73 | +const schema = { |
| 74 | + "user.name": "required|min:3", |
| 75 | + "user.contact.email": "required|email", |
| 76 | + "user.contact.phone": "required", |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +### Option 2: Function-based rules |
| 81 | + |
| 82 | +```ts |
| 83 | +import { required, min, email } from "robust-validator"; |
| 84 | + |
| 85 | +const schema = { |
| 86 | + "user.name": [required(), min(3)], |
| 87 | + "user.contact.email": [required(), email()], |
| 88 | + "user.contact.phone": [required()], |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +Both ways work the same. Pick the one you like better. |
| 93 | + |
| 94 | +## Step 4: Run the Validation |
| 95 | + |
| 96 | +Here’s how you validate the data: |
| 97 | + |
| 98 | +```ts |
| 99 | +const result = validate(data, schema); |
| 100 | + |
| 101 | +if (!result.passes()) { |
| 102 | + console.log(result.errors()); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Example Output |
| 107 | + |
| 108 | +If `user.contact.phone` is empty, you’ll get this: |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "isValid": false, |
| 113 | + "isInvalid": true, |
| 114 | + "fields": { |
| 115 | + "user.name": true, |
| 116 | + "user.contact.email": true, |
| 117 | + "user.contact.phone": false |
| 118 | + }, |
| 119 | + "errors": { |
| 120 | + "user.contact.phone": [ |
| 121 | + { "rule": "required", "message": "The field is required." } |
| 122 | + ] |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Now you can show this message to the user or send it back in an API response. |
| 128 | + |
| 129 | +## When to Use This |
| 130 | + |
| 131 | +Use this approach anytime you’re validating: |
| 132 | + |
| 133 | +- Form inputs with nested data |
| 134 | +- API request bodies |
| 135 | +- Config or settings objects |
| 136 | + |
| 137 | +Basically, anytime your object isn’t flat. |
| 138 | + |
| 139 | +## Wrap-Up |
| 140 | + |
| 141 | +Nested object validation doesn’t have to be complicated. With [Robust Validator](https://validator.axe-api.com), you can write clear, simple rules using dot notation. It handles the hard parts so you can focus on building your app. |
| 142 | + |
| 143 | +Give it a try and make your validation cleaner and more reliable. |
| 144 | + |
| 145 | +## Related Links |
| 146 | + |
| 147 | +- [Getting Started](https://validator.axe-api.com/getting-started) |
| 148 | + |
| 149 | +- [All Validation Rules](https://validator.axe-api.com/rules) |
| 150 | + |
| 151 | +- [Customization](https://validator.axe-api.com/customization) |
0 commit comments