| Index: third_party/WebKit/Source/modules/payments/PaymentsValidators.cpp
|
| diff --git a/third_party/WebKit/Source/modules/payments/PaymentsValidators.cpp b/third_party/WebKit/Source/modules/payments/PaymentsValidators.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2d1d32c8f8c025dd2d468a69e4921446f3eb593d
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/payments/PaymentsValidators.cpp
|
| @@ -0,0 +1,175 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "modules/payments/PaymentsValidators.h"
|
| +
|
| +namespace blink {
|
| +
|
| +bool isValidCurrencyCodeFormat(const String& code, String* optionalErrorMessage)
|
| +{
|
| + if (code.length() != 3) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 4217 currency code, should be 3 letters";
|
| + return false;
|
| + }
|
| +
|
| + for (size_t i = 0; i < code.length(); ++i) {
|
| + if (code[i] < 'A' || code[i] > 'Z') {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 4217 currency code, should be upper case letters [A-Z]";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool isValidAmountFormat(const String& amount, String* optionalErrorMessage)
|
| +{
|
| + if (amount.isEmpty()) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should be nonempty";
|
| + return false;
|
| + }
|
| +
|
| + if (amount.length() > 32) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should be at most 32 characters";
|
| + return false;
|
| + }
|
| +
|
| + int numberOfPeriods = 0;
|
| + int numberOfDigits = 0;
|
| + int numberOfFractionDigits = 0;
|
| + for (size_t i = 0; i < amount.length(); ++i) {
|
| + if (i == 0 && amount[i] == '-')
|
| + continue;
|
| +
|
| + bool isPeriod = amount[i] == '.';
|
| + bool isDigit = amount[i] >= '0' && amount[i] <= '9';
|
| + if (!isPeriod && !isDigit) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should contain only '.' and [0-9] with optional prefix '-'";
|
| + return false;
|
| + }
|
| +
|
| + if (isPeriod) {
|
| + if (numberOfDigits == 0) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, '.' should follow at least one digit";
|
| + return false;
|
| + }
|
| + ++numberOfPeriods;
|
| + }
|
| +
|
| + if (isDigit) {
|
| + ++numberOfDigits;
|
| + if (numberOfPeriods > 0)
|
| + ++numberOfFractionDigits;
|
| + }
|
| + }
|
| +
|
| + if (numberOfPeriods > 1) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should contain at most one '.'";
|
| + return false;
|
| + }
|
| +
|
| + if (numberOfPeriods > 0 && numberOfFractionDigits == 0) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, at least one digit should follow '.'";
|
| + return false;
|
| + }
|
| +
|
| + if (numberOfDigits == 0) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should contain at least one digit";
|
| + return false;
|
| + }
|
| +
|
| + if (numberOfDigits > 30) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should contain at most 30 digits";
|
| + return false;
|
| + }
|
| +
|
| + if (numberOfFractionDigits > 10) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid ISO 200222 CurrencyAnd30Amount currency amount, should contain at most 10 fraction digits";
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool isValidRegionCodeFormat(const String& code, String* optionalErrorMessage)
|
| +{
|
| + if (code.length() != 2) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 3166 country code, should be 2 letters";
|
| + return false;
|
| + }
|
| +
|
| + for (size_t i = 0; i < code.length(); ++i) {
|
| + if (code[i] < 'A' || code[i] > 'Z') {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 3166 country code, should be upper case letters [A-Z]";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool isValidLanguageCodeFormat(const String& code, String* optionalErrorMessage)
|
| +{
|
| + if (code.isEmpty())
|
| + return true;
|
| +
|
| + if (code.length() != 2 && code.length() != 3) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 639 language code, should be 2-3 letters";
|
| + return false;
|
| + }
|
| +
|
| + for (size_t i = 0; i < code.length(); ++i) {
|
| + if (code[i] < 'a' || code[i] > 'z') {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 639 language code, should be lower case letters [a-z]";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool isValidScriptCodeFormat(const String& code, String* optionalErrorMessage)
|
| +{
|
| + if (code.isEmpty())
|
| + return true;
|
| +
|
| + if (code.length() != 4) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524 script code, should be 4 letters";
|
| + return false;
|
| + }
|
| +
|
| + if (code[0] < 'A' || code[0] > 'Z') {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524 script code, first letter should be upper case [A-Z]";
|
| + return false;
|
| + }
|
| +
|
| + for (size_t i = 1; i < code.length(); ++i) {
|
| + if (code[i] < 'a' || code[i] > 'z') {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code + "' is not a valid ISO 1524 script code, letters 2-4 should be lower case [a-z]";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +} // namespace blink
|
|
|