Wexus2 0.20
wexus2.src/wexus/ValidationExpr.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_VALIDATIONEXPR_H__
00009 #define __INCLUDED_WEXUS_VALIDATIONEXPR_H__
00010 
00011 #include <wexus/TR1.h>
00012 
00013 #include <QVariant>
00014 #include <QStringList>
00015 
00016 namespace wexus
00017 {
00018   class ValidationExpr;
00019 
00020   class validate;
00021 }
00022 
00023 /**
00024  * A validation expression.
00025  *
00026  * Although there were plans to be able to create
00027  * nesting operators, compelte with ! || and other operators,
00028  * this was deemed to confusing with respect to how
00029  * to make meaningfull error messages. A future
00030  * project, perhaps.
00031  *
00032  * @author Aleksander Demko
00033  */ 
00034 class wexus::ValidationExpr
00035 {
00036   public:
00037     /**
00038      * Null expression, that doesn't require anything
00039      * This expression cannot be used or tested.
00040      *
00041      * @author Aleksander Demko
00042      */ 
00043     ValidationExpr(void);
00044     /// shallow copy ctor
00045     ValidationExpr(const ValidationExpr &rhs);
00046 
00047     /// is this the default, null expression
00048     bool isNull(void) const;
00049 
00050     /// tests to se if v meets the requiresments
00051     /// of the expressinall the tests
00052     /// this ValidationExpr cannt be null
00053     bool test(const QVariant &v, QStringList *outerrors = 0) const;
00054 
00055     // "serialization"
00056 
00057     /// package this expression into a variant
00058     QVariant toVariant(void) const;
00059 
00060     /// unpackage an expression from the variant
00061     static ValidationExpr fromVariant(const QVariant &v);
00062 
00063     // explicit "ctors"
00064 
00065 
00066     /// always returns true
00067     static ValidationExpr optional(void);
00068 
00069     /// the value cannot be invalid
00070     static ValidationExpr required(void);
00071 
00072     // note that all the follow checkers
00073     // only test valid variables.
00074     // that is, they all return true on invalid values
00075     //  (so as to allow the processing of optional fields, etc)
00076 
00077     // string checkers
00078     static ValidationExpr minLength(int l);
00079     static ValidationExpr maxLength(int l);
00080     static ValidationExpr notEmptyLength(void);    // similar to minLength(1), but with a different error message
00081 
00082     // int checkers
00083     static ValidationExpr minValue(double d);
00084     static ValidationExpr maxValue(double d);
00085 
00086     // fancy string checkers
00087     static ValidationExpr isEmail(void);
00088 
00089     // operators
00090 
00091     //ValidationExpr operator ! (void); // not operator
00092 
00093     /// (short circuit) and operator
00094     ValidationExpr operator && (const ValidationExpr &rhs);
00095 
00096   public:
00097     class ImpObject;
00098   private:
00099     class Imp;
00100     class Optional;
00101     class Required;
00102     class Length;
00103     //class UniOp;
00104     class BinOp;
00105 
00106     ValidationExpr(const std::shared_ptr<Imp> &imp);
00107 
00108   private:
00109     std::shared_ptr<Imp> dm_imp;
00110 };
00111 
00112 /**
00113  * This is a convience class. Rather than make a new validate namespace
00114  * and make a bunch of inline functions to bounce to the static methods
00115  * of ValidationExpr, we'll make a similarly named class that just
00116  * inherits all the static methods.
00117  * 
00118  * @author Aleksander Demko
00119  */ 
00120 class wexus::validate : public ValidationExpr
00121 {
00122   public:
00123 
00124   private:
00125     /// not implemented. this class is not instantiatable.
00126     validate(void);
00127 };
00128 
00129 #endif
00130 
 All Classes Namespaces Functions Variables Enumerations Enumerator