OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/payments/PaymentsValidators.h" |
| 6 |
| 7 namespace blink { |
| 8 |
| 9 bool isValidCurrencyCodeFormat(const String& code, String* optionalErrorMessage) |
| 10 { |
| 11 if (code.length() != 3) { |
| 12 if (optionalErrorMessage) |
| 13 *optionalErrorMessage = "'" + code + "' is not a valid ISO 4217 curr
ency code, should be 3 letters"; |
| 14 return false; |
| 15 } |
| 16 |
| 17 for (size_t i = 0; i < code.length(); ++i) { |
| 18 if (code[i] < 'A' || code[i] > 'Z') { |
| 19 if (optionalErrorMessage) |
| 20 *optionalErrorMessage = "'" + code + "' is not a valid ISO 4217
currency code, should be upper case letters [A-Z]"; |
| 21 return false; |
| 22 } |
| 23 } |
| 24 |
| 25 return true; |
| 26 } |
| 27 |
| 28 bool isValidAmountFormat(const String& amount, String* optionalErrorMessage) |
| 29 { |
| 30 if (amount.isEmpty()) { |
| 31 if (optionalErrorMessage) |
| 32 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should be nonempty"; |
| 33 return false; |
| 34 } |
| 35 |
| 36 if (amount.length() > 32) { |
| 37 if (optionalErrorMessage) |
| 38 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should be at most 32 characters"; |
| 39 return false; |
| 40 } |
| 41 |
| 42 int numberOfPeriods = 0; |
| 43 int numberOfDigits = 0; |
| 44 int numberOfFractionDigits = 0; |
| 45 for (size_t i = 0; i < amount.length(); ++i) { |
| 46 if (i == 0 && amount[i] == '-') |
| 47 continue; |
| 48 |
| 49 bool isPeriod = amount[i] == '.'; |
| 50 bool isDigit = amount[i] >= '0' && amount[i] <= '9'; |
| 51 if (!isPeriod && !isDigit) { |
| 52 if (optionalErrorMessage) |
| 53 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200
222 CurrencyAnd30Amount currency amount, should contain only '.' and [0-9] with
optional prefix '-'"; |
| 54 return false; |
| 55 } |
| 56 |
| 57 if (isPeriod) { |
| 58 if (numberOfDigits == 0) { |
| 59 if (optionalErrorMessage) |
| 60 *optionalErrorMessage = "'" + amount + "' is not a valid ISO
200222 CurrencyAnd30Amount currency amount, '.' should follow at least one digi
t"; |
| 61 return false; |
| 62 } |
| 63 ++numberOfPeriods; |
| 64 } |
| 65 |
| 66 if (isDigit) { |
| 67 ++numberOfDigits; |
| 68 if (numberOfPeriods > 0) |
| 69 ++numberOfFractionDigits; |
| 70 } |
| 71 } |
| 72 |
| 73 if (numberOfPeriods > 1) { |
| 74 if (optionalErrorMessage) |
| 75 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should contain at most one '.'"; |
| 76 return false; |
| 77 } |
| 78 |
| 79 if (numberOfPeriods > 0 && numberOfFractionDigits == 0) { |
| 80 if (optionalErrorMessage) |
| 81 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, at least one digit should follow '.'"; |
| 82 return false; |
| 83 } |
| 84 |
| 85 if (numberOfDigits == 0) { |
| 86 if (optionalErrorMessage) |
| 87 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should contain at least one digit"; |
| 88 return false; |
| 89 } |
| 90 |
| 91 if (numberOfDigits > 30) { |
| 92 if (optionalErrorMessage) |
| 93 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should contain at most 30 digits"; |
| 94 return false; |
| 95 } |
| 96 |
| 97 if (numberOfFractionDigits > 10) { |
| 98 if (optionalErrorMessage) |
| 99 *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222
CurrencyAnd30Amount currency amount, should contain at most 10 fraction digits"; |
| 100 return false; |
| 101 } |
| 102 |
| 103 return true; |
| 104 } |
| 105 |
| 106 bool isValidRegionCodeFormat(const String& code, String* optionalErrorMessage) |
| 107 { |
| 108 if (code.length() != 2) { |
| 109 if (optionalErrorMessage) |
| 110 *optionalErrorMessage = "'" + code + "' is not a valid ISO 3166 coun
try code, should be 2 letters"; |
| 111 return false; |
| 112 } |
| 113 |
| 114 for (size_t i = 0; i < code.length(); ++i) { |
| 115 if (code[i] < 'A' || code[i] > 'Z') { |
| 116 if (optionalErrorMessage) |
| 117 *optionalErrorMessage = "'" + code + "' is not a valid ISO 3166
country code, should be upper case letters [A-Z]"; |
| 118 return false; |
| 119 } |
| 120 } |
| 121 |
| 122 return true; |
| 123 } |
| 124 |
| 125 bool isValidLanguageCodeFormat(const String& code, String* optionalErrorMessage) |
| 126 { |
| 127 if (code.isEmpty()) |
| 128 return true; |
| 129 |
| 130 if (code.length() != 2 && code.length() != 3) { |
| 131 if (optionalErrorMessage) |
| 132 *optionalErrorMessage = "'" + code + "' is not a valid ISO 639 langu
age code, should be 2-3 letters"; |
| 133 return false; |
| 134 } |
| 135 |
| 136 for (size_t i = 0; i < code.length(); ++i) { |
| 137 if (code[i] < 'a' || code[i] > 'z') { |
| 138 if (optionalErrorMessage) |
| 139 *optionalErrorMessage = "'" + code + "' is not a valid ISO 639 l
anguage code, should be lower case letters [a-z]"; |
| 140 return false; |
| 141 } |
| 142 } |
| 143 |
| 144 return true; |
| 145 } |
| 146 |
| 147 bool isValidScriptCodeFormat(const String& code, String* optionalErrorMessage) |
| 148 { |
| 149 if (code.isEmpty()) |
| 150 return true; |
| 151 |
| 152 if (code.length() != 4) { |
| 153 if (optionalErrorMessage) |
| 154 *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524 scri
pt code, should be 4 letters"; |
| 155 return false; |
| 156 } |
| 157 |
| 158 if (code[0] < 'A' || code[0] > 'Z') { |
| 159 if (optionalErrorMessage) |
| 160 *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524 scri
pt code, first letter should be upper case [A-Z]"; |
| 161 return false; |
| 162 } |
| 163 |
| 164 for (size_t i = 1; i < code.length(); ++i) { |
| 165 if (code[i] < 'a' || code[i] > 'z') { |
| 166 if (optionalErrorMessage) |
| 167 *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524
script code, letters 2-4 should be lower case [a-z]"; |
| 168 return false; |
| 169 } |
| 170 } |
| 171 |
| 172 return true; |
| 173 } |
| 174 |
| 175 } // namespace blink |
OLD | NEW |