1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111 |
- #pragma once
- #include <functional>
- #include <source_location>
- #include "Array.h"
- #include "ReferenceCounter.h"
- #include "Text.h"
- #include "Trie.h"
- #include "Writer.h"
- #include "XML.h"
- namespace Framework
- {
- namespace JSON
- {
- enum class JSONType
- {
- NULL_,
- BOOLEAN,
- NUMBER,
- STRING,
- ARRAY,
- OBJECT
- };
- class JSONArray;
- class JSONObject;
- class JSONBool;
- class JSONNumber;
- class JSONString;
- class JSONValue : public virtual ReferenceCounter
- {
- private:
- JSONType type;
- protected:
- __declspec(dllexport) JSONValue(JSONType type);
- public:
- __declspec(dllexport) JSONValue();
- __declspec(dllexport) virtual ~JSONValue();
- __declspec(dllexport) JSONType getType() const;
- __declspec(dllexport) virtual Text toString() const;
- __declspec(dllexport) virtual JSONValue* clone() const;
- __declspec(dllexport) JSONBool* asBool() const;
- __declspec(dllexport) JSONNumber* asNumber() const;
- __declspec(dllexport) JSONString* asString() const;
- __declspec(dllexport) JSONArray* asArray() const;
- __declspec(dllexport) JSONObject* asObject() const;
- };
- class JSONBool : public JSONValue
- {
- private:
- bool b;
- public:
- __declspec(dllexport) JSONBool(bool b);
- __declspec(dllexport) bool getBool() const;
- __declspec(dllexport) Text toString() const override;
- __declspec(dllexport) JSONValue* clone() const override;
- };
- class JSONNumber : public JSONValue
- {
- private:
- double number;
- public:
- __declspec(dllexport) JSONNumber(double num);
- __declspec(dllexport) double getNumber() const;
- __declspec(dllexport) Text toString() const override;
- __declspec(dllexport) JSONValue* clone() const override;
- };
- class JSONString : public JSONValue
- {
- private:
- Text string;
- public:
- __declspec(dllexport) JSONString(Text string);
- __declspec(dllexport) Text getString() const;
- __declspec(dllexport) Text toString() const override;
- __declspec(dllexport) JSONValue* clone() const override;
- };
- class JSONArray : public JSONValue
- {
- private:
- RCArray<JSONValue>* array;
- public:
- __declspec(dllexport) JSONArray();
- __declspec(dllexport) JSONArray(Text string);
- __declspec(dllexport) JSONArray(const JSONArray& arr);
- __declspec(dllexport) ~JSONArray();
- __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
- __declspec(dllexport) void addValue(JSONValue* value);
- __declspec(dllexport) void setValue(int i, JSONValue* value);
- __declspec(dllexport) void removeValue(int i);
- __declspec(dllexport) JSONValue* getValue(int i) const;
- __declspec(dllexport) JSONValue* zValue(int i) const;
- __declspec(dllexport) int getLength() const;
- __declspec(dllexport) bool isValueOfType(
- int i, JSONType type) const;
- //! Gibt einen Iterator zurück.
- //! Mit ++ kann durch die Liste iteriert werden
- __declspec(dllexport) ArrayIterator<JSONValue*> begin() const;
- __declspec(dllexport) ArrayIterator<JSONValue*> end() const;
- template<class T>
- RCArray<T>* toRCArray(std::function<T*(JSONValue&)> map) const
- {
- return toRCArray([](JSONValue& v) { return 1; }, map);
- }
- template<class T>
- RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter,
- std::function<T*(JSONValue&)> map) const
- {
- RCArray<T>* result = new RCArray<T>();
- for (auto v : *array)
- {
- if (filter(*v))
- {
- result->add(map(*v));
- }
- }
- return result;
- }
- template<class T>
- Array<T>* toArray(std::function<T(JSONValue&)> map) const
- {
- return toArray([](JSONValue& v) { return 1; }, map);
- ;
- }
- template<class T>
- Array<T>* toArray(std::function<bool(JSONValue&)> filter,
- std::function<T(JSONValue&)> map) const
- {
- Array<T>* result = new Array<T>();
- for (auto v : *array)
- {
- if (filter(*v))
- {
- result->add(map(*v));
- }
- }
- return result;
- }
- __declspec(dllexport) Text toString() const override;
- __declspec(dllexport) JSONValue* clone() const override;
- template<class T> static JSONArray* fromRCArray(
- Framework::RCArray<T>& arr, std::function<JSONValue*(T&)> map)
- {
- JSONArray* array = new JSONArray();
- for (T* v : arr)
- {
- array->addValue(map(*v));
- }
- return array;
- }
- template<class T> static JSONArray* fromArray(
- Framework::Array<T>& arr, std::function<JSONValue*(T)> map)
- {
- JSONArray* array = new JSONArray();
- for (T v : arr)
- {
- array->addValue(map(v));
- }
- return array;
- }
- };
- class JSONObject : public JSONValue
- {
- private:
- Array<Text>* fields;
- RCArray<JSONValue>* values;
- public:
- __declspec(dllexport) JSONObject();
- __declspec(dllexport) JSONObject(Text string);
- __declspec(dllexport) JSONObject(const JSONObject& obj);
- __declspec(dllexport) ~JSONObject();
- __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
- __declspec(dllexport) bool addValue(Text field, JSONValue* value);
- __declspec(dllexport) bool removeValue(Text field);
- __declspec(dllexport) bool hasValue(Text field);
- __declspec(dllexport) JSONValue* getValue(Text field);
- __declspec(dllexport) JSONValue* zValue(Text field);
- __declspec(dllexport) ArrayIterator<Text> getFields();
- __declspec(dllexport) ArrayIterator<JSONValue*> getValues();
- __declspec(dllexport) int getFieldCount() const;
- __declspec(dllexport) bool isValueOfType(
- Text field, JSONType type) const;
- template<class T> T* parseTo(T* initialState,
- std::function<void(
- T* obj, Text fieldName, JSONValue& fieldValue)> parser)
- const
- {
- auto fieldsI = fields->begin();
- auto valuesI = values->begin();
- while (fieldsI && valuesI)
- {
- parser(initialState, fieldsI, *(JSONValue*)valuesI);
- fieldsI++;
- valuesI++;
- }
- return initialState;
- }
- __declspec(dllexport) Text toString() const override;
- __declspec(dllexport) JSONValue* clone() const override;
- };
- __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
- namespace Parser
- {
- __declspec(dllexport) int findObjectEndInArray(const char* str);
- __declspec(dllexport) Text removeWhitespace(const char* str);
- __declspec(dllexport) JSONValue* getValue(const char* str);
- __declspec(dllexport) int findFieldEndInObject(const char* str);
- __declspec(dllexport) int findValueEndInObject(const char* str);
- }; // namespace Parser
- namespace Validator
- {
- class JSONValidationResult : public Framework::ReferenceCounter
- {
- public:
- __declspec(dllexport) JSONValidationResult();
- __declspec(dllexport) virtual ~JSONValidationResult();
- virtual bool isValid() const = 0;
- __declspec(dllexport) void logInvalidInfo(
- std::source_location location
- = std::source_location::current()) const;
- virtual Text getInvalidInfo(int indent = 0) const = 0;
- virtual JSONValue* getValidPart(RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults)
- = 0;
- virtual Text getPath() const = 0;
- virtual void addBasePath(Text basePath) = 0;
- virtual bool isDifferent(
- const JSONValidationResult* zResult) 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) Text
- getInvalidInfo(int indent) const override;
- protected:
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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) Text
- getInvalidInfo(int indent) const override;
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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) Text
- getInvalidInfo(int indent) const override;
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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) Text
- getInvalidInfo(int indent) const override;
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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) Text
- getInvalidInfo(int indent) const override;
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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) Text
- getInvalidInfo(int indent) const override;
- __declspec(dllexport) JSONValue* getValidPart(
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) override;
- __declspec(dllexport) Text getPath() const override;
- __declspec(dllexport) bool isDifferent(
- const JSONValidationResult* zResult) const override;
- __declspec(dllexport) void addBasePath(Text basePath) 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;
- template<typename T> class OneOfValidationBuilder;
- class JSONValidator : public Framework::ReferenceCounter
- {
- private:
- XML::Element* constraints;
- Trie<XML::Element*> typeConstraints;
- 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,
- RCArray<JSONValidationResult>*
- zRemovedPartsValidationResults) 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();
- __declspec(dllexport) static OneOfValidationBuilder<
- JSONValidator>* buildForOneOf();
- __declspec(
- dllexport) static JSONValidator* buildForObjectReference(Text
- objectId);
- };
- template<typename T> class StringValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- StringValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<string/>"),
- builder(builder)
- {}
- StringValidationBuilder<T>* withExactMatch(Text value)
- {
- element.setAttribute("equals", value);
- return this;
- }
- StringValidationBuilder<T>* whichContainsMatch(Text value)
- {
- element.setAttribute("contains", value);
- return this;
- }
- StringValidationBuilder<T>* whichStartsWithMatch(Text value)
- {
- element.setAttribute("startsWith", value);
- return this;
- }
- StringValidationBuilder<T>* whichEndsWithMatch(Text value)
- {
- element.setAttribute("endsWith", value);
- return this;
- }
- StringValidationBuilder<T>* whichIsOneOf(RCArray<Text> values)
- {
- JSONArray arr;
- for (Text* str : values)
- arr.addValue(new JSONString(str->getText()));
- element.setAttribute("oneOf", arr.toString());
- return this;
- }
- StringValidationBuilder<T>* whichIsOneOf(
- std::initializer_list<Text> values)
- {
- JSONArray arr;
- for (Text str : values)
- arr.addValue(new JSONString(str));
- element.setAttribute("oneOf", arr.toString());
- return this;
- }
- StringValidationBuilder<T>* whichIs(Text value)
- {
- JSONArray arr;
- arr.addValue(new JSONString(value));
- element.setAttribute("oneOf", arr.toString());
- return this;
- }
- StringValidationBuilder<T>* withDefault(Text value)
- {
- element.setAttribute(
- "default", JSONString(value).toString());
- return this;
- }
- StringValidationBuilder<T>* withDefaultNull()
- {
- element.setAttribute("default", "null");
- return this;
- }
- StringValidationBuilder<T>* whichCanBeNull()
- {
- element.setAttribute("nullable", "true");
- return this;
- }
- StringValidationBuilder<T>* whichIsOptional()
- {
- element.setAttribute("optional", "true");
- return this;
- }
- T* finishString()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- template<typename T> class NumberValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- NumberValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<number/>"),
- builder(builder)
- {}
- NumberValidationBuilder<T>* whichIs(double value)
- {
- element.setAttribute("equals", Text(value));
- return this;
- }
- NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
- {
- element.setAttribute("lessOrEqual", Text(value));
- return this;
- }
- NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
- {
- element.setAttribute("greaterOrEqual", Text(value));
- return this;
- }
- NumberValidationBuilder<T>* whichIsLessThen(double value)
- {
- element.setAttribute("less", Text(value));
- return this;
- }
- NumberValidationBuilder<T>* whichIsGreaterThen(double value)
- {
- element.setAttribute("greater", Text(value));
- return this;
- }
- NumberValidationBuilder<T>* withDefault(double value)
- {
- element.setAttribute(
- "default", JSONNumber(value).toString());
- return this;
- }
- NumberValidationBuilder<T>* withDefaultNull()
- {
- element.setAttribute("default", "null");
- return this;
- }
- NumberValidationBuilder<T>* whichCanBeNull()
- {
- element.setAttribute("nullable", "true");
- return this;
- }
- NumberValidationBuilder<T>* whichIsOptional()
- {
- element.setAttribute("optional", "true");
- return this;
- }
- T* finishNumber()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- template<typename T> class BoolValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- BoolValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<bool/>"),
- builder(builder)
- {}
- BoolValidationBuilder<T>* whichIs(bool value)
- {
- element.setAttribute("equals", value ? "true" : "false");
- return this;
- }
- BoolValidationBuilder<T>* withDefault(bool value)
- {
- element.setAttribute("default", JSONBool(value).toString());
- return this;
- }
- BoolValidationBuilder<T>* withDefaultNull()
- {
- element.setAttribute("default", "null");
- return this;
- }
- BoolValidationBuilder<T>* whichCanBeNull()
- {
- element.setAttribute("nullable", "true");
- return this;
- }
- BoolValidationBuilder<T>* whichIsOptional()
- {
- element.setAttribute("optional", "true");
- return this;
- }
- T* finishBool()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- template<typename T> class ArrayValidationBuilder;
- template<typename T> class ObjectValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- ObjectValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<object></object>"),
- builder(builder)
- {}
- ObjectValidationBuilder<T>* setObjectReferenceId(Text id)
- {
- element.setAttribute("id", id);
- return this;
- }
- NumberValidationBuilder<ObjectValidationBuilder<T>>*
- withRequiredNumber(Text name)
- {
- return new NumberValidationBuilder<
- ObjectValidationBuilder<T>>(
- [this, name](XML::Element& e) {
- 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(e.dublicate());
- return this;
- });
- }
- StringValidationBuilder<ObjectValidationBuilder<T>>*
- withRequiredString(Text name)
- {
- return new StringValidationBuilder<
- ObjectValidationBuilder<T>>(
- [this, name](XML::Element& e) {
- 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(e.dublicate());
- return this;
- });
- }
- BoolValidationBuilder<ObjectValidationBuilder<T>>*
- withRequiredBool(Text name)
- {
- return new BoolValidationBuilder<
- ObjectValidationBuilder<T>>(
- [this, name](XML::Element& e) {
- 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(e.dublicate());
- return this;
- });
- }
- ArrayValidationBuilder<ObjectValidationBuilder<T>>*
- withRequiredArray(Text name)
- {
- return new ArrayValidationBuilder<
- ObjectValidationBuilder<T>>(
- [this, name](XML::Element& e) {
- 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(e.dublicate());
- return this;
- });
- }
- ObjectValidationBuilder<ObjectValidationBuilder<T>>*
- withRequiredObject(Text name)
- {
- return new ObjectValidationBuilder<
- ObjectValidationBuilder<T>>(
- [this, name](XML::Element& e) {
- 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(e.dublicate());
- return this;
- });
- }
- 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", obj->toString());
- obj->release();
- return this;
- }
- ObjectValidationBuilder<T>* withDefaultNull()
- {
- element.setAttribute("default", "null");
- return this;
- }
- ObjectValidationBuilder<T>* whichCanBeNull()
- {
- element.setAttribute("nullable", "true");
- return this;
- }
- ObjectValidationBuilder<T>* whichIsOptional()
- {
- element.setAttribute("optional", "true");
- return this;
- }
- ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
- Text valueName, Text attributeName)
- {
- if (!element.selectChildsByAttribute("name", valueName)
- .exists())
- {
- XML::Element* attr
- = new XML::Element("<value></value>");
- attr->setAttribute("name", valueName);
- element.addChild(attr);
- }
- element.selectChildsByAttribute("name", valueName)
- .setAttribute("typeSpecifiedBy", attributeName);
- return this;
- }
- ObjectValidationBuilder<T>* removeInvalidEntries()
- {
- element.setAttribute("removeInvalidEntries", "true");
- return this;
- }
- ObjectValidationBuilder<T>* allowAdditionalAttriutes()
- {
- element.setAttribute("allowAdditionalAttributes", "true");
- return this;
- }
- T* finishObject()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- template<typename T> class ArrayValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- ArrayValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<array></array>"),
- builder(builder)
- {}
- StringValidationBuilder<ArrayValidationBuilder<T>>*
- addAcceptedStringInArray()
- {
- return new StringValidationBuilder<
- ArrayValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- NumberValidationBuilder<ArrayValidationBuilder<T>>*
- addAcceptedNumberInArray()
- {
- return new NumberValidationBuilder<
- ArrayValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- BoolValidationBuilder<ArrayValidationBuilder<T>>*
- addAcceptedBooleanInArray()
- {
- return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
- [this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- ObjectValidationBuilder<ArrayValidationBuilder<T>>*
- addAcceptedObjectInArray()
- {
- return new ObjectValidationBuilder<
- ArrayValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- ArrayValidationBuilder<ArrayValidationBuilder<T>>*
- addAcceptedArrayInArray()
- {
- return new ArrayValidationBuilder<
- ArrayValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- 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* array)
- {
- element.setAttribute("default", array->toString());
- array->release();
- return this;
- }
- ArrayValidationBuilder<T>* withDefaultNull()
- {
- element.setAttribute("default", "null");
- return this;
- }
- ArrayValidationBuilder<T>* whichCanBeNull()
- {
- element.setAttribute("nullable", "true");
- return this;
- }
- ArrayValidationBuilder<T>* whichIsOptional()
- {
- element.setAttribute("optional", "true");
- return this;
- }
- ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
- {
- element.setAttribute("typeSpecifiedBy", name);
- return this;
- }
- ArrayValidationBuilder<T>* removeInvalidEntries()
- {
- element.setAttribute("removeInvalidEntries", "true");
- return this;
- }
- T* finishArray()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- template<typename T> class OneOfValidationBuilder
- {
- private:
- XML::Element element;
- std::function<T*(XML::Element& element)> builder;
- public:
- OneOfValidationBuilder(
- std::function<T*(XML::Element& element)> builder)
- : element("<oneOf></oneOf>"),
- builder(builder)
- {}
- StringValidationBuilder<OneOfValidationBuilder<T>>*
- addAcceptedStringInArray()
- {
- return new StringValidationBuilder<
- OneOfValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- NumberValidationBuilder<OneOfValidationBuilder<T>>*
- addAcceptedNumberInArray()
- {
- return new NumberValidationBuilder<
- OneOfValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- BoolValidationBuilder<OneOfValidationBuilder<T>>*
- addAcceptedBooleanInArray()
- {
- return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
- [this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- ObjectValidationBuilder<OneOfValidationBuilder<T>>*
- addAcceptedObjectInArray()
- {
- return new ObjectValidationBuilder<
- OneOfValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- ArrayValidationBuilder<OneOfValidationBuilder<T>>*
- addAcceptedArrayInArray()
- {
- return new ArrayValidationBuilder<
- OneOfValidationBuilder<T>>([this](XML::Element& e) {
- element.addChild(e.dublicate());
- return this;
- });
- }
- OneOfValidationBuilder<T>* addAcceptedType(
- JSONValidator* validator)
- {
- element.addChild(validator->zConstraints()->dublicate());
- validator->release();
- return this;
- }
- OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
- {
- element.setAttribute("typeSpecifiedBy", name);
- return this;
- }
- T* finishOneOf()
- {
- T* result = builder(element);
- delete this;
- return result;
- }
- };
- } // namespace Validator
- } // namespace JSON
- } // namespace Framework
|