deser-validate 0.10.1

Validation for deser
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! A value together with the errors it had.
use std::borrow::Cow;
use std::fmt;
use std::marker::PhantomData;

use deser_core::de::{Deserialize, OwnedSink, Sink, SinkHandle};
use deser_core::ser::{Describe, Emit, Serialize};
use deser_core::{Atom, ContainerShape, Error, ErrorKind, State};
use deser_location::Locations;

use crate::Validator;
use crate::report::ReportHandle;

/// A value that might be invalid, together with its errors.
///
/// Deserializing a `Validated` never fails: the errors of the value are
/// kept in it instead of failing the deserialization of the value it's in.
/// That covers the errors of the value itself (for instance a string where
/// a number is expected), of the values nested in it and the violation of
/// the validator `V` (none by default).  Within the value, all errors are
/// collected (see [`State::set_collect_errors`]): the error holds all
/// problems of the value, not just the first one.  Only the errors of the
/// data format and of layers (for instance
/// [`Limits`](deser_core::de::Limits)) still fail the deserialization, and
/// the error that exceeds the limit of errors (see
/// [`State::set_max_errors`]).
///
/// ```
/// use deser::Deserialize;
/// use deser_validate::{Email, Validated};
///
/// #[derive(Deserialize)]
/// struct Signup {
///     email: Validated<String, Email>,
///     age: Validated<u8>,
/// }
///
/// let signup: Signup =
///     deser_json::from_str(r#"{"email": "nope", "age": "x"}"#).unwrap();
/// assert!(!signup.email.is_valid());
/// assert_eq!(signup.email.unchecked_value().unwrap(), "nope");
/// assert_eq!(
///     signup.email.error().unwrap().to_string(),
///     "InvalidValue: invalid value: must be an email address at offset 10"
/// );
/// assert_eq!(signup.age.value(), None);
/// ```
///
/// The errors have the context of the value attached, like errors that
/// are returned (for instance the path with a
/// [`PathLayer`](deser_path::PathLayer)).  If the format provides the
/// source (see [`deser_location`]), their lines and columns are resolved.
/// If a [`Validation`](crate::Validation) runs, they are reported to it
/// too.
///
/// While an untagged enum tries its variants, errors are not kept: a
/// variant with an invalid `Validated` value does not match.
///
/// ```
/// use deser::Deserialize;
/// use deser_validate::Validated;
///
/// #[derive(Deserialize)]
/// struct Address {
///     street: String,
///     zip: u32,
/// }
///
/// #[derive(Deserialize)]
/// struct Order {
///     shipping: Validated<Address>,
/// }
///
/// let order: Order =
///     deser_json::from_str(r#"{"shipping": {"zip": "x"}}"#).unwrap();
/// let err = order.shipping.error().unwrap();
/// let errors: Vec<_> = err.errors().map(|err| err.message()).collect();
/// assert_eq!(
///     errors,
///     ["unexpected string, expected u32", "missing field `street`"]
/// );
/// ```
///
/// When serialized, the value is serialized (also if it's invalid), a
/// value that could not be deserialized is serialized as null.
pub struct Validated<T, V = ()> {
    value: Option<T>,
    error: Option<Error>,
    _validator: PhantomData<fn() -> V>,
}

impl<T, V: Validator<T>> Validated<T, V> {
    /// Validates a value.
    pub fn new(value: T) -> Validated<T, V> {
        let error = V::validate(&value).err().map(|x| x.into_error());
        Validated {
            value: Some(value),
            error,
            _validator: PhantomData,
        }
    }
}

impl<T, V> Validated<T, V> {
    /// Creates an invalid value from an error.
    pub fn from_error(error: Error) -> Validated<T, V> {
        Validated {
            value: None,
            error: Some(error),
            _validator: PhantomData,
        }
    }

    /// Returns `true` if the value is valid.
    pub fn is_valid(&self) -> bool {
        self.error.is_none()
    }

    /// Returns the value if it's valid.
    pub fn value(&self) -> Option<&T> {
        match self.error {
            None => self.value.as_ref(),
            Some(_) => None,
        }
    }

