• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

juancastillo0 / json_form / 11788932365

12 Nov 2024 01:21AM UTC coverage: 80.651% (+6.3%) from 74.365%
11788932365

push

github

juancastillo0
Add more README docs

1659 of 2057 relevant lines covered (80.65%)

1.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/lib/src/utils/string_validation.dart
1
import 'package:json_form/src/models/property_schema.dart';
2
import 'package:validators/validators.dart' as validators;
3

4
enum StringValidationError {
5
  minLength,
6
  maxLength,
7
  noMatchForPattern,
8
  format,
9
}
10

11
List<StringValidationError> validateJsonSchemaString({
1✔
12
  required String newValue,
13
  required SchemaProperty property,
14
}) {
15
  final errors = <StringValidationError>[];
1✔
16
  final maxLength = property.maxLength;
1✔
17
  if (newValue.length < (property.minLength ?? 0)) {
3✔
18
    errors.add(StringValidationError.minLength);
1✔
19
  }
20
  if (maxLength != null && newValue.length > maxLength) {
2✔
21
    errors.add(StringValidationError.maxLength);
×
22
  }
23
  if (property.pattern != null &&
1✔
24
      !validators.matches(newValue, property.pattern)) {
2✔
25
    errors.add(StringValidationError.noMatchForPattern);
×
26
  }
27
  if (!isValidFormat(property.format, newValue)) {
2✔
28
    errors.add(StringValidationError.format);
1✔
29
  }
30
  return errors;
31
}
32

33
bool isValidFormat(PropertyFormat format, String value) {
1✔
34
  switch (format) {
35
    case PropertyFormat.email:
1✔
36
    case PropertyFormat.idnEmail:
1✔
37
      return validators.isEmail(value);
1✔
38
    case PropertyFormat.time:
1✔
39
      return RegExp(r'^[0-9]{2}:[0-9]{2}(:[0-9]{2})?$').hasMatch(value);
2✔
40
    case PropertyFormat.uuid:
1✔
41
      return validators.isUUID(value);
1✔
42
    case PropertyFormat.regex:
1✔
43
      try {
44
        RegExp(value);
1✔
45
        return true;
46
      } catch (_) {
47
        return false;
48
      }
49
    case PropertyFormat.ipv4:
1✔
50
    case PropertyFormat.ipv6:
1✔
51
      return validators.isIP(
1✔
52
        value,
53
        format == PropertyFormat.ipv4 ? '4' : '6',
1✔
54
      );
55
    case PropertyFormat.hostname:
1✔
56
    case PropertyFormat.idnHostname:
1✔
57
    case PropertyFormat.uriTemplate:
1✔
58
    case PropertyFormat.uri:
1✔
59
    case PropertyFormat.uriReference:
1✔
60
    case PropertyFormat.iri:
1✔
61
    case PropertyFormat.iriReference:
1✔
62
      return validators.isURL(value, requireTld: false);
1✔
63
    case PropertyFormat.dataUrl:
1✔
64
      return validators.isURL(
×
65
        value,
66
        protocols: const ['data'],
67
        requireProtocol: true,
68
      );
69
    // TODO:
70
    case PropertyFormat.date:
1✔
71
    case PropertyFormat.dateTime:
1✔
72
    case PropertyFormat.jsonPointer:
1✔
73
    case PropertyFormat.relativeJsonPointer:
1✔
74
    case PropertyFormat.general:
1✔
75
      return true;
76
  }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc