ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
validation.cpp
Go to the documentation of this file.
1#include "validation.hpp"
2
3String validateUInt(const String& value, const String& name) {
4 if (value.length() == 0) {
5 return name + " cannot be empty";
6 }
7 for (unsigned int i = 0; i < value.length(); i++) {
8 if (!isdigit(value.charAt(i))) {
9 return name + " must be a positive integer";
10 }
11 }
12 return "";
13}
14
15String validateFloat(const String& value, const String& name) {
16 if (value.length() == 0) {
17 return name + " cannot be empty";
18 }
19
20 bool decimal_point = false;
21 bool exponent = false;
22 bool digit_seen = false;
23 bool digit_after_exp = true;
24
25 for (unsigned int i = 0; i < value.length(); i++) {
26 char c = value.charAt(i);
27
28 // Allow leading sign
29 if (i == 0 && (c == '-' || c == '+')) continue;
30
31 // Handle digits
32 if (isdigit(c)) {
33 digit_seen = true;
34 if (exponent) digit_after_exp = true;
35 continue;
36 }
37
38 // Handle decimal point
39 if (c == '.' && !decimal_point && !exponent) {
40 decimal_point = true;
41 continue;
42 }
43
44 // Handle exponent (e/E)
45 if ((c == 'e' || c == 'E') && !exponent && digit_seen) {
46 exponent = true;
47 digit_after_exp = false;
48
49 // Allow sign right after exponent
50 if (i + 1 < value.length() &&
51 (value.charAt(i + 1) == '+' || value.charAt(i + 1) == '-')) {
52 i++;
53 }
54 continue;
55 }
56
57 // If we get here, invalid character
58 return name + " must be a valid number";
59 }
60
61 // Must have at least one digit, and if exponent present, digits after it
62 if (!digit_seen || !digit_after_exp) {
63 return name + " must be a valid number";
64 }
65
66 return "";
67}
68
69String validateURL(const String& value) {
70 if (value.length() == 0) {
71 return "API Endpoint URL cannot be empty";
72 }
73 if (!value.startsWith("http://") && !value.startsWith("https://")) {
74 return "API Endpoint URL must start with http:// or https://";
75 }
76 return "";
77}
uint8_t i
String validateFloat(const String &value, const String &name)
Validate a string as a floating-point number.
String validateUInt(const String &value, const String &name)
Validate a string as an unsigned integer.
Definition validation.cpp:3
String validateURL(const String &value)
Validate a string as a URL.