Strict value validation in passport.

This commit is contained in:
John Preston 2018-04-13 15:15:07 +04:00
parent e7ce4ca10a
commit e82430cb50
5 changed files with 49 additions and 14 deletions

View File

@ -20,6 +20,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Passport {
constexpr auto kMaxNameSize = 255;
constexpr auto kMaxDocumentSize = 24;
constexpr auto kMaxStreetSize = 64;
constexpr auto kMinCitySize = 2;
constexpr auto kMaxCitySize = 64;
constexpr auto kMinPostcodeSize = 2;
constexpr auto kMaxPostcodeSize = 12;
EditDocumentScheme GetDocumentScheme(
Scope::Type type,
base::optional<Value::Type> scansType) {
@ -39,9 +47,18 @@ EditDocumentScheme GetDocumentScheme(
return value;
};
const auto DontValidate = nullptr;
const auto NotEmptyValidate = [](const QString &value) {
return !value.isEmpty();
const auto LimitedValidate = [](int max, int min = 1) {
return [=](const QString &value) {
return (value.size() >= min) && (value.size() <= max);
};
};
const auto NameValidate = LimitedValidate(kMaxNameSize);
const auto DocumentValidate = LimitedValidate(kMaxDocumentSize);
const auto StreetValidate = LimitedValidate(kMaxStreetSize);
const auto CityValidate = LimitedValidate(kMaxCitySize, kMinCitySize);
const auto PostcodeValidate = LimitedValidate(
kMaxPostcodeSize,
kMinPostcodeSize);
const auto DateValidate = [](const QString &value) {
return QRegularExpression(
"^\\d{2}\\.\\d{2}\\.\\d{4}$"
@ -82,16 +99,18 @@ EditDocumentScheme GetDocumentScheme(
PanelDetailsType::Text,
qsl("first_name"),
lang(lng_passport_first_name),
NotEmptyValidate,
NameValidate,
DontFormat,
kMaxNameSize,
},
{
ValueClass::Fields,
PanelDetailsType::Text,
qsl("last_name"),
lang(lng_passport_last_name),
DontValidate,
NameValidate,
DontFormat,
kMaxNameSize,
},
{
ValueClass::Fields,
@ -122,8 +141,9 @@ EditDocumentScheme GetDocumentScheme(
PanelDetailsType::Text,
qsl("document_no"),
lang(lng_passport_document_number),
NotEmptyValidate,
DocumentValidate,
DontFormat,
kMaxDocumentSize,
},
{
ValueClass::Scans,
@ -161,8 +181,9 @@ EditDocumentScheme GetDocumentScheme(
PanelDetailsType::Text,
qsl("street_line1"),
lang(lng_passport_street),
NotEmptyValidate,
StreetValidate,
DontFormat,
kMaxStreetSize,
},
{
ValueClass::Fields,
@ -171,14 +192,16 @@ EditDocumentScheme GetDocumentScheme(
lang(lng_passport_street),
DontValidate,
DontFormat,
kMaxStreetSize,
},
{
ValueClass::Fields,
PanelDetailsType::Text,
qsl("city"),
lang(lng_passport_city),
NotEmptyValidate,
CityValidate,
DontFormat,
kMaxStreetSize,
},
{
ValueClass::Fields,
@ -187,6 +210,7 @@ EditDocumentScheme GetDocumentScheme(
lang(lng_passport_state),
DontValidate,
DontFormat,
kMaxStreetSize,
},
{
ValueClass::Fields,
@ -201,8 +225,9 @@ EditDocumentScheme GetDocumentScheme(
PanelDetailsType::Text,
qsl("post_code"),
lang(lng_passport_postcode),
NotEmptyValidate,
PostcodeValidate,
DontFormat,
kMaxPostcodeSize,
},
};
return result;

View File

@ -24,7 +24,11 @@ namespace {
class TextRow : public PanelDetailsRow {
public:
TextRow(QWidget *parent, const QString &label, const QString &value);
TextRow(
QWidget *parent,
const QString &label,
const QString &value,
int limit);
bool setFocusFast() override;
rpl::producer<QString> value() const override;
@ -191,10 +195,12 @@ private:
TextRow::TextRow(
QWidget *parent,
const QString &label,
const QString &value)
const QString &value,
int limit)
: PanelDetailsRow(parent, label)
, _field(this, st::passportDetailsField, nullptr, value)
, _value(value) {
_field->setMaxLength(limit);
connect(_field, &Ui::InputField::changed, [=] {
_value = valueCurrent();
});
@ -890,11 +896,12 @@ object_ptr<PanelDetailsRow> PanelDetailsRow::Create(
not_null<PanelController*> controller,
const QString &label,
const QString &value,
const QString &error) {
const QString &error,
int limit) {
auto result = [&]() -> object_ptr<PanelDetailsRow> {
switch (type) {
case Type::Text:
return object_ptr<TextRow>(parent, label, value);
return object_ptr<TextRow>(parent, label, value, limit);
case Type::Country:
return object_ptr<CountryRow>(parent, controller, label, value);
case Type::Gender:

View File

@ -59,7 +59,8 @@ public:
not_null<PanelController*> controller,
const QString &label,
const QString &value,
const QString &error);
const QString &error,
int limit = 0);
virtual bool setFocusFast();
virtual rpl::producer<QString> value() const = 0;

View File

@ -246,7 +246,8 @@ not_null<Ui::RpWidget*> PanelEditDocument::setupContent(
_controller,
row.label,
valueOrEmpty(*fields, row.key),
QString())));
QString(),
row.lengthLimit)));
}
inner->add(

View File

@ -38,6 +38,7 @@ struct EditDocumentScheme {
QString label;
base::lambda<bool(const QString &value)> validate;
base::lambda<QString(const QString &value)> format;
int lengthLimit = 0;
};
std::vector<Row> rows;
QString rowsHeader;