-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodecs_example.dart
More file actions
36 lines (32 loc) · 1.24 KB
/
Copy pathcodecs_example.dart
File metadata and controls
36 lines (32 loc) · 1.24 KB
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
/// Codec examples for the Ack validation library.
///
/// Codecs decode boundary (wire) values into rich Dart runtime types and encode
/// them back. Built-in codecs cover common conversions; `Ack.codec(...)` builds
/// a custom bidirectional codec.
library;
import 'package:ack/ack.dart';
/// Priority levels, used with `Ack.enumCodec`.
enum Priority { low, medium, high }
/// An event whose fields use built-in codecs:
///
/// - `startsAt`: ISO 8601 string <-> UTC `DateTime` (`Ack.datetime`)
/// - `due`: `YYYY-MM-DD` string <-> local-midnight `DateTime` (`Ack.date`)
/// - `website`: string <-> `Uri` (`Ack.uri`)
/// - `timeout`: milliseconds `int` <-> `Duration` (`Ack.duration`)
/// - `priority`: enum-name string <-> `Priority` (`Ack.enumCodec`)
final eventSchema = Ack.object({
'name': Ack.string(),
'startsAt': Ack.datetime(),
'due': Ack.date(),
'website': Ack.uri(),
'timeout': Ack.duration(),
'priority': Ack.enumCodec(Priority.values),
});
/// A custom bidirectional codec: comma-separated string <-> `List<String>`.
///
/// `decode` runs on parse; `encode` runs on `schema.encode`.
final tagsCodec = Ack.codec<String, String, List<String>>(
input: Ack.string(),
decode: (value) => value.split(','),
encode: (tags) => tags.join(','),
);