1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303 |
- #include "DataValidator.h"
- #include "Logging.h"
- using namespace Framework;
- using namespace Validator;
- #pragma region ValidationResult
- ValidationResult::ValidationResult()
- : ReferenceCounter()
- {}
- ValidationResult::~ValidationResult() {}
- void ValidationResult::logInvalidInfo(std::source_location location) const
- {
- Logging::error(location) << getInvalidInfo(0).getText();
- }
- #pragma region TypeMissmatch
- Framework::Validator::TypeMissmatch::TypeMissmatch(Text path,
- AbstractElement* zFoundValue,
- XML::Element* expected,
- ValidationResult* reason)
- : ValidationResult()
- {
- this->path = path;
- this->foundValue = foundValue;
- this->expected = expected;
- this->reason = reason;
- }
- TypeMissmatch::~TypeMissmatch()
- {
- expected->release();
- if (reason) reason->release();
- }
- bool TypeMissmatch::isValid() const
- {
- return 0;
- }
- Text TypeMissmatch::getInvalidInfo(int indent) const
- {
- Text ind = "";
- ind.fillText(' ', indent);
- Text error = "";
- error.append() << ind.getText() << "Type missmatch at path '"
- << path.getText() << "'. Value\n"
- << ind.getText() << foundValue->toString().getText() << "\n"
- << ind.getText() << "does not match expected type\n";
- if (reason)
- {
- error.append() << ind.getText() << "Reason for type mismatch:\n"
- << reason->getInvalidInfo(indent + 4);
- }
- else
- {
- error.append() << ind.getText() << expected->toString().getText()
- << "\n";
- }
- return error;
- }
- JSON::JSONValue* TypeMissmatch::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- if (reason)
- {
- RCArray<ValidationResult> temporaryInvalidParts;
- JSON::JSONValue* valid = reason->getValidPart(&temporaryInvalidParts);
- Text* p = reason->getPath().getTeilText(path.getLength());
- if (foundValue->getType() == AbstractType::ARRAY)
- {
- if (!valid
- && (!expected->hasAttribute("removeInvalidEntries")
- || !expected->getAttributeValue("removeInvalidEntries")
- .istGleich("true")))
- {
- if (removedPartsValidationResults)
- {
- if (temporaryInvalidParts.getEintragAnzahl() == 1)
- {
- if (reason) reason->release();
- reason = temporaryInvalidParts.get(0);
- }
- else
- {
- for (ValidationResult* res : temporaryInvalidParts)
- {
- if (res->isDifferent(this))
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(
- res->getThis()));
- }
- }
- }
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- p->release();
- return 0;
- }
- if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
- {
- Text* it = p->getTeilText(1, p->getLength() - 1);
- int i = (int)*it;
- it->release();
- if (i >= 0 && foundValue->asAbstractArray()->getLength() > i)
- {
- if (removedPartsValidationResults)
- {
- for (ValidationResult* res : temporaryInvalidParts)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(
- res->getThis()));
- }
- }
- JSON::JSONValue* foundValueJson
- = dynamic_cast<JSON::JSONValue*>(foundValue);
- if (foundValueJson)
- {
- JSON::JSONValue* tmp = foundValueJson->clone();
- if (valid)
- tmp->asArray()->setValue(
- i, dynamic_cast<JSON::JSONValue*>(valid));
- else
- tmp->asArray()->removeValue(i);
- ValidationResult* res = DataValidator(
- dynamic_cast<XML::Element*>(expected->getThis()))
- .validate(tmp);
- res->addBasePath(path);
- if (res->isValid())
- {
- res->release();
- p->release();
- return tmp;
- }
- else if (res->isDifferent(this) || !valid)
- {
- p->release();
- tmp->release();
- JSON::JSONValue* result = res->getValidPart(
- removedPartsValidationResults);
- res->release();
- return result;
- }
- tmp->release();
- res->release();
- }
- }
- }
- }
- else if (foundValue->getType() == AbstractType::OBJECT)
- {
- if (!valid
- && (!expected->hasAttribute("removeInvalidEntries")
- || !expected->getAttributeValue("removeInvalidEntries")
- .istGleich("true")))
- {
- if (removedPartsValidationResults)
- {
- if (temporaryInvalidParts.getEintragAnzahl() == 1)
- {
- if (reason) reason->release();
- reason = temporaryInvalidParts.get(0);
- }
- else
- {
- for (ValidationResult* res : temporaryInvalidParts)
- {
- if (res->isDifferent(this))
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(
- res->getThis()));
- }
- }
- }
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- p->release();
- return 0;
- }
- if (p->hatAt(0, "."))
- {
- if (removedPartsValidationResults)
- {
- for (ValidationResult* res : temporaryInvalidParts)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(res->getThis()));
- }
- }
- Text* at = p->getTeilText(1);
- Text attr = *at;
- at->release();
- JSON::JSONValue* foundValueJson
- = dynamic_cast<JSON::JSONValue*>(foundValue);
- if (foundValueJson)
- {
- JSON::JSONValue* tmp = foundValueJson->clone();
- tmp->asObject()->removeValue(attr);
- if (valid)
- tmp->asObject()->addValue(
- attr, dynamic_cast<JSON::JSONValue*>(valid));
- ValidationResult* res = DataValidator(
- dynamic_cast<XML::Element*>(expected->getThis()))
- .validate(tmp);
- res->addBasePath(path);
- if (res->isValid())
- {
- res->release();
- p->release();
- return tmp;
- }
- else if (res->isDifferent(this) || !valid)
- {
- p->release();
- tmp->release();
- JSON::JSONValue* result
- = res->getValidPart(removedPartsValidationResults);
- res->release();
- return result;
- }
- tmp->release();
- res->release();
- }
- }
- }
- p->release();
- if (valid) valid->release();
- }
- if (removedPartsValidationResults)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- return 0;
- }
- Text TypeMissmatch::getPath() const
- {
- return path;
- }
- bool TypeMissmatch::isDifferent(const ValidationResult* zResult) const
- {
- const TypeMissmatch* casted = dynamic_cast<const TypeMissmatch*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- return reason->isDifferent(casted->reason);
- }
- void TypeMissmatch::addBasePath(Text basePath)
- {
- path = basePath + path;
- if (reason) reason->addBasePath(basePath);
- }
- #pragma endregion Cotent
- #pragma region UnknownValue
- UnknownValue::UnknownValue(Text path, AbstractElement* zFoundValue)
- : ValidationResult()
- {
- this->path = path;
- this->foundValue = foundValue;
- }
- UnknownValue::~UnknownValue() {}
- bool UnknownValue::isValid() const
- {
- return 0;
- }
- Text UnknownValue::getInvalidInfo(int indent) const
- {
- Text ind = "";
- ind.fillText(' ', indent);
- Text error = "";
- error.append() << ind.getText() << "Unknown Value at '" << path.getText()
- << "'. Value found:\n"
- << ind.getText() << foundValue->toString().getText() << "\n";
- return error;
- }
- JSON::JSONValue* UnknownValue::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- if (removedPartsValidationResults)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- return 0;
- }
- Text UnknownValue::getPath() const
- {
- return path;
- }
- bool UnknownValue::isDifferent(const ValidationResult* zResult) const
- {
- const UnknownValue* casted = dynamic_cast<const UnknownValue*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- return 0;
- }
- void UnknownValue::addBasePath(Text basePath)
- {
- path = basePath + path;
- }
- #pragma endregion Cotent
- #pragma region MissingValue
- MissingValue::MissingValue(Text path, XML::Element* expected)
- : ValidationResult()
- {
- this->path = path;
- this->expected = expected;
- }
- MissingValue::~MissingValue()
- {
- expected->release();
- }
- bool MissingValue::isValid() const
- {
- return 0;
- }
- Text MissingValue::getInvalidInfo(int indent) const
- {
- Text ind = "";
- ind.fillText(' ', indent);
- Text error = "";
- error.append() << ind.getText() << "Missing Value at '" << path.getText()
- << "'. Expected type:\n"
- << ind.getText() << expected->toString().getText() << "\n";
- return error;
- }
- JSON::JSONValue* MissingValue::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- if (expected->hasAttribute("default"))
- {
- JSON::JSONValue* def
- = JSON::Parser::getValue(expected->getAttributeValue("default"));
- ValidationResult* res
- = DataValidator(dynamic_cast<XML::Element*>(expected->getThis()))
- .validate(def);
- res->addBasePath(path);
- if (res->isValid())
- {
- res->release();
- return def;
- }
- else if (res->isDifferent(this))
- {
- def->release();
- JSON::JSONValue* result
- = res->getValidPart(removedPartsValidationResults);
- res->release();
- return result;
- }
- def->release();
- }
- if (removedPartsValidationResults)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- return 0;
- }
- Text MissingValue::getPath() const
- {
- return path;
- }
- bool MissingValue::isDifferent(const ValidationResult* zResult) const
- {
- const MissingValue* casted = dynamic_cast<const MissingValue*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- return 0;
- }
- void MissingValue::addBasePath(Text basePath)
- {
- path = basePath + path;
- }
- #pragma endregion Cotent
- #pragma region MissingOneOf
- MissingOneOf::MissingOneOf(Text path, XML::Editor expected)
- : ValidationResult()
- {
- this->path = path;
- for (XML::Element* e : expected)
- this->expected.add(dynamic_cast<XML::Element*>(e->getThis()));
- }
- MissingOneOf::~MissingOneOf() {}
- bool MissingOneOf::isValid() const
- {
- return 0;
- }
- Text MissingOneOf::getInvalidInfo(int indent) const
- {
- Text ind = "";
- ind.fillText(' ', indent);
- Text error = "";
- error.append() << ind.getText() << "Missing Value at '" << path.getText()
- << "'. The value should have one of the specified types:\n";
- ind += " ";
- for (XML::Element* e : expected)
- {
- error.append() << ind.getText() << e->toString().getText() << "\n";
- }
- return error;
- }
- JSON::JSONValue* MissingOneOf::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- for (XML::Element* e : expected)
- {
- if (e->hasAttribute("default"))
- {
- JSON::JSONValue* defaultValue
- = JSON::Parser::getValue(e->getAttributeValue("default"));
- if (defaultValue)
- {
- JSON::JSONValue* valid
- = DataValidator(dynamic_cast<XML::Element*>(e->getThis()))
- .getValidParts(
- defaultValue, removedPartsValidationResults);
- defaultValue->release();
- if (valid)
- {
- return valid;
- }
- }
- }
- }
- if (removedPartsValidationResults)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- }
- // multiple possibilities are undecidable
- return 0;
- }
- Text MissingOneOf::getPath() const
- {
- return path;
- }
- bool MissingOneOf::isDifferent(const ValidationResult* zResult) const
- {
- const MissingOneOf* casted = dynamic_cast<const MissingOneOf*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- return 0;
- }
- void MissingOneOf::addBasePath(Text basePath)
- {
- path = basePath + path;
- }
- #pragma endregion Content
- #pragma region NoTypeMatching
- NoTypeMatching::NoTypeMatching(Text path,
- AbstractElement* zFoundValue,
- RCArray<XML::Element>& expected,
- RCArray<ValidationResult>& reasons)
- : ValidationResult()
- {
- this->path = path;
- this->foundValue = foundValue;
- this->expected = expected;
- this->reasons = reasons;
- }
- NoTypeMatching::~NoTypeMatching() {}
- bool NoTypeMatching::isValid() const
- {
- return 0;
- }
- Text NoTypeMatching::getInvalidInfo(int indent) const
- {
- Text ind = "";
- ind.fillText(' ', indent);
- Text error = "";
- error.append() << ind.getText() << "Found Value at '" << path.getText()
- << "' did not match any of the specified types\n";
- Text ind2 = ind + " ";
- error.append() << ind.getText() << "Reasons:\n";
- for (ValidationResult* reason : reasons)
- {
- error += reason->getInvalidInfo(indent + 4);
- }
- return error;
- }
- JSON::JSONValue* NoTypeMatching::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- RCArray<ValidationResult> tempErrors;
- for (ValidationResult* reason : reasons)
- {
- JSON::JSONValue* result = reason->getValidPart(&tempErrors);
- if (result)
- {
- return result;
- }
- }
- if (removedPartsValidationResults)
- {
- removedPartsValidationResults->add(
- dynamic_cast<ValidationResult*>(getThis()));
- reasons.leeren();
- for (ValidationResult* error : tempErrors)
- {
- reasons.add(dynamic_cast<ValidationResult*>(error->getThis()));
- }
- }
- // multiple possibilities are undecidable
- return 0;
- }
- Text NoTypeMatching::getPath() const
- {
- return path;
- }
- bool NoTypeMatching::isDifferent(const ValidationResult* zResult) const
- {
- const NoTypeMatching* casted = dynamic_cast<const NoTypeMatching*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- for (int i = 0; i < reasons.getEintragAnzahl(); i++)
- {
- if (reasons.z(i)->isDifferent(casted->reasons.z(i))) return 1;
- }
- return 0;
- }
- void NoTypeMatching::addBasePath(Text basePath)
- {
- path = basePath + path;
- for (ValidationResult* reason : reasons)
- {
- reason->addBasePath(basePath);
- }
- }
- #pragma endregion Content
- #pragma region ValidationPathNotFound
- ValidationPathNotFound::ValidationPathNotFound(
- Text path, AbstractElement* zFoundValue, Text validationPath)
- : ValidationResult()
- {
- this->path = path;
- this->foundValue = foundValue;
- this->validationPath = validationPath;
- }
- ValidationPathNotFound::~ValidationPathNotFound() {}
- bool ValidationPathNotFound::isValid() const
- {
- return false;
- }
- Text ValidationPathNotFound::getInvalidInfo(int indent) const
- {
- Text result;
- result.append() << "Expected to validate path '" << validationPath.getText()
- << "' but at path '" << path.getText() << "' the value "
- << foundValue->toString().getText() << " was found.";
- return result;
- }
- JSON::JSONValue* ValidationPathNotFound::getValidPart(
- RCArray<ValidationResult>* zRemovedPartsValidationResults)
- {
- return 0;
- }
- Text ValidationPathNotFound::getPath() const
- {
- return path;
- }
- bool ValidationPathNotFound::isDifferent(const ValidationResult* zResult) const
- {
- const ValidationPathNotFound* other
- = dynamic_cast<const ValidationPathNotFound*>(zResult);
- if (other == 0) return 1;
- if (!other->path.istGleich(path)) return 1;
- return 0;
- }
- void ValidationPathNotFound::addBasePath(Text basePath)
- {
- path = basePath + path;
- }
- #pragma endregion Content
- #pragma region ValidValue
- ValidValue::ValidValue(Text path, AbstractElement* zValue)
- : ValidationResult()
- {
- this->path = path;
- this->value = zValue;
- }
- ValidValue::~ValidValue() {}
- bool ValidValue::isValid() const
- {
- return 1;
- }
- Text ValidValue::getInvalidInfo(int indent) const
- {
- return "";
- }
- JSON::JSONValue* ValidValue::getValidPart(
- RCArray<ValidationResult>* removedPartsValidationResults)
- {
- JSON::JSONValue* json = dynamic_cast<JSON::JSONValue*>(value);
- return json ? json->clone() : 0;
- }
- Text ValidValue::getPath() const
- {
- return path;
- }
- bool ValidValue::isDifferent(const ValidationResult* zResult) const
- {
- const ValidValue* casted = dynamic_cast<const ValidValue*>(zResult);
- if (casted == 0) return 1;
- if (!casted->getPath().istGleich(path)) return 1;
- return 0;
- }
- void ValidValue::addBasePath(Text basePath)
- {
- path = basePath + path;
- }
- #pragma endregion Content
- #pragma endregion Content
- #pragma region DataValidator
- DataValidator::DataValidator(XML::Element* constraints)
- : ReferenceCounter(),
- constraints(constraints),
- typeConstraints(new RCTrie<XML::Element>())
- {
- for (XML::Element* e : constraints->select()
- .selectAllElements()
- .whereNameEquals("object")
- .whereAttributeExists("id"))
- {
- Framework::Text id = e->getAttributeValue("id");
- typeConstraints->set(
- id, id.getLength(), dynamic_cast<XML::Element*>(e->getThis()));
- }
- }
- DataValidator::DataValidator(
- XML::Element* constraints, RCTrie<XML::Element>* typeConstraints)
- : ReferenceCounter(),
- constraints(constraints),
- typeConstraints(typeConstraints)
- {}
- DataValidator::~DataValidator()
- {
- constraints->release();
- typeConstraints->release();
- }
- ValidationResult* DataValidator::validate(AbstractElement* zValue) const
- {
- return validate(ElementPathBuilder().build(), zValue);
- }
- ValidationResult* Framework::Validator::DataValidator::validate(
- ElementPath* path, AbstractElement* zValue) const
- {
- ValidationResult* result = validate(path, zValue, constraints, "");
- path->release();
- return result;
- }
- bool DataValidator::isValid(AbstractElement* zValue) const
- {
- ValidationResult* res = validate(zValue);
- if (res->isValid())
- {
- res->release();
- return 1;
- }
- res->release();
- return 0;
- }
- JSON::JSONValue* DataValidator::getValidParts(JSON::JSONValue* zValue,
- RCArray<ValidationResult>* removedPartsValidationResults) const
- {
- ValidationResult* res = validate(zValue);
- if (res->isValid())
- {
- res->release();
- return zValue->clone();
- }
- JSON::JSONValue* valid = res->getValidPart(removedPartsValidationResults);
- res->release();
- return valid;
- }
- XML::Element* DataValidator::zConstraints()
- {
- return constraints;
- }
- ValidationResult* DataValidator::validate(ElementPath* pathToValidate,
- AbstractElement* zValue,
- XML::Element* zConstraints,
- Text path) const
- {
- if (zConstraints->getName().istGleich("oneOf"))
- {
- return validateMultipleTypes(
- pathToValidate, zValue, zConstraints, path);
- }
- switch (zValue->getType())
- {
- case AbstractType::NULL_:
- if (pathToValidate->isValid())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- if (!zConstraints->hasAttribute("nullable")
- || !zConstraints->getAttributeValue("nullable").istGleich("true"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- break;
- case AbstractType::BOOLEAN:
- if (pathToValidate->isValid())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- if (!zConstraints->getName().istGleich("bool"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- else if (zConstraints->hasAttribute("equals"))
- {
- if (!zConstraints->getAttributeValue("equals").istGleich("true")
- == !zValue->asAbstractBool()->getBool())
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- }
- break;
- case AbstractType::NUMBER:
- if (pathToValidate->isValid())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- if (!zConstraints->getName().istGleich("number"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- else
- {
- double number = zValue->asAbstractNumber()->getNumber();
- if (zConstraints->hasAttribute("equals")
- && (double)zConstraints->getAttributeValue("equals") != number)
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("lessOrEqual")
- && number
- > (double)zConstraints->getAttributeValue("lessOrEqual"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("greaterOrEqual")
- && number < (double)zConstraints->getAttributeValue(
- "greaterOrEqual"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("less")
- && number >= (double)zConstraints->getAttributeValue("less"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("greater")
- && number <= (double)zConstraints->getAttributeValue("greater"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- }
- break;
- case AbstractType::STRING:
- if (pathToValidate->isValid())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- if (!zConstraints->getName().istGleich("string"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- else
- {
- Text string = zValue->asAbstractString()->getString();
- if (zConstraints->hasAttribute("equals")
- && !zConstraints->getAttributeValue("equals").istGleich(string))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("contains")
- && string.hat(
- zConstraints->getAttributeValue("contains").getText()))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("startsWith")
- && string.positionVon(
- zConstraints->getAttributeValue("startsWith").getText())
- == 0)
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("endsWith")
- && string.positionVon(
- zConstraints->getAttributeValue("endsWith").getText())
- == string.getLength()
- - zConstraints->getAttributeValue("endsWith")
- .getLength())
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- if (zConstraints->hasAttribute("oneOf"))
- {
- JSON::JSONArray* array = JSON::Parser::getValue(
- zConstraints->getAttributeValue("oneOf"))
- ->asArray();
- bool ok = 0;
- for (JSON::JSONValue* v : *array)
- {
- if (v->asString()->getString().istGleich(string))
- {
- ok = 1;
- break;
- }
- }
- array->release();
- if (!ok)
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- }
- }
- break;
- case AbstractType::ARRAY:
- if (!zConstraints->getName().istGleich("array"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- else
- {
- if (pathToValidate->isValid())
- {
- if (!pathToValidate->isArrayElement())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- else
- {
- int index = pathToValidate->getArrayIndex();
- const AbstractArray* array = zValue->asAbstractArray();
- if (index >= array->getLength())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- Text p = path;
- p += "[";
- p += index;
- p += "]";
- pathToValidate->next();
- return validateMultipleTypes(pathToValidate,
- array->zAbstractValue(index),
- zConstraints,
- p);
- }
- }
- const AbstractArray* array = zValue->asAbstractArray();
- for (int i = 0; i < array->getLength(); i++)
- {
- Text p = path;
- p += "[";
- p += i;
- p += "]";
- ValidationResult* res = validateMultipleTypes(
- pathToValidate, array->zAbstractValue(i), zConstraints, p);
- if (!res->isValid())
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- res);
- }
- res->release();
- }
- }
- break;
- case AbstractType::OBJECT:
- if (zConstraints->getName().istGleich("objectRef"))
- {
- Text id = zConstraints->getAttributeValue("ref");
- XML::Element* e = typeConstraints->z(id, id.getLength());
- if (e)
- {
- zConstraints = e;
- }
- }
- if (!zConstraints->getName().istGleich("object"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(zConstraints->getThis()),
- 0);
- }
- else
- {
- if (pathToValidate->isValid())
- {
- if (!pathToValidate->isObjectAttribute())
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- else
- {
- Text key = pathToValidate->getObjectAttribute();
- const AbstractObject* obj = zValue->asAbstractObject();
- if (!obj->hasValue(key))
- {
- return new ValidationPathNotFound(
- path, zValue, pathToValidate->toString());
- }
- Text p = path;
- p += ".";
- p += key;
- if (!zConstraints->selectChildsByAttribute("name", key)
- .exists())
- {
- if (!zConstraints
- ->getAttributeValue(
- "allowAdditionalAttributes")
- .istGleich("true"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(
- zConstraints->getThis()),
- new UnknownValue(p, obj->zAbstractValue(key)));
- }
- }
- else
- {
- XML::Editor tmp = zConstraints->selectChildsByAttribute(
- "name", key);
- pathToValidate->next();
- return validateMultipleTypes(pathToValidate,
- obj->zAbstractValue(key),
- tmp.begin().val(),
- p);
- }
- }
- }
- const AbstractObject* obj = zValue->asAbstractObject();
- for (int i = 0; i < obj->getFieldCount(); i++)
- {
- Text key = obj->getFieldKey(i);
- Text p = path;
- p += ".";
- p += key;
- if (!zConstraints->selectChildsByAttribute("name", key)
- .exists())
- {
- if (!zConstraints
- ->getAttributeValue("allowAdditionalAttributes")
- .istGleich("true"))
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(
- zConstraints->getThis()),
- new UnknownValue(p, obj->zAbstractValue(i)));
- }
- }
- else
- {
- XML::Editor tmp
- = zConstraints->selectChildsByAttribute("name", key);
- ValidationResult* res
- = validateMultipleTypes(pathToValidate,
- obj->zAbstractValue(i),
- tmp.begin().val(),
- p);
- if (!res->isValid())
- {
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(
- zConstraints->getThis()),
- res);
- }
- res->release();
- }
- }
- for (XML::Element* constraint : zConstraints->selectChildren())
- {
- if (!obj->hasValue(constraint->getAttributeValue("name")))
- {
- XML::Editor tmp = constraint->selectChildren();
- bool optional = true;
- std::function<void(XML::Element*)> checkOptional;
- checkOptional = [&optional, &checkOptional](
- XML::Element* zElement) {
- if (zElement->getName().istGleich("oneOf"))
- {
- XML::Editor tmp = zElement->selectChildren();
- tmp.forEach(checkOptional);
- }
- else
- {
- optional &= zElement->hasAttribute("optional")
- && zElement->getAttributeValue("optional")
- .istGleich("true");
- }
- };
- tmp.forEach(checkOptional);
- if (!optional)
- {
- Text p = path;
- p += ".";
- p += constraint->getAttributeValue("name");
- if (constraint->getChildCount() != 1)
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(
- zConstraints->getThis()),
- new MissingOneOf(p, tmp));
- return new TypeMissmatch(path,
- zValue,
- dynamic_cast<XML::Element*>(
- zConstraints->getThis()),
- new MissingValue(p,
- dynamic_cast<XML::Element*>(
- tmp.begin()->getThis())));
- }
- }
- }
- }
- break;
- }
- return new ValidValue(path, zValue);
- }
- ValidationResult* DataValidator::validateMultipleTypes(
- ElementPath* pathToValidate,
- AbstractElement* zChildValue,
- XML::Element* zPossibleChildConstraints,
- Text childPath) const
- {
- if (zPossibleChildConstraints->getChildCount() == 1
- && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
- {
- XML::Editor children = zPossibleChildConstraints->selectChildren();
- return validate(pathToValidate,
- zChildValue,
- children.begin().val(),
- childPath); // only one type is possible
- }
- bool hasTypeAttr = 0;
- RCArray<XML::Element> possibleConstraints;
- if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
- { // try to find the correct constraints based on the type attribute
- hasTypeAttr = 1;
- Text typeAttr
- = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
- if (zChildValue->getType() == AbstractType::OBJECT)
- {
- const AbstractObject* obj = zChildValue->asAbstractObject();
- if (obj->hasValue(typeAttr))
- {
- AbstractElement* typeV = obj->zAbstractValue(typeAttr);
- ElementPath* tmp = ElementPathBuilder().build();
- for (XML::Element* constraint :
- zPossibleChildConstraints->selectChildsByName("object")
- .whereChildWithAttributeExists("name", typeAttr))
- {
- XML::Editor nameChildren
- = constraint->selectChildsByAttribute("name", typeAttr);
- XML::Element* typeAttrContraints
- = nameChildren.begin().val();
- ValidationResult* res = validateMultipleTypes(tmp,
- typeV,
- typeAttrContraints,
- childPath + "." + typeAttr);
- if (res->isValid())
- possibleConstraints.add(
- dynamic_cast<XML::Element*>(constraint->getThis()));
- res->release();
- }
- tmp->release();
- }
- }
- }
- if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
- return validate(pathToValidate,
- zChildValue,
- possibleConstraints.begin().val(),
- childPath); // if the type is clear
- else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
- { // more then one type is possible
- RCArray<ValidationResult> invalidResults;
- int index = pathToValidate->getCurrentPathElementIndex();
- for (XML::Element* constraint : possibleConstraints)
- {
- pathToValidate->setCurrentPathElementIndex(index);
- ValidationResult* res
- = validate(pathToValidate, zChildValue, constraint, childPath);
- invalidResults.add(res);
- if (res->isValid()) return new ValidValue(childPath, zChildValue);
- }
- return new NoTypeMatching(
- childPath, zChildValue, possibleConstraints, invalidResults);
- }
- // try all types
- possibleConstraints.leeren();
- RCArray<ValidationResult> invalidResults;
- int index = pathToValidate->getCurrentPathElementIndex();
- for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
- {
- pathToValidate->setCurrentPathElementIndex(index);
- ValidationResult* res
- = validate(pathToValidate, zChildValue, constraint, childPath);
- invalidResults.add(res);
- if (res->isValid()) return new ValidValue(childPath, zChildValue);
- possibleConstraints.add(
- dynamic_cast<XML::Element*>(constraint->getThis()));
- }
- pathToValidate->setCurrentPathElementIndex(index);
- if (pathToValidate->isValid())
- {
- return new ValidationPathNotFound(
- childPath, zChildValue, pathToValidate->toString());
- }
- return new NoTypeMatching(
- childPath, zChildValue, possibleConstraints, invalidResults);
- }
- StringValidationBuilder<DataValidator>* DataValidator::buildForString()
- {
- return new StringValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
- NumberValidationBuilder<DataValidator>* DataValidator::buildForNumber()
- {
- return new NumberValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
- BoolValidationBuilder<DataValidator>* DataValidator::buildForBool()
- {
- return new BoolValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
- ObjectValidationBuilder<DataValidator>* DataValidator::buildForObject()
- {
- return new ObjectValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
- ArrayValidationBuilder<DataValidator>* DataValidator::buildForArray()
- {
- return new ArrayValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
- DataValidator* DataValidator::buildForObjectReference(Text objectId)
- {
- return new DataValidator(
- new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
- }
- OneOfValidationBuilder<DataValidator>* DataValidator::buildForOneOf()
- {
- return new OneOfValidationBuilder<DataValidator>(
- [](XML::Element& e) { return new DataValidator(e.dublicate()); });
- }
|