Line data Source code
1 : import 'package:at_client/at_client.dart';
2 : import 'package:at_commons/at_commons.dart';
3 :
4 : class AtClientValidation {
5 0 : static void validateKey(String? key) {
6 0 : if (key == null || key.isEmpty) {
7 0 : throw AtKeyException('Key cannot be null or empty');
8 : }
9 : // Key cannot contain @
10 0 : if (key.contains('@')) {
11 0 : throw AtKeyException('Key cannot contain @');
12 : }
13 : // Key cannot contain whitespaces
14 0 : if (key.contains(' ')) {
15 0 : throw AtKeyException('Key cannot contain whitespaces');
16 : }
17 : }
18 :
19 : /// Validates the metadata of the key.
20 : /// Throws [AtKeyException] if metadata has invalid values.
21 0 : static void validateMetadata(Metadata? metadata) {
22 : if (metadata == null) {
23 : return;
24 : }
25 : // validate TTL
26 0 : if (metadata.ttl != null && metadata.ttl! < 0) {
27 0 : throw AtKeyException(
28 0 : 'Invalid TTL value: ${metadata.ttl}. TTL value cannot be less than 0');
29 : }
30 : // validate TTB
31 0 : if (metadata.ttb != null && metadata.ttb! < 0) {
32 0 : throw AtKeyException(
33 0 : 'Invalid TTB value: ${metadata.ttb}. TTB value cannot be less than 0');
34 : }
35 : //validate TTR
36 0 : if (metadata.ttr != null && metadata.ttr! < -1) {
37 0 : throw AtKeyException(
38 0 : 'Invalid TTR value: ${metadata.ttr}. valid values for TTR are -1 and greater than or equal to 1');
39 : }
40 : }
41 :
42 : /// Verify if the atSign exists in root server.
43 : /// Throws [InvalidAtSignException] if atSign does not exist.
44 0 : static void isAtSignExists(
45 : String atSign, String rootDomain, int rootPort) async {
46 0 : if (atSign.isEmpty) {
47 0 : throw AtKeyException('@sign cannot be empty');
48 : }
49 : try {
50 0 : await AtClientUtil.findSecondary(atSign, rootDomain, rootPort);
51 0 : } on SecondaryNotFoundException {
52 0 : throw AtKeyException('$atSign does not exist');
53 : }
54 : }
55 : }
|