|
@@ -235,6 +235,166 @@ namespace Framework
|
|
|
|
|
|
namespace Validator
|
|
|
{
|
|
|
+ class JSONValidationResult : public Framework::ReferenceCounter
|
|
|
+ {
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONValidationResult();
|
|
|
+ __declspec(dllexport) virtual ~JSONValidationResult();
|
|
|
+ virtual bool isValid() const = 0;
|
|
|
+ virtual void printInvalidInfo(int indent = 0) const = 0;
|
|
|
+ virtual JSONValue* getValidPart() const = 0;
|
|
|
+ virtual Text getPath() const = 0;
|
|
|
+ virtual bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const = 0;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONTypeMissmatch : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ JSONValue* foundValue;
|
|
|
+ XML::Element* expected;
|
|
|
+ JSONValidationResult* reason;
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason);
|
|
|
+ __declspec(dllexport) ~JSONTypeMissmatch();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONUnknownValue : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ JSONValue* foundValue;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONUnknownValue(Text path, JSONValue* foundValue);
|
|
|
+ __declspec(dllexport) ~JSONUnknownValue();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ __declspec(dllexport) bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONMissingValue : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ XML::Element* expected;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONMissingValue(Text path, XML::Element* expected);
|
|
|
+ __declspec(dllexport) ~JSONMissingValue();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ __declspec(dllexport) bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONMissingOneOf : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ RCArray<XML::Element> expected;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONMissingOneOf(Text path, XML::Editor expected);
|
|
|
+ __declspec(dllexport) ~JSONMissingOneOf();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ __declspec(dllexport) bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONNoTypeMatching : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ JSONValue* foundValue;
|
|
|
+ RCArray<XML::Element> expected;
|
|
|
+ RCArray<JSONValidationResult> reasons;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons);
|
|
|
+ __declspec(dllexport) ~JSONNoTypeMatching();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ __declspec(dllexport) bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ class JSONValidValue : public JSONValidationResult
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ Text path;
|
|
|
+ JSONValue* value;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONValidValue(Text path, JSONValue* value);
|
|
|
+ __declspec(dllexport) ~JSONValidValue();
|
|
|
+ __declspec(dllexport) bool isValid() const override;
|
|
|
+ __declspec(dllexport) void printInvalidInfo(int indent) const override;
|
|
|
+ __declspec(dllexport) JSONValue* getValidPart() const override;
|
|
|
+ __declspec(dllexport) Text getPath() const override;
|
|
|
+ __declspec(dllexport) bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
+ };
|
|
|
+
|
|
|
+ template<typename T>
|
|
|
+ class StringValidationBuilder;
|
|
|
+
|
|
|
+ template<typename T>
|
|
|
+ class NumberValidationBuilder;
|
|
|
+
|
|
|
+ template<typename T>
|
|
|
+ class BoolValidationBuilder;
|
|
|
+
|
|
|
+ template<typename T>
|
|
|
+ class ObjectValidationBuilder;
|
|
|
+
|
|
|
+ template<typename T>
|
|
|
+ class ArrayValidationBuilder;
|
|
|
+
|
|
|
+ class JSONValidator : public Framework::ReferenceCounter
|
|
|
+ {
|
|
|
+ private:
|
|
|
+ XML::Element* constraints;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) JSONValidator(XML::Element* constraints);
|
|
|
+ __declspec(dllexport) ~JSONValidator();
|
|
|
+ __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue) const;
|
|
|
+ __declspec(dllexport) bool isValid(JSONValue* zValue) const;
|
|
|
+ /**
|
|
|
+ * returns the valid part of the json by performing the following operations in the specified order untill no further changes are made:
|
|
|
+ * - invalid or unknown object properties are removed
|
|
|
+ * - missing object properties with default values are added if missing
|
|
|
+ * - invalid array elements are removed
|
|
|
+ *
|
|
|
+ * @param zValue the json value to to extract the valid part without increased reference counter
|
|
|
+ * @return the valid part or 0 if no valid part exists
|
|
|
+ */
|
|
|
+ __declspec(dllexport) JSONValue* getValidParts(JSONValue* zValue) const;
|
|
|
+ __declspec(dllexport) XML::Element* zConstraints();
|
|
|
+
|
|
|
+ private:
|
|
|
+ __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue, XML::Element* zConstraints, Text path) const;
|
|
|
+ __declspec(dllexport) JSONValidationResult* validateMultipleTypes(JSONValue* zChildValue, XML::Element* zPossibleChildConstraints, Text childPath) const;
|
|
|
+
|
|
|
+ public:
|
|
|
+ __declspec(dllexport) static StringValidationBuilder<JSONValidator>* buildForString();
|
|
|
+ __declspec(dllexport) static NumberValidationBuilder<JSONValidator>* buildForNumber();
|
|
|
+ __declspec(dllexport) static BoolValidationBuilder<JSONValidator>* buildForBool();
|
|
|
+ __declspec(dllexport) static ObjectValidationBuilder<JSONValidator>* buildForObject();
|
|
|
+ __declspec(dllexport) static ArrayValidationBuilder<JSONValidator>* buildForArray();
|
|
|
+ };
|
|
|
+
|
|
|
template<typename T>
|
|
|
class StringValidationBuilder
|
|
|
{
|
|
@@ -521,9 +681,23 @@ namespace Framework
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- ObjectValidationBuilder<T>* withDefault(JSONObject* zObj)
|
|
|
+ ObjectValidationBuilder<T>* withRequiredAttribute(Text name, JSONValidator* validator)
|
|
|
+ {
|
|
|
+ if (!element.selectChildsByAttribute("name", name).exists())
|
|
|
+ {
|
|
|
+ XML::Element* attr = new XML::Element("<value></value>");
|
|
|
+ attr->setAttribute("name", name);
|
|
|
+ element.addChild(attr);
|
|
|
+ }
|
|
|
+ element.selectChildsByAttribute("name", name).addChild(validator->zConstraints()->dublicate());
|
|
|
+ validator->release();
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
|
|
|
{
|
|
|
- element.setAttribute("default", zObj->toString());
|
|
|
+ element.setAttribute("default", obj->toString()); obj
|
|
|
+ obj->release();
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -557,6 +731,12 @@ namespace Framework
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ ObjectValidationBuilder<T>* removeInvalidEntries()
|
|
|
+ {
|
|
|
+ element.setAttribute("removeInvalidEntries", "true");
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
T* finishObject()
|
|
|
{
|
|
|
T* result = builder(element);
|
|
@@ -623,20 +803,30 @@ namespace Framework
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ ArrayValidationBuilder<T>* addAcceptedTypeInArray(JSONValidator* validator)
|
|
|
+ {
|
|
|
+ element.addChild(validator->zConstraints()->dublicate());
|
|
|
+ validator->release();
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
ArrayValidationBuilder<T>* acceptNullsInArray()
|
|
|
{
|
|
|
element.setAttribute("nullsEnabled", "true");
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- ArrayValidationBuilder<T>* withDefault(JSONArray* zArray)
|
|
|
+ ArrayValidationBuilder<T>* withDefault(JSONArray* array)
|
|
|
{
|
|
|
- element.setAttribute("default", zArray->toString());
|
|
|
+ element.setAttribute("default", array->toString());
|
|
|
+ array->release();
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
ArrayValidationBuilder<T>* withDefaultNull()
|
|
|
{
|
|
|
element.setAttribute("default", "null");
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
ArrayValidationBuilder<T>* whichCanBeNull()
|
|
@@ -657,6 +847,12 @@ namespace Framework
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ ArrayValidationBuilder<T>* removeInvalidEntries()
|
|
|
+ {
|
|
|
+ element.setAttribute("removeInvalidEntries", "true");
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
T* finishArray()
|
|
|
{
|
|
|
T* result = builder(element);
|
|
@@ -664,151 +860,6 @@ namespace Framework
|
|
|
return result;
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
- class JSONValidationResult : public Framework::ReferenceCounter
|
|
|
- {
|
|
|
- public:
|
|
|
- JSONValidationResult();
|
|
|
- virtual ~JSONValidationResult();
|
|
|
- virtual bool isValid() const = 0;
|
|
|
- virtual void printInvalidInfo(int indent = 0) const = 0;
|
|
|
- virtual JSONValue* getValidPart() const = 0;
|
|
|
- virtual Text getPath() const = 0;
|
|
|
- virtual bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const = 0;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONTypeMissmatch : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- JSONValue* foundValue;
|
|
|
- XML::Element* expected;
|
|
|
- JSONValidationResult* reason;
|
|
|
- public:
|
|
|
- JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason);
|
|
|
- ~JSONTypeMissmatch();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONUnknownValue : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- JSONValue* foundValue;
|
|
|
-
|
|
|
- public:
|
|
|
- JSONUnknownValue(Text path, JSONValue* foundValue);
|
|
|
- ~JSONUnknownValue();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONMissingValue : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- XML::Element* expected;
|
|
|
-
|
|
|
- public:
|
|
|
- JSONMissingValue(Text path, XML::Element* expected);
|
|
|
- ~JSONMissingValue();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONMissingOneOf : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- RCArray<XML::Element> expected;
|
|
|
-
|
|
|
- public:
|
|
|
- JSONMissingOneOf(Text path, XML::Editor expected);
|
|
|
- ~JSONMissingOneOf();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONNoTypeMatching : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- JSONValue* foundValue;
|
|
|
- RCArray<XML::Element> expected;
|
|
|
- RCArray<JSONValidationResult> reasons;
|
|
|
-
|
|
|
- public:
|
|
|
- JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons);
|
|
|
- ~JSONNoTypeMatching();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONValidValue : public JSONValidationResult
|
|
|
- {
|
|
|
- private:
|
|
|
- Text path;
|
|
|
- JSONValue* value;
|
|
|
-
|
|
|
- public:
|
|
|
- JSONValidValue(Text path, JSONValue* value);
|
|
|
- ~JSONValidValue();
|
|
|
- bool isValid() const override;
|
|
|
- void printInvalidInfo(int indent) const override;
|
|
|
- JSONValue* getValidPart() const override;
|
|
|
- Text getPath() const override;
|
|
|
- bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
|
|
|
- };
|
|
|
-
|
|
|
- class JSONValidator : public Framework::ReferenceCounter
|
|
|
- {
|
|
|
- private:
|
|
|
- XML::Element* constraints;
|
|
|
-
|
|
|
- public:
|
|
|
- __declspec(dllexport) JSONValidator(XML::Element* constraints);
|
|
|
- __declspec(dllexport) ~JSONValidator();
|
|
|
- __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue) const;
|
|
|
- __declspec(dllexport) bool isValid(JSONValue* zValue) const;
|
|
|
- /**
|
|
|
- * returns the valid part of the json by performing the following operations in the specified order untill no further changes are made:
|
|
|
- * - invalid or unknown object properties are removed
|
|
|
- * - missing object properties with default values are added if missing
|
|
|
- * - invalid array elements are removed
|
|
|
- *
|
|
|
- * @param zValue the json value to to extract the valid part without increased reference counter
|
|
|
- * @return the valid part or 0 if no valid part exists
|
|
|
- */
|
|
|
- __declspec(dllexport) JSONValue* getValidParts(JSONValue* zValue) const;
|
|
|
- __declspec(dllexport) XML::Element* zConstraints();
|
|
|
-
|
|
|
- private:
|
|
|
- __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue, XML::Element* zConstraints, Text path) const;
|
|
|
- __declspec(dllexport) JSONValidationResult* validateMultipleTypes(JSONValue* zChildValue, XML::Element* zPossibleChildConstraints, Text childPath) const;
|
|
|
-
|
|
|
- public:
|
|
|
- __declspec(dllexport) static StringValidationBuilder<JSONValidator>* buildForString();
|
|
|
- __declspec(dllexport) static NumberValidationBuilder<JSONValidator>* buildForNumber();
|
|
|
- __declspec(dllexport) static BoolValidationBuilder<JSONValidator>* buildForBool();
|
|
|
- __declspec(dllexport) static ObjectValidationBuilder<JSONValidator>* buildForObject();
|
|
|
- __declspec(dllexport) static ArrayValidationBuilder<JSONValidator>* buildForArray();
|
|
|
- };
|
|
|
}
|
|
|
}
|
|
|
}
|