    /// Returns the value, also if the validator rejected it.
    ///
    /// Returns `None` if the value could not be deserialized.
    pub fn unchecked_value(&self) -> Option<&T> {
        self.value.as_ref()
    }

    /// Returns the error if the value is invalid.
    ///
    /// The error holds all errors of the value (see [`Error::errors`]).
    pub fn error(&self) -> Option<&Error> {
        self.error.as_ref()
    }

    /// Returns the value if it's valid and the error otherwise.
    pub fn into_result(self) -> Result<T, Error> {
        match (self.value, self.error) {
            (Some(value), None) => Ok(value),
            (_, Some(err)) => Err(err),
            (None, None) => Err(Error::new(ErrorKind::InvalidState, "missing value")),
        }
    }
}

impl<T: fmt::Debug, V> fmt::Debug for Validated<T, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.error {
            None => fmt::Debug::fmt(&self.value, f),
            Some(ref err) => f
                .debug_struct("Invalid")
                .field("value", &self.value)
                .field("error", &format_args!("{:#}", err))
                .finish(),
        }
    }
}

impl<'de, T: Deserialize<'de>, V: Validator<T>> Deserialize<'de> for Validated<T, V> {
    fn deserialize_into<'out>(
        out: &'out mut Option<Self>,
        state: &mut State,
    ) -> SinkHandle<'out, 'de> {
        SinkHandle::arena(
            ValidatedSink {
                out,
                sink: Some(OwnedSink::deserialize(state)),
                error: None,
                start: None,
                outer: None,
            },
            state,
        )
    }

    fn expecting() -> Cow<'static, str> {
        T::expecting()
    }

    fn describe_type(d: &mut dyn Describe) {
        T::describe_type(d)
    }

    /// Missing values are the missing values of `T` (validated).
    fn initial_value() -> Option<Self> {
        T::initial_value().map(Validated::new)
    }
}

struct ValidatedSink<'a, 'de, T, V> {
    out: &'a mut Option<Validated<T, V>>,
    // `None` once the value failed, the rest of it is ignored then
    sink: Option<OwnedSink<'de, T>>,
    error: Option<Error>,
    // the start of the value in the input
    start: Option<usize>,
    // if errors are collected outside of the value while it's deserialized
    outer: Option<bool>,
}

impl<'a, 'de, T, V> ValidatedSink<'a, 'de, T, V> {
    /// Returns the sink of the value unless it failed.
    fn sink(&mut self) -> Option<&mut (dyn Sink<'de> + '_)> {
        self.sink.as_mut().map(|sink| sink.get_mut())
    }

    /// Returns the sink of the value at its start.
    ///
    /// The errors in the value are collected.
    fn begin(&mut self, state: &mut State) -> Option<&mut (dyn Sink<'de> + '_)> {
        if self.start.is_none() {
            self.start = state.input_range().map(|range| range.start);
        }
        if self.outer.is_none() {
            self.outer = Some(state.collect_errors());
            state.set_collect_errors(true);
        }
        self.sink()
    }

    /// Restores whether errors are collected once the value is complete or
    /// failed.
    fn end(&mut self, state: &mut State) {
        if let Some(outer) = self.outer.take() {
            state.set_collect_errors(outer);
        }
    }

    /// Keeps the error of the value.
    ///
    /// The rest of the value is ignored.  While errors are discarded and
    /// once the limit of errors is reached, the error is returned instead.
    fn fail(&mut self, mut err: Error, state: &mut State) -> Result<(), Error> {
        if state.discards_errors() || state.error_limit_reached() {
            self.end(state);
            return Err(err);
        }
        self.sink = None;
        state.attach_error_context(&mut err);
        self.error = Some(err);
        Ok(())
    }

    /// Keeps the error of an operation on the value if it failed.
    fn check(&mut self, rv: Result<(), Error>, state: &mut State) -> Result<(), Error> {
        match rv {
            Ok(()) => Ok(()),
            Err(err) => self.fail(err, state),
        }
    }
}

