| 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..c4004a5c727b742706a669817857dbcb5dc4a343
|
| --- /dev/null
|
| +++ b/components/payments/payments_validators.cc
|
| @@ -0,0 +1,53 @@
|
| +// 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"
|
| +
|
| +#include "third_party/re2/src/re2/re2.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,
|
| + const std::string& system,
|
| + std::string* optionalErrorMessage) {
|
| + if (system == "urn:iso:std:iso:4217") {
|
| + if (RE2::FullMatch(code, "[A-Z]{3}"))
|
| + return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + code +
|
| + "' is not a valid ISO 4217 currency code, should "
|
| + "be 3 upper case letters [A-Z]";
|
| +
|
| + return false;
|
| + }
|
| +
|
| + 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 (RE2::FullMatch(amount, "-?[0-9]+(\\.[0-9]+)?"))
|
| + return true;
|
| +
|
| + if (optionalErrorMessage)
|
| + *optionalErrorMessage = "'" + amount + "' is not a valid amount format";
|
| +
|
| + return false;
|
| +}
|
| +
|
| +} // namespace payments
|
|
|