1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! # `vld_data::surrealdb` — SurrealDB helpers for `vld`
//!
//! Validate JSON documents against a [`vld`] schema **before** create /
//! insert / merge and **after** select ([`validate_content`],
//! [`validate_records`], [`validate_json`], [`validate_value`],
//! [`validate_fields!`]), and return a serializable
//! [`VldSurrealResponse`] to API clients.
//!
//! No dependency on the `surrealdb` crate: everything goes through `serde`,
//! so any SDK version works, and the module has no feature: it is always
//! compiled.
//!
//! ## Example
//!
//! ```rust
//! use vld_data::surrealdb::prelude::*;
//!
//! vld::schema! {
//! #[derive(Debug)]
//! pub struct PersonSchema {
//! pub name: String => vld::string().min(1).max(100),
//! pub email: String => vld::string().email(),
//! pub age: i64 => vld::number().int().min(0).max(150),
//! }
//! }
//!
//! #[derive(serde::Serialize)]
//! struct Person { name: String, email: String, age: i64 }
//!
//! let person = Person {
//! name: "Alice".into(),
//! email: "alice@example.com".into(),
//! age: 30,
//! };
//!
//! // Validate before db.create("person").content(person)
//! validate_content::<PersonSchema, _>(&person).unwrap();
//! ```
//!
//! Issue paths have no leading dot (`user.email`, `items[0]`).
//!
//! Guide: <https://s00d.github.io/vld/integrations/database/surrealdb>
pub use vld;
// ========================= Error type ========================================
crate__vld_db_error!;
/// A single field error (`field` is a clean path such as `user.email` or `items[0]`).
pub use FieldError;
/// Serializable validation error for API responses.
///
/// Converts from [`VldSurrealError`] and can be sent over the wire.
// ========================= Validated<S, T> ===================================
crate__vld_db_validated!;
// ========================= Standalone helpers ================================
/// Validate a value against schema `S` before create/insert/update.
///
/// Use before `db.create("table").content(value)` or `db.insert("table").content(value)`.
///
/// ```
/// use vld::prelude::*;
///
/// vld::schema! {
/// #[derive(Debug)]
/// pub struct ItemSchema {
/// pub name: String => vld::string().min(1),
/// pub qty: i64 => vld::number().int().min(0),
/// }
/// }
///
/// #[derive(serde::Serialize)]
/// struct Item { name: String, qty: i64 }
///
/// let item = Item { name: "Widget".into(), qty: 5 };
/// vld_data::surrealdb::validate_content::<ItemSchema, _>(&item).unwrap();
/// ```
/// Validate raw JSON value against schema **type** `S` (a `vld::schema!` /
/// `#[derive(Validate)]` type).
///
/// Useful for validating SurrealQL query results or raw JSON payloads. To check
/// a value against a schema **instance** (`vld::string().min(1)`), use
/// [`validate_value`].
///
/// ```
/// use vld::prelude::*;
///
/// vld::schema! {
/// #[derive(Debug)]
/// pub struct NameSchema {
/// pub name: String => vld::string().min(1),
/// }
/// }
///
/// let json = serde_json::json!({"name": "Alice"});
/// vld_data::surrealdb::validate_json::<NameSchema>(&json).unwrap();
/// ```
/// Validate a batch of records against schema `S`.
///
/// Returns the index and error of the first invalid record.
///
/// ```
/// use vld::prelude::*;
///
/// vld::schema! {
/// #[derive(Debug)]
/// pub struct NameSchema {
/// pub name: String => vld::string().min(1),
/// }
/// }
///
/// #[derive(serde::Serialize)]
/// struct Row { name: String }
///
/// let rows = vec![
/// Row { name: "Alice".into() },
/// Row { name: "Bob".into() },
/// ];
/// assert!(vld_data::surrealdb::validate_records::<NameSchema, _>(&rows).is_ok());
/// ```
/// Validate a single field value against a vld schema **instance**
/// (`vld::string().min(1)`, …); [`validate_json`] takes a schema **type**.
///
/// Useful for validating individual fields in SurrealDB `MERGE` operations.
///
/// ```
/// use vld::prelude::*;
///
/// let schema = vld::string().min(1).max(100);
/// let val = serde_json::json!("Bob");
/// assert!(vld_data::surrealdb::validate_value(&schema, &val).is_ok());
///
/// let bad = serde_json::json!("");
/// assert!(vld_data::surrealdb::validate_value(&schema, &bad).is_err());
/// ```
// ========================= Column wrappers ===================================
//
// `VldText`, `VldInt`, `VldFloat`, `VldBool`: validated on construction and on
// deserialization; the schema must have a field named `value`.
//
// ```
// vld::schema! {
// #[derive(Debug)]
// pub struct EmailField { pub value: String => vld::string().email() }
// }
// let email = vld_data::surrealdb::VldText::<EmailField>::new("user@example.com").unwrap();
// ```
crate__vld_db_wrappers!
// ========================= Macro =============================================
/// Validate multiple fields inline before a SurrealDB operation.
///
/// ```
/// use vld::prelude::*;
/// use vld_data::surrealdb::validate_fields;
///
/// let name = "Alice";
/// let email = "alice@example.com";
///
/// let result = validate_fields! {
/// name => vld::string().min(1).max(100),
/// email => vld::string().email(),
/// };
/// assert!(result.is_ok());
/// ```
///
/// Each field's real validation issues are returned, with the field name
/// prepended to their path. A field that cannot be serialized yields
/// [`VldSurrealError::Serialization`].
pub use crate__surrealdb_validate_fields as validate_fields;
// ========================= Prelude ===========================================
/// Common imports: the SurrealDB helpers plus `vld::prelude`.