File tree Expand file tree Collapse file tree 2 files changed +89
-2
lines changed Expand file tree Collapse file tree 2 files changed +89
-2
lines changed Original file line number Diff line number Diff line change @@ -89,3 +89,90 @@ By the example, you would get the following response:
89
89
}
90
90
}
91
91
```
92
+
93
+ ## Nested data validation
94
+
95
+ This feature allows dynamic traversal of nested data structures, supporting complex validation rules for paths like ` users.*.addresses.*.city ` .
96
+
97
+ It is inspired by Laravel's validation system and works seamlessly with arrays and objects, including deeply nested data.
98
+
99
+ ``` ts
100
+ import { validate , setLocales , en } from " robust-validator" ;
101
+
102
+ setLocales (en );
103
+
104
+ const data = {
105
+ secret: " some secret" ,
106
+ users: [
107
+ {
108
+ addresses: [
109
+ {
110
+ city: " New York" ,
111
+ },
112
+ {
113
+ city: " Istanbul" ,
114
+ },
115
+ ],
116
+ },
117
+ {
118
+ addresses: [
119
+ {
120
+ city: " New York" ,
121
+ },
122
+ {
123
+ street: " Wall Street" ,
124
+ },
125
+ ],
126
+ },
127
+ ],
128
+ permissons: {
129
+ read: true ,
130
+ write: true ,
131
+ },
132
+ };
133
+
134
+ const definition = {
135
+ secret: " required|min:100" ,
136
+ " users.*.addresses.*.city" : " required" ,
137
+ " permissons.read" : " required|boolean" ,
138
+ " permissons.delete" : " required|boolean" ,
139
+ };
140
+
141
+ const result = await validate (data , definition );
142
+ console .log (result );
143
+ ```
144
+
145
+ And this is the content of the ` result ` variable:
146
+
147
+ ``` json
148
+ {
149
+ "isValid" : false ,
150
+ "isInvalid" : true ,
151
+ "fields" : {
152
+ "secret" : false ,
153
+ "users.*.addresses.*.city" : false ,
154
+ "permissons.read" : true ,
155
+ "permissons.delete" : false
156
+ },
157
+ "errors" : {
158
+ "secret" : [
159
+ {
160
+ "rule" : " min" ,
161
+ "message" : " The field must be at least 100."
162
+ }
163
+ ],
164
+ "users.1.addresses.1.city" : [
165
+ {
166
+ "rule" : " required" ,
167
+ "message" : " The field is required."
168
+ }
169
+ ],
170
+ "permissons.delete" : [
171
+ {
172
+ "rule" : " required" ,
173
+ "message" : " The field is required."
174
+ }
175
+ ]
176
+ }
177
+ }
178
+ ```
Original file line number Diff line number Diff line change @@ -15,9 +15,9 @@ features:
15
15
- title : Declarative ✍🏽
16
16
details : Declarative rule definition allows you to save your rules in different places such as configuration files, databases, etc.
17
17
- title : Simple 🐤
18
- details : Starting to validate data is very fast instead of creating complicated validation rules. You just need seconds.
18
+ details : Starting to validate data is very fast. Instead of creating complicated validation rules, you just need seconds.
19
19
- title : Proof of work 💪
20
20
details : Laravel-ish data validation rules are well-tested as a concept. This library is just another implementation for JavaScript.
21
21
- title : i18n 🇺🇳
22
- details : Multi-language error messages are supported internally, unlike other libraries. It provides consistency.
22
+ details : Multi-language error messages are supported internally, unlike other libraries.
23
23
---
You can’t perform that action at this time.
0 commit comments