| Index: components/payments/payments_validators.cc
|
| diff --git a/components/payments/payments_validators.cc b/components/payments/payments_validators.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..40f5e7a1fc8370cfcd6e9c75f95b4498e89445f3
|
| --- /dev/null
|
| +++ b/components/payments/payments_validators.cc
|
| @@ -0,0 +1,106 @@
|
| +// 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 "components/payments/payments_validators.h"
|
| +
|
| +namespace payments {
|
| +
|
| +// We limit the maximum length of the currency code to 2048 bytes for security
|
| +// reasons.
|
| +static const int maxCurrencyCodeLength = 2048;
|
| +
|
| +bool PaymentsValidators::isValidCurrencyCodeFormat(
|
| + const std::string& code,
|
| + std::string* optionalErrorMessage) {
|
| + if (code.size() <= maxCurrencyCodeLength)
|
| + return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage =
|
| + "The currency code should be at most 2048 characters long";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool PaymentsValidators::isValidAmountFormat(
|
| + const std::string& amount,
|
| + std::string* optionalErrorMessage) {
|
| + // if (ScriptRegexp("^-?[0-9]+(\\.[0-9]+)?$",
|
| + // TextCaseSensitive).match(amount) == 0)
|
| + // return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid amount format";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool PaymentsValidators::isValidCountryCodeFormat(
|
| + const std::string& code,
|
| + std::string* optionalErrorMessage) {
|
| + // if (ScriptRegexp("^[A-Z]{2}$", TextCaseSensitive).match(code) == 0)
|
| + // return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code +
|
| + "' is not a valid CLDR country code, should be 2 "
|
| + "upper case letters [A-Z]";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool PaymentsValidators::isValidLanguageCodeFormat(
|
| + const std::string& code,
|
| + std::string* optionalErrorMessage) {
|
| + // if (ScriptRegexp("^([a-z]{2,3})?$", TextCaseSensitive).match(code) == 0)
|
| + // return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code +
|
| + "' is not a valid BCP-47 language code, should be "
|
| + "2-3 lower case letters [a-z]";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool PaymentsValidators::isValidScriptCodeFormat(
|
| + const std::string& code,
|
| + std::string* optionalErrorMessage) {
|
| + // if (ScriptRegexp("^([A-Z][a-z]{3})?$", TextCaseSensitive).match(code) ==
|
| + // 0)
|
| + // return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code +
|
| + "' is not a valid ISO 15924 script code, should be "
|
| + "an upper case letter [A-Z] followed by 3 lower "
|
| + "case letters [a-z]";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool PaymentsValidators::isValidShippingAddress(
|
| + const blink::mojom::PaymentAddressPtr& address,
|
| + std::string* optionalErrorMessage) {
|
| + if (!isValidCountryCodeFormat(address->country, optionalErrorMessage))
|
| + return false;
|
| +
|
| + if (!isValidLanguageCodeFormat(address->language_code, optionalErrorMessage))
|
| + return false;
|
| +
|
| + if (!isValidScriptCodeFormat(address->script_code, optionalErrorMessage))
|
| + return false;
|
| +
|
| + if (!address->language_code.size() && address->script_code.size()) {
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage =
|
| + "If language code is empty, then script code should also be empty";
|
| +
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +} // namespace payments
|
|
|