The most awesome validation engine ever created for C++.
Qt is one of the best frameworks for C++ mostly used for GUI and Mobile application but also usefull for developing high-end backends. I looked for a good validation library to be used in companion with QRestServer but I couldn't find any good one, So I developed this library based on Respect\Validation php library.
The Hello World validator is something like this:
QString foo = "bar";
QFieldValidator().notEmpty().validate(foo);
It is possible to use validators in a chain. Sample below validates a string containing numbers and letters, no whitespace and length between 1 and 15.
QString foo = "bar";
QFieldValidator().asciiAlNum().minLenght(1).maxLenght(15).validate(foo);
There are three conditional methods that can be used for complex rules:
oneOf(const QList<QFieldValidator>& _validators)
Used when the value can match with any of the specified validation rulesQFieldValidator().oneOf({ QFieldValidator().email(), QFieldValidator().mobile() }).isValid("09136477280");
allOf(const QList<QFieldValidator>& _validators)
This is an alias for chained rulesQFieldValidator().allOf({ QFieldValidator().asciiAlNum(), QFieldValidator().minLenght(5) }).isValid("09136477280");
when(const QFieldValidator& _if, const QFieldValidator& _then, const QFieldValidator& _else)
For binary conditions when to validate based on the type of valueQFieldValidator().when({ QFieldValidator().email(), QFieldValidator().emailNotDomain("gmail.com",true), QFieldValidator().mobile() }).isValid("09136477280");
There are two main methods to validate a value:
bool isValid(const QVariant& _value, const QString& _fieldName = QString())
This method will validate without throwing an excption. returns false on any error and error message can be accessed byerrorMessage()
methodvoid validate(const QVariant& _value, const QString& _fieldName = QString());
This method will throw an excption on any error with a full description of the error as exception message
Given this simple object:
const char user[]="{\"name\": \"Mehran\"},{\"mobile\": \"09126174582\"}";
It is possible to validate its attributes in a single chain:
QFieldValidator().hasKey("name")
.hasKey("mobile", QFieldValidator().mobile())
.validate(user);
Or with more complex and nested objects: Given this simple object:
QJsonObject user={
{"name", "Mehran"},
{"info", QVariantMap({
{"mobile", "09126174582"}
})
}
};
It is possible to validate its attributes in a single chain:
QFieldValidator().hasKey("name")
.hasNestedKey("info.mobile", QFieldValidator().mobile())
.validate(user);
All chained rules are mandatory in QfieeldValidator so if you want to treat a value as optional you can use QFieldValidator::optional()
rule:
QString sampleEmpty;
QString sampleString = "abcd";
QString sampleMobile = "09126578425";
QFieldValidator mobileValidator = QFieldValidator().mobile();
mobileValidator.isValid(sampleEmpty); //false as mobile number is empty
mobileValidator.isValid(sampleString); //false as it is no mobile
mobileValidator.isValid(sampleMobile); //true
QFiledValidator().optional(mobileValidator).isValid(sampleEmpty) //true as was optional
QFiledValidator().optional(mobileValidator).isValid(sampleString) //false as it is incorrect
QFiledValidator().optional(mobileValidator).isValid(sampleMobile) //true
Once created, you can reuse your validator anywhere. Remember mobileValidator
?
mobileValidator.isValid(""); //false as mobile number is empty
mobileValidator.isValid("abcd"); //false as it is no mobile
mobileValidator.isValid("0915154650"); //true
This exception is thrown just if there were an error creating a validator. As said at coding time not run time and independednt of the validating value
This exception is thrown when a required parameter is passed as empty or null except when specified as optional
This exception is thrown when a required parameter is set but invalid according to the chained rules
Installation is described in INSTALL file
Main idea of this library has been derived from Respect\Validation PHP library
QFieldValidator has been published under the terms of GNU LESSER GENERAL PUBLIC LICENSE as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.