impl<'a, 'de, T: Send, V: Validator<T>> Sink<'de> for ValidatedSink<'a, 'de, T, V> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        let rv = match self.begin(state) {
            Some(sink) => sink.atom(atom, state),
            None => Ok(()),
        };
        self.check(rv, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        let rv = match self.begin(state) {
            Some(sink) => sink.borrowed_atom(atom, state),
            None => Ok(()),
        };
        self.check(rv, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        let rv = match self.begin(state) {
            Some(sink) => sink.map(state),
            None => Ok(()),
        };
        self.check(rv, state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        let rv = match self.begin(state) {
            Some(sink) => sink.seq(state),
            None => Ok(()),
        };
        self.check(rv, state)
    }

    // The errors of the items are handled in `recover`.

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match self.sink() {
            Some(sink) => sink.next_key(state),
            None => Ok(SinkHandle::null()),
        }
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match self.sink() {
            Some(sink) => sink.next_value(state),
            None => Ok(SinkHandle::null()),
        }
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match self.sink() {
            Some(sink) => sink.__private_key_atom(atom, state),
            None => Ok(()),
        }
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match self.sink() {
            Some(sink) => sink.__private_value_atom(atom, state),
            None => Ok(()),
        }
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match self.sink() {
            Some(sink) => sink.__private_borrowed_key_atom(atom, state),
            None => Ok(()),
        }
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match self.sink() {
            Some(sink) => sink.__private_borrowed_value_atom(atom, state),
            None => Ok(()),
        }
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        match self.sink() {
            Some(sink) => sink.value_for_key(key, state),
            None => Ok(None),
        }
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        // the value might recover itself (for instance if it collects
        // errors), otherwise it failed
        let rv = match self.sink() {
            Some(sink) => sink.recover(err, state),
            None => Ok(()),
        };
        self.check(rv, state)
    }

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        let rv = match self.sink() {
            Some(sink) => sink.finish(state),
            None => Ok(()),
        };
        self.check(rv, state)?;
        self.end(state);
        let value = self.sink.as_mut().and_then(|sink| sink.take());
        if let Some(ref value) = value
            && let Err(violation) = V::validate(value)
        {
            // the value is kept, it's invalid
            let mut err = violation.into_error();
            if let Some(start) = self.start {
                err.set_offset(start);
            }
            if state.discards_errors() {
                return Err(err);
            }
            state.attach_error_context(&mut err);
            self.error = Some(err);
        }
        if value.is_none() && self.error.is_none() {
            return Ok(());
        }
        let error = self.error.take().map(|err| report_error(err, state));
        *self.out = Some(Validated {
            value,
            error,
            _validator: PhantomData,
        });
        Ok(())
    }

    fn expecting(&self) -> Cow<'_, str> {
        match self.sink {
            Some(ref sink) => sink.get().expecting(),
            None => Cow::Borrowed("compatible type"),
        }
    }
}

/// Resolves the position of an error that is kept and reports it.
fn report_error(mut err: Error, state: &mut State) -> Error {
    if let Some(source_map) = Locations::source_map(state) {
        err.resolve_position(source_map.source().as_bytes());
    }
    ReportHandle::report(&err, state);
    err
}

impl<T: Serialize, V> Serialize for Validated<T, V> {
    fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
        match this.value {
            Some(ref value) => T::serialize(value, state),
            None => Ok(Emit::Atom(Atom::Null)),
        }
    }

    fn finish(this: &Self, state: &mut State) -> Result<(), Error> {
        match this.value {
            Some(ref value) => T::finish(value, state),
            None => Ok(()),
        }
    }

    fn is_optional(this: &Self) -> bool {
        match this.value {
            Some(ref value) => T::is_optional(value),
            None => true,
        }
    }

    fn container_shape(this: &Self) -> ContainerShape {
        match this.value {
            Some(ref value) => T::container_shape(value),
            None => ContainerShape::new(),
        }
    }

    fn describe(this: &Self, d: &mut dyn Describe) {
        if let Some(ref value) = this.value {
            T::describe(value, d)
        }
    }
}