Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_PROP_CONSTRAINTS_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_PROP_CONSTRAINTS_H_ |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <type_traits> |
| 10 | #include <vector> |
| 11 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 13 | #include <base/values.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 14 | #include <weave/error.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 15 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 16 | #include "src/commands/prop_values.h" |
| 17 | #include "src/commands/schema_constants.h" |
| 18 | #include "src/commands/schema_utils.h" |
| 19 | #include "src/string_utils.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 20 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 21 | namespace weave { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 22 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 23 | enum class ConstraintType { Min, Max, StringLengthMin, StringLengthMax, OneOf }; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 24 | |
| 25 | // Abstract base class for all parameter constraints. Many constraints are |
| 26 | // type-dependent. Thus, a numeric parameter could have "minimum" and/or |
| 27 | // "maximum" constraints specified. Some constraints, such as "OneOf" apply to |
| 28 | // any data type. |
| 29 | class Constraint { |
| 30 | public: |
| 31 | Constraint() = default; |
| 32 | virtual ~Constraint(); |
| 33 | |
| 34 | // Gets the constraint type. |
| 35 | virtual ConstraintType GetType() const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 36 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 37 | // Checks if any of the constraint properties/attributes are overridden |
| 38 | // from their base schema definition. If the constraint is inherited, then |
| 39 | // it will not be written to JSON when saving partial schema. |
| 40 | virtual bool HasOverriddenAttributes() const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 41 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 42 | // Validates a parameter against the constraint. Returns true if parameter |
| 43 | // value satisfies the constraint, otherwise fills the optional |error| with |
| 44 | // the details for the failure. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 45 | virtual bool Validate(const PropValue& value, ErrorPtr* error) const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 46 | |
| 47 | // Makes a full copy of this Constraint instance. |
| 48 | virtual std::unique_ptr<Constraint> Clone() const = 0; |
| 49 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 50 | // Makes a copy of the constraint object, marking all the attributes |
| 51 | // as inherited from the original definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 52 | virtual std::unique_ptr<Constraint> CloneAsInherited() const = 0; |
| 53 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 54 | // Saves the constraint into the specified JSON |dict| object, representing |
| 55 | // the object schema. If |overridden_only| is set to true, then the |
| 56 | // inherited constraints will not be added to the schema object. |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 57 | virtual void AddToJsonDict(base::DictionaryValue* dict, |
| 58 | bool overridden_only) const; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 59 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 60 | // Saves the value of constraint to JSON value. E.g., if the numeric |
| 61 | // constraint was defined as {"minimum":20} this will create a JSON value |
| 62 | // of 20. The current design implies that each constraint has one value |
| 63 | // only. If this assumption changes, this interface needs to be updated |
| 64 | // accordingly. |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 65 | virtual std::unique_ptr<base::Value> ToJson() const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 66 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 67 | // Overloaded by the concrete class implementation, it should return the |
| 68 | // JSON object property name to store the constraint's value as. |
| 69 | // E.g., if the numeric constraint was defined as {"minimum":20} this |
| 70 | // method should return "minimum". |
| 71 | virtual const char* GetDictKey() const = 0; |
| 72 | |
| 73 | protected: |
| 74 | // Static helper methods to format common constraint validation errors. |
| 75 | // They fill the |error| object with specific error message. |
| 76 | // Since these functions could be used by constraint objects for various |
| 77 | // data types, the values used in validation are expected to be |
| 78 | // send as strings already. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 79 | static bool ReportErrorLessThan(ErrorPtr* error, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 80 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 81 | const std::string& limit); |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 82 | static bool ReportErrorGreaterThan(ErrorPtr* error, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 83 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 84 | const std::string& limit); |
| 85 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 86 | static bool ReportErrorNotOneOf(ErrorPtr* error, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 87 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 88 | const std::vector<std::string>& values); |
| 89 | |
| 90 | private: |
| 91 | DISALLOW_COPY_AND_ASSIGN(Constraint); |
| 92 | }; |
| 93 | |
| 94 | // ConstraintMinMaxBase is a base class for numeric Minimum and Maximum |
| 95 | // constraints. |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 96 | template <typename T> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 97 | class ConstraintMinMaxBase : public Constraint { |
| 98 | public: |
| 99 | explicit ConstraintMinMaxBase(const InheritableAttribute<T>& limit) |
| 100 | : limit_(limit) {} |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 101 | explicit ConstraintMinMaxBase(const T& limit) : limit_(limit) {} |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 102 | |
| 103 | // Implementation of Constraint::HasOverriddenAttributes(). |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 104 | bool HasOverriddenAttributes() const override { return !limit_.is_inherited; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 105 | |
| 106 | // Implementation of Constraint::ToJson(). |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 107 | std::unique_ptr<base::Value> ToJson() const override { |
| 108 | return TypedValueToJson(limit_.value); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Stores the upper/lower value limit for maximum/minimum constraint. |
| 112 | // |limit_.is_inherited| indicates whether the constraint is inherited |
| 113 | // from base schema or overridden. |
| 114 | InheritableAttribute<T> limit_; |
| 115 | |
| 116 | private: |
| 117 | DISALLOW_COPY_AND_ASSIGN(ConstraintMinMaxBase); |
| 118 | }; |
| 119 | |
| 120 | // Implementation of Minimum value constraint for Integer/Double types. |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 121 | template <typename T> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 122 | class ConstraintMin : public ConstraintMinMaxBase<T> { |
| 123 | public: |
| 124 | explicit ConstraintMin(const InheritableAttribute<T>& limit) |
| 125 | : ConstraintMinMaxBase<T>(limit) {} |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 126 | explicit ConstraintMin(const T& limit) : ConstraintMinMaxBase<T>(limit) {} |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 127 | |
| 128 | // Implementation of Constraint::GetType(). |
Yunlian Jiang | 89a9725 | 2015-01-28 13:33:46 -0800 | [diff] [blame] | 129 | ConstraintType GetType() const override { return ConstraintType::Min; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 130 | |
| 131 | // Implementation of Constraint::Validate(). |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 132 | bool Validate(const PropValue& value, ErrorPtr* error) const override { |
Vitaly Buka | c58a828 | 2015-07-29 01:25:20 -0700 | [diff] [blame] | 133 | const T& v = static_cast<const TypedValueBase<T>&>(value).GetValue(); |
| 134 | if (v < this->limit_.value) { |
Vitaly Buka | 24d6fd5 | 2015-08-13 23:22:48 -0700 | [diff] [blame] | 135 | return this->ReportErrorLessThan(error, std::to_string(v), |
| 136 | std::to_string(this->limit_.value)); |
Vitaly Buka | c58a828 | 2015-07-29 01:25:20 -0700 | [diff] [blame] | 137 | } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 141 | // Implementation of Constraint::Clone(). |
| 142 | std::unique_ptr<Constraint> Clone() const override { |
| 143 | return std::unique_ptr<Constraint>{new ConstraintMin{this->limit_}}; |
| 144 | } |
| 145 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 146 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 147 | std::unique_ptr<Constraint> CloneAsInherited() const override { |
| 148 | return std::unique_ptr<Constraint>{new ConstraintMin{this->limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 152 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 153 | return commands::attributes::kNumeric_Min; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | DISALLOW_COPY_AND_ASSIGN(ConstraintMin); |
| 158 | }; |
| 159 | |
| 160 | // Implementation of Maximum value constraint for Integer/Double types. |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 161 | template <typename T> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 162 | class ConstraintMax : public ConstraintMinMaxBase<T> { |
| 163 | public: |
| 164 | explicit ConstraintMax(const InheritableAttribute<T>& limit) |
| 165 | : ConstraintMinMaxBase<T>(limit) {} |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 166 | explicit ConstraintMax(const T& limit) : ConstraintMinMaxBase<T>(limit) {} |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 167 | |
| 168 | // Implementation of Constraint::GetType(). |
Yunlian Jiang | 89a9725 | 2015-01-28 13:33:46 -0800 | [diff] [blame] | 169 | ConstraintType GetType() const override { return ConstraintType::Max; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 170 | |
| 171 | // Implementation of Constraint::Validate(). |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 172 | bool Validate(const PropValue& value, ErrorPtr* error) const override { |
Vitaly Buka | c58a828 | 2015-07-29 01:25:20 -0700 | [diff] [blame] | 173 | const T& v = static_cast<const TypedValueBase<T>&>(value).GetValue(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 174 | if (v > this->limit_.value) |
Vitaly Buka | 24d6fd5 | 2015-08-13 23:22:48 -0700 | [diff] [blame] | 175 | return this->ReportErrorGreaterThan(error, std::to_string(v), |
| 176 | std::to_string(this->limit_.value)); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 180 | // Implementation of Constraint::Clone(). |
| 181 | std::unique_ptr<Constraint> Clone() const override { |
| 182 | return std::unique_ptr<Constraint>{new ConstraintMax{this->limit_}}; |
| 183 | } |
| 184 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 185 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 186 | std::unique_ptr<Constraint> CloneAsInherited() const override { |
| 187 | return std::unique_ptr<Constraint>{new ConstraintMax{this->limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 191 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 192 | return commands::attributes::kNumeric_Max; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | DISALLOW_COPY_AND_ASSIGN(ConstraintMax); |
| 197 | }; |
| 198 | |
| 199 | // ConstraintStringLength is a base class for Minimum/Maximum string length |
| 200 | // constraints, similar to ConstraintMinMaxBase of numeric types. |
| 201 | class ConstraintStringLength : public Constraint { |
| 202 | public: |
| 203 | explicit ConstraintStringLength(const InheritableAttribute<int>& limit); |
| 204 | explicit ConstraintStringLength(int limit); |
| 205 | |
| 206 | // Implementation of Constraint::HasOverriddenAttributes(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 207 | bool HasOverriddenAttributes() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 208 | // Implementation of Constraint::ToJson(). |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 209 | std::unique_ptr<base::Value> ToJson() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 210 | |
| 211 | // Stores the upper/lower value limit for string length constraint. |
| 212 | // |limit_.is_inherited| indicates whether the constraint is inherited |
| 213 | // from base schema or overridden. |
| 214 | InheritableAttribute<int> limit_; |
| 215 | |
| 216 | private: |
| 217 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLength); |
| 218 | }; |
| 219 | |
| 220 | // Implementation of Minimum string length constraint. |
| 221 | class ConstraintStringLengthMin : public ConstraintStringLength { |
| 222 | public: |
| 223 | explicit ConstraintStringLengthMin(const InheritableAttribute<int>& limit); |
| 224 | explicit ConstraintStringLengthMin(int limit); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 225 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 226 | // Implementation of Constraint::GetType(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 227 | ConstraintType GetType() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 228 | return ConstraintType::StringLengthMin; |
| 229 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 230 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 231 | // Implementation of Constraint::Validate(). |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 232 | bool Validate(const PropValue& value, ErrorPtr* error) const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 233 | |
| 234 | // Implementation of Constraint::Clone(). |
| 235 | std::unique_ptr<Constraint> Clone() const override; |
| 236 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 237 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 238 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 239 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 240 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 241 | return commands::attributes::kString_MinLength; |
| 242 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 243 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 244 | private: |
| 245 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLengthMin); |
| 246 | }; |
| 247 | |
| 248 | // Implementation of Maximum string length constraint. |
| 249 | class ConstraintStringLengthMax : public ConstraintStringLength { |
| 250 | public: |
| 251 | explicit ConstraintStringLengthMax(const InheritableAttribute<int>& limit); |
| 252 | explicit ConstraintStringLengthMax(int limit); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 253 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 254 | // Implementation of Constraint::GetType(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 255 | ConstraintType GetType() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 256 | return ConstraintType::StringLengthMax; |
| 257 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 258 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 259 | // Implementation of Constraint::Validate(). |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 260 | bool Validate(const PropValue& value, ErrorPtr* error) const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 261 | |
| 262 | // Implementation of Constraint::Clone(). |
| 263 | std::unique_ptr<Constraint> Clone() const override; |
| 264 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 265 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 266 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
| 267 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 268 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 269 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 270 | return commands::attributes::kString_MaxLength; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLengthMax); |
| 275 | }; |
| 276 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 277 | // Implementation of OneOf constraint for different data types. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 278 | class ConstraintOneOf : public Constraint { |
| 279 | public: |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 280 | explicit ConstraintOneOf(InheritableAttribute<ValueVector> set); |
| 281 | explicit ConstraintOneOf(ValueVector set); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 282 | |
| 283 | // Implementation of Constraint::GetType(). |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 284 | ConstraintType GetType() const override { return ConstraintType::OneOf; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 285 | |
| 286 | // Implementation of Constraint::HasOverriddenAttributes(). |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 287 | bool HasOverriddenAttributes() const override { return !set_.is_inherited; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 288 | |
| 289 | // Implementation of Constraint::Validate(). |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 290 | bool Validate(const PropValue& value, ErrorPtr* error) const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 291 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 292 | // Implementation of Constraint::Clone(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 293 | std::unique_ptr<Constraint> Clone() const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 294 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 295 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 296 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 297 | |
| 298 | // Implementation of Constraint::ToJson(). |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 299 | std::unique_ptr<base::Value> ToJson() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 300 | |
| 301 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 302 | const char* GetDictKey() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 303 | |
| 304 | // Stores the list of acceptable values for the parameter. |
| 305 | // |set_.is_inherited| indicates whether the constraint is inherited |
| 306 | // from base schema or overridden. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 307 | InheritableAttribute<ValueVector> set_; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 308 | |
| 309 | private: |
| 310 | DISALLOW_COPY_AND_ASSIGN(ConstraintOneOf); |
| 311 | }; |
| 312 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 313 | } // namespace weave |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 314 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 315 | #endif // LIBWEAVE_SRC_COMMANDS_PROP_CONSTRAINTS_H_ |