DataValidator.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. #include "DataValidator.h"
  2. #include "Logging.h"
  3. using namespace Framework;
  4. using namespace Validator;
  5. #pragma region ValidationResult
  6. ValidationResult::ValidationResult()
  7. : ReferenceCounter()
  8. {}
  9. ValidationResult::~ValidationResult() {}
  10. void ValidationResult::logInvalidInfo(std::source_location location) const
  11. {
  12. Logging::error(location) << getInvalidInfo(0).getText();
  13. }
  14. #pragma region TypeMissmatch
  15. Framework::Validator::TypeMissmatch::TypeMissmatch(Text path,
  16. AbstractElement* zFoundValue,
  17. XML::Element* expected,
  18. ValidationResult* reason)
  19. : ValidationResult()
  20. {
  21. this->path = path;
  22. this->foundValue = foundValue;
  23. this->expected = expected;
  24. this->reason = reason;
  25. }
  26. TypeMissmatch::~TypeMissmatch()
  27. {
  28. expected->release();
  29. if (reason) reason->release();
  30. }
  31. bool TypeMissmatch::isValid() const
  32. {
  33. return 0;
  34. }
  35. Text TypeMissmatch::getInvalidInfo(int indent) const
  36. {
  37. Text ind = "";
  38. ind.fillText(' ', indent);
  39. Text error = "";
  40. error.append() << ind.getText() << "Type missmatch at path '"
  41. << path.getText() << "'. Value\n"
  42. << ind.getText() << foundValue->toString().getText() << "\n"
  43. << ind.getText() << "does not match expected type\n";
  44. if (reason)
  45. {
  46. error.append() << ind.getText() << "Reason for type mismatch:\n"
  47. << reason->getInvalidInfo(indent + 4);
  48. }
  49. else
  50. {
  51. error.append() << ind.getText() << expected->toString().getText()
  52. << "\n";
  53. }
  54. return error;
  55. }
  56. JSON::JSONValue* TypeMissmatch::getValidPart(
  57. RCArray<ValidationResult>* removedPartsValidationResults)
  58. {
  59. if (reason)
  60. {
  61. RCArray<ValidationResult> temporaryInvalidParts;
  62. JSON::JSONValue* valid = reason->getValidPart(&temporaryInvalidParts);
  63. Text* p = reason->getPath().getTeilText(path.getLength());
  64. if (foundValue->getType() == AbstractType::ARRAY)
  65. {
  66. if (!valid
  67. && (!expected->hasAttribute("removeInvalidEntries")
  68. || !expected->getAttributeValue("removeInvalidEntries")
  69. .istGleich("true")))
  70. {
  71. if (removedPartsValidationResults)
  72. {
  73. if (temporaryInvalidParts.getEintragAnzahl() == 1)
  74. {
  75. if (reason) reason->release();
  76. reason = temporaryInvalidParts.get(0);
  77. }
  78. else
  79. {
  80. for (ValidationResult* res : temporaryInvalidParts)
  81. {
  82. if (res->isDifferent(this))
  83. {
  84. removedPartsValidationResults->add(
  85. dynamic_cast<ValidationResult*>(
  86. res->getThis()));
  87. }
  88. }
  89. }
  90. removedPartsValidationResults->add(
  91. dynamic_cast<ValidationResult*>(getThis()));
  92. }
  93. p->release();
  94. return 0;
  95. }
  96. if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
  97. {
  98. Text* it = p->getTeilText(1, p->getLength() - 1);
  99. int i = (int)*it;
  100. it->release();
  101. if (i >= 0 && foundValue->asAbstractArray()->getLength() > i)
  102. {
  103. if (removedPartsValidationResults)
  104. {
  105. for (ValidationResult* res : temporaryInvalidParts)
  106. {
  107. removedPartsValidationResults->add(
  108. dynamic_cast<ValidationResult*>(
  109. res->getThis()));
  110. }
  111. }
  112. JSON::JSONValue* foundValueJson
  113. = dynamic_cast<JSON::JSONValue*>(foundValue);
  114. if (foundValueJson)
  115. {
  116. JSON::JSONValue* tmp = foundValueJson->clone();
  117. if (valid)
  118. tmp->asArray()->setValue(
  119. i, dynamic_cast<JSON::JSONValue*>(valid));
  120. else
  121. tmp->asArray()->removeValue(i);
  122. ValidationResult* res = DataValidator(
  123. dynamic_cast<XML::Element*>(expected->getThis()))
  124. .validate(tmp);
  125. res->addBasePath(path);
  126. if (res->isValid())
  127. {
  128. res->release();
  129. p->release();
  130. return tmp;
  131. }
  132. else if (res->isDifferent(this) || !valid)
  133. {
  134. p->release();
  135. tmp->release();
  136. JSON::JSONValue* result = res->getValidPart(
  137. removedPartsValidationResults);
  138. res->release();
  139. return result;
  140. }
  141. tmp->release();
  142. res->release();
  143. }
  144. }
  145. }
  146. }
  147. else if (foundValue->getType() == AbstractType::OBJECT)
  148. {
  149. if (!valid
  150. && (!expected->hasAttribute("removeInvalidEntries")
  151. || !expected->getAttributeValue("removeInvalidEntries")
  152. .istGleich("true")))
  153. {
  154. if (removedPartsValidationResults)
  155. {
  156. if (temporaryInvalidParts.getEintragAnzahl() == 1)
  157. {
  158. if (reason) reason->release();
  159. reason = temporaryInvalidParts.get(0);
  160. }
  161. else
  162. {
  163. for (ValidationResult* res : temporaryInvalidParts)
  164. {
  165. if (res->isDifferent(this))
  166. {
  167. removedPartsValidationResults->add(
  168. dynamic_cast<ValidationResult*>(
  169. res->getThis()));
  170. }
  171. }
  172. }
  173. removedPartsValidationResults->add(
  174. dynamic_cast<ValidationResult*>(getThis()));
  175. }
  176. p->release();
  177. return 0;
  178. }
  179. if (p->hatAt(0, "."))
  180. {
  181. if (removedPartsValidationResults)
  182. {
  183. for (ValidationResult* res : temporaryInvalidParts)
  184. {
  185. removedPartsValidationResults->add(
  186. dynamic_cast<ValidationResult*>(res->getThis()));
  187. }
  188. }
  189. Text* at = p->getTeilText(1);
  190. Text attr = *at;
  191. at->release();
  192. JSON::JSONValue* foundValueJson
  193. = dynamic_cast<JSON::JSONValue*>(foundValue);
  194. if (foundValueJson)
  195. {
  196. JSON::JSONValue* tmp = foundValueJson->clone();
  197. tmp->asObject()->removeValue(attr);
  198. if (valid)
  199. tmp->asObject()->addValue(
  200. attr, dynamic_cast<JSON::JSONValue*>(valid));
  201. ValidationResult* res = DataValidator(
  202. dynamic_cast<XML::Element*>(expected->getThis()))
  203. .validate(tmp);
  204. res->addBasePath(path);
  205. if (res->isValid())
  206. {
  207. res->release();
  208. p->release();
  209. return tmp;
  210. }
  211. else if (res->isDifferent(this) || !valid)
  212. {
  213. p->release();
  214. tmp->release();
  215. JSON::JSONValue* result
  216. = res->getValidPart(removedPartsValidationResults);
  217. res->release();
  218. return result;
  219. }
  220. tmp->release();
  221. res->release();
  222. }
  223. }
  224. }
  225. p->release();
  226. if (valid) valid->release();
  227. }
  228. if (removedPartsValidationResults)
  229. {
  230. removedPartsValidationResults->add(
  231. dynamic_cast<ValidationResult*>(getThis()));
  232. }
  233. return 0;
  234. }
  235. Text TypeMissmatch::getPath() const
  236. {
  237. return path;
  238. }
  239. bool TypeMissmatch::isDifferent(const ValidationResult* zResult) const
  240. {
  241. const TypeMissmatch* casted = dynamic_cast<const TypeMissmatch*>(zResult);
  242. if (casted == 0) return 1;
  243. if (!casted->getPath().istGleich(path)) return 1;
  244. return reason->isDifferent(casted->reason);
  245. }
  246. void TypeMissmatch::addBasePath(Text basePath)
  247. {
  248. path = basePath + path;
  249. if (reason) reason->addBasePath(basePath);
  250. }
  251. #pragma endregion Cotent
  252. #pragma region UnknownValue
  253. UnknownValue::UnknownValue(Text path, AbstractElement* zFoundValue)
  254. : ValidationResult()
  255. {
  256. this->path = path;
  257. this->foundValue = foundValue;
  258. }
  259. UnknownValue::~UnknownValue() {}
  260. bool UnknownValue::isValid() const
  261. {
  262. return 0;
  263. }
  264. Text UnknownValue::getInvalidInfo(int indent) const
  265. {
  266. Text ind = "";
  267. ind.fillText(' ', indent);
  268. Text error = "";
  269. error.append() << ind.getText() << "Unknown Value at '" << path.getText()
  270. << "'. Value found:\n"
  271. << ind.getText() << foundValue->toString().getText() << "\n";
  272. return error;
  273. }
  274. JSON::JSONValue* UnknownValue::getValidPart(
  275. RCArray<ValidationResult>* removedPartsValidationResults)
  276. {
  277. if (removedPartsValidationResults)
  278. {
  279. removedPartsValidationResults->add(
  280. dynamic_cast<ValidationResult*>(getThis()));
  281. }
  282. return 0;
  283. }
  284. Text UnknownValue::getPath() const
  285. {
  286. return path;
  287. }
  288. bool UnknownValue::isDifferent(const ValidationResult* zResult) const
  289. {
  290. const UnknownValue* casted = dynamic_cast<const UnknownValue*>(zResult);
  291. if (casted == 0) return 1;
  292. if (!casted->getPath().istGleich(path)) return 1;
  293. return 0;
  294. }
  295. void UnknownValue::addBasePath(Text basePath)
  296. {
  297. path = basePath + path;
  298. }
  299. #pragma endregion Cotent
  300. #pragma region MissingValue
  301. MissingValue::MissingValue(Text path, XML::Element* expected)
  302. : ValidationResult()
  303. {
  304. this->path = path;
  305. this->expected = expected;
  306. }
  307. MissingValue::~MissingValue()
  308. {
  309. expected->release();
  310. }
  311. bool MissingValue::isValid() const
  312. {
  313. return 0;
  314. }
  315. Text MissingValue::getInvalidInfo(int indent) const
  316. {
  317. Text ind = "";
  318. ind.fillText(' ', indent);
  319. Text error = "";
  320. error.append() << ind.getText() << "Missing Value at '" << path.getText()
  321. << "'. Expected type:\n"
  322. << ind.getText() << expected->toString().getText() << "\n";
  323. return error;
  324. }
  325. JSON::JSONValue* MissingValue::getValidPart(
  326. RCArray<ValidationResult>* removedPartsValidationResults)
  327. {
  328. if (expected->hasAttribute("default"))
  329. {
  330. JSON::JSONValue* def
  331. = JSON::Parser::getValue(expected->getAttributeValue("default"));
  332. ValidationResult* res
  333. = DataValidator(dynamic_cast<XML::Element*>(expected->getThis()))
  334. .validate(def);
  335. res->addBasePath(path);
  336. if (res->isValid())
  337. {
  338. res->release();
  339. return def;
  340. }
  341. else if (res->isDifferent(this))
  342. {
  343. def->release();
  344. JSON::JSONValue* result
  345. = res->getValidPart(removedPartsValidationResults);
  346. res->release();
  347. return result;
  348. }
  349. def->release();
  350. }
  351. if (removedPartsValidationResults)
  352. {
  353. removedPartsValidationResults->add(
  354. dynamic_cast<ValidationResult*>(getThis()));
  355. }
  356. return 0;
  357. }
  358. Text MissingValue::getPath() const
  359. {
  360. return path;
  361. }
  362. bool MissingValue::isDifferent(const ValidationResult* zResult) const
  363. {
  364. const MissingValue* casted = dynamic_cast<const MissingValue*>(zResult);
  365. if (casted == 0) return 1;
  366. if (!casted->getPath().istGleich(path)) return 1;
  367. return 0;
  368. }
  369. void MissingValue::addBasePath(Text basePath)
  370. {
  371. path = basePath + path;
  372. }
  373. #pragma endregion Cotent
  374. #pragma region MissingOneOf
  375. MissingOneOf::MissingOneOf(Text path, XML::Editor expected)
  376. : ValidationResult()
  377. {
  378. this->path = path;
  379. for (XML::Element* e : expected)
  380. this->expected.add(dynamic_cast<XML::Element*>(e->getThis()));
  381. }
  382. MissingOneOf::~MissingOneOf() {}
  383. bool MissingOneOf::isValid() const
  384. {
  385. return 0;
  386. }
  387. Text MissingOneOf::getInvalidInfo(int indent) const
  388. {
  389. Text ind = "";
  390. ind.fillText(' ', indent);
  391. Text error = "";
  392. error.append() << ind.getText() << "Missing Value at '" << path.getText()
  393. << "'. The value should have one of the specified types:\n";
  394. ind += " ";
  395. for (XML::Element* e : expected)
  396. {
  397. error.append() << ind.getText() << e->toString().getText() << "\n";
  398. }
  399. return error;
  400. }
  401. JSON::JSONValue* MissingOneOf::getValidPart(
  402. RCArray<ValidationResult>* removedPartsValidationResults)
  403. {
  404. for (XML::Element* e : expected)
  405. {
  406. if (e->hasAttribute("default"))
  407. {
  408. JSON::JSONValue* defaultValue
  409. = JSON::Parser::getValue(e->getAttributeValue("default"));
  410. if (defaultValue)
  411. {
  412. JSON::JSONValue* valid
  413. = DataValidator(dynamic_cast<XML::Element*>(e->getThis()))
  414. .getValidParts(
  415. defaultValue, removedPartsValidationResults);
  416. defaultValue->release();
  417. if (valid)
  418. {
  419. return valid;
  420. }
  421. }
  422. }
  423. }
  424. if (removedPartsValidationResults)
  425. {
  426. removedPartsValidationResults->add(
  427. dynamic_cast<ValidationResult*>(getThis()));
  428. }
  429. // multiple possibilities are undecidable
  430. return 0;
  431. }
  432. Text MissingOneOf::getPath() const
  433. {
  434. return path;
  435. }
  436. bool MissingOneOf::isDifferent(const ValidationResult* zResult) const
  437. {
  438. const MissingOneOf* casted = dynamic_cast<const MissingOneOf*>(zResult);
  439. if (casted == 0) return 1;
  440. if (!casted->getPath().istGleich(path)) return 1;
  441. return 0;
  442. }
  443. void MissingOneOf::addBasePath(Text basePath)
  444. {
  445. path = basePath + path;
  446. }
  447. #pragma endregion Content
  448. #pragma region NoTypeMatching
  449. NoTypeMatching::NoTypeMatching(Text path,
  450. AbstractElement* zFoundValue,
  451. RCArray<XML::Element>& expected,
  452. RCArray<ValidationResult>& reasons)
  453. : ValidationResult()
  454. {
  455. this->path = path;
  456. this->foundValue = foundValue;
  457. this->expected = expected;
  458. this->reasons = reasons;
  459. }
  460. NoTypeMatching::~NoTypeMatching() {}
  461. bool NoTypeMatching::isValid() const
  462. {
  463. return 0;
  464. }
  465. Text NoTypeMatching::getInvalidInfo(int indent) const
  466. {
  467. Text ind = "";
  468. ind.fillText(' ', indent);
  469. Text error = "";
  470. error.append() << ind.getText() << "Found Value at '" << path.getText()
  471. << "' did not match any of the specified types\n";
  472. Text ind2 = ind + " ";
  473. error.append() << ind.getText() << "Reasons:\n";
  474. for (ValidationResult* reason : reasons)
  475. {
  476. error += reason->getInvalidInfo(indent + 4);
  477. }
  478. return error;
  479. }
  480. JSON::JSONValue* NoTypeMatching::getValidPart(
  481. RCArray<ValidationResult>* removedPartsValidationResults)
  482. {
  483. RCArray<ValidationResult> tempErrors;
  484. for (ValidationResult* reason : reasons)
  485. {
  486. JSON::JSONValue* result = reason->getValidPart(&tempErrors);
  487. if (result)
  488. {
  489. return result;
  490. }
  491. }
  492. if (removedPartsValidationResults)
  493. {
  494. removedPartsValidationResults->add(
  495. dynamic_cast<ValidationResult*>(getThis()));
  496. reasons.leeren();
  497. for (ValidationResult* error : tempErrors)
  498. {
  499. reasons.add(dynamic_cast<ValidationResult*>(error->getThis()));
  500. }
  501. }
  502. // multiple possibilities are undecidable
  503. return 0;
  504. }
  505. Text NoTypeMatching::getPath() const
  506. {
  507. return path;
  508. }
  509. bool NoTypeMatching::isDifferent(const ValidationResult* zResult) const
  510. {
  511. const NoTypeMatching* casted = dynamic_cast<const NoTypeMatching*>(zResult);
  512. if (casted == 0) return 1;
  513. if (!casted->getPath().istGleich(path)) return 1;
  514. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  515. {
  516. if (reasons.z(i)->isDifferent(casted->reasons.z(i))) return 1;
  517. }
  518. return 0;
  519. }
  520. void NoTypeMatching::addBasePath(Text basePath)
  521. {
  522. path = basePath + path;
  523. for (ValidationResult* reason : reasons)
  524. {
  525. reason->addBasePath(basePath);
  526. }
  527. }
  528. #pragma endregion Content
  529. #pragma region ValidationPathNotFound
  530. ValidationPathNotFound::ValidationPathNotFound(
  531. Text path, AbstractElement* zFoundValue, Text validationPath)
  532. : ValidationResult()
  533. {
  534. this->path = path;
  535. this->foundValue = foundValue;
  536. this->validationPath = validationPath;
  537. }
  538. ValidationPathNotFound::~ValidationPathNotFound() {}
  539. bool ValidationPathNotFound::isValid() const
  540. {
  541. return false;
  542. }
  543. Text ValidationPathNotFound::getInvalidInfo(int indent) const
  544. {
  545. Text result;
  546. result.append() << "Expected to validate path '" << validationPath.getText()
  547. << "' but at path '" << path.getText() << "' the value "
  548. << foundValue->toString().getText() << " was found.";
  549. return result;
  550. }
  551. JSON::JSONValue* ValidationPathNotFound::getValidPart(
  552. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  553. {
  554. return 0;
  555. }
  556. Text ValidationPathNotFound::getPath() const
  557. {
  558. return path;
  559. }
  560. bool ValidationPathNotFound::isDifferent(const ValidationResult* zResult) const
  561. {
  562. const ValidationPathNotFound* other
  563. = dynamic_cast<const ValidationPathNotFound*>(zResult);
  564. if (other == 0) return 1;
  565. if (!other->path.istGleich(path)) return 1;
  566. return 0;
  567. }
  568. void ValidationPathNotFound::addBasePath(Text basePath)
  569. {
  570. path = basePath + path;
  571. }
  572. #pragma endregion Content
  573. #pragma region ValidValue
  574. ValidValue::ValidValue(Text path, AbstractElement* zValue)
  575. : ValidationResult()
  576. {
  577. this->path = path;
  578. this->value = zValue;
  579. }
  580. ValidValue::~ValidValue() {}
  581. bool ValidValue::isValid() const
  582. {
  583. return 1;
  584. }
  585. Text ValidValue::getInvalidInfo(int indent) const
  586. {
  587. return "";
  588. }
  589. JSON::JSONValue* ValidValue::getValidPart(
  590. RCArray<ValidationResult>* removedPartsValidationResults)
  591. {
  592. JSON::JSONValue* json = dynamic_cast<JSON::JSONValue*>(value);
  593. return json ? json->clone() : 0;
  594. }
  595. Text ValidValue::getPath() const
  596. {
  597. return path;
  598. }
  599. bool ValidValue::isDifferent(const ValidationResult* zResult) const
  600. {
  601. const ValidValue* casted = dynamic_cast<const ValidValue*>(zResult);
  602. if (casted == 0) return 1;
  603. if (!casted->getPath().istGleich(path)) return 1;
  604. return 0;
  605. }
  606. void ValidValue::addBasePath(Text basePath)
  607. {
  608. path = basePath + path;
  609. }
  610. #pragma endregion Content
  611. #pragma endregion Content
  612. #pragma region DataValidator
  613. DataValidator::DataValidator(XML::Element* constraints)
  614. : ReferenceCounter(),
  615. constraints(constraints),
  616. typeConstraints(new RCTrie<XML::Element>())
  617. {
  618. for (XML::Element* e : constraints->select()
  619. .selectAllElements()
  620. .whereNameEquals("object")
  621. .whereAttributeExists("id"))
  622. {
  623. Framework::Text id = e->getAttributeValue("id");
  624. typeConstraints->set(
  625. id, id.getLength(), dynamic_cast<XML::Element*>(e->getThis()));
  626. }
  627. }
  628. DataValidator::DataValidator(
  629. XML::Element* constraints, RCTrie<XML::Element>* typeConstraints)
  630. : ReferenceCounter(),
  631. constraints(constraints),
  632. typeConstraints(typeConstraints)
  633. {}
  634. DataValidator::~DataValidator()
  635. {
  636. constraints->release();
  637. typeConstraints->release();
  638. }
  639. ValidationResult* DataValidator::validate(AbstractElement* zValue) const
  640. {
  641. return validate(ElementPathBuilder().build(), zValue);
  642. }
  643. ValidationResult* Framework::Validator::DataValidator::validate(
  644. ElementPath* path, AbstractElement* zValue) const
  645. {
  646. ValidationResult* result = validate(path, zValue, constraints, "");
  647. path->release();
  648. return result;
  649. }
  650. bool DataValidator::isValid(AbstractElement* zValue) const
  651. {
  652. ValidationResult* res = validate(zValue);
  653. if (res->isValid())
  654. {
  655. res->release();
  656. return 1;
  657. }
  658. res->release();
  659. return 0;
  660. }
  661. JSON::JSONValue* DataValidator::getValidParts(JSON::JSONValue* zValue,
  662. RCArray<ValidationResult>* removedPartsValidationResults) const
  663. {
  664. ValidationResult* res = validate(zValue);
  665. if (res->isValid())
  666. {
  667. res->release();
  668. return zValue->clone();
  669. }
  670. JSON::JSONValue* valid = res->getValidPart(removedPartsValidationResults);
  671. res->release();
  672. return valid;
  673. }
  674. XML::Element* DataValidator::zConstraints()
  675. {
  676. return constraints;
  677. }
  678. ValidationResult* DataValidator::validate(ElementPath* pathToValidate,
  679. AbstractElement* zValue,
  680. XML::Element* zConstraints,
  681. Text path) const
  682. {
  683. if (zConstraints->getName().istGleich("oneOf"))
  684. {
  685. return validateMultipleTypes(
  686. pathToValidate, zValue, zConstraints, path);
  687. }
  688. switch (zValue->getType())
  689. {
  690. case AbstractType::NULL_:
  691. if (pathToValidate->isValid())
  692. {
  693. return new ValidationPathNotFound(
  694. path, zValue, pathToValidate->toString());
  695. }
  696. if (!zConstraints->hasAttribute("nullable")
  697. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  698. {
  699. return new TypeMissmatch(path,
  700. zValue,
  701. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  702. 0);
  703. }
  704. break;
  705. case AbstractType::BOOLEAN:
  706. if (pathToValidate->isValid())
  707. {
  708. return new ValidationPathNotFound(
  709. path, zValue, pathToValidate->toString());
  710. }
  711. if (!zConstraints->getName().istGleich("bool"))
  712. {
  713. return new TypeMissmatch(path,
  714. zValue,
  715. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  716. 0);
  717. }
  718. else if (zConstraints->hasAttribute("equals"))
  719. {
  720. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  721. == !zValue->asAbstractBool()->getBool())
  722. {
  723. return new TypeMissmatch(path,
  724. zValue,
  725. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  726. 0);
  727. }
  728. }
  729. break;
  730. case AbstractType::NUMBER:
  731. if (pathToValidate->isValid())
  732. {
  733. return new ValidationPathNotFound(
  734. path, zValue, pathToValidate->toString());
  735. }
  736. if (!zConstraints->getName().istGleich("number"))
  737. {
  738. return new TypeMissmatch(path,
  739. zValue,
  740. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  741. 0);
  742. }
  743. else
  744. {
  745. double number = zValue->asAbstractNumber()->getNumber();
  746. if (zConstraints->hasAttribute("equals")
  747. && (double)zConstraints->getAttributeValue("equals") != number)
  748. {
  749. return new TypeMissmatch(path,
  750. zValue,
  751. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  752. 0);
  753. }
  754. if (zConstraints->hasAttribute("lessOrEqual")
  755. && number
  756. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  757. {
  758. return new TypeMissmatch(path,
  759. zValue,
  760. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  761. 0);
  762. }
  763. if (zConstraints->hasAttribute("greaterOrEqual")
  764. && number < (double)zConstraints->getAttributeValue(
  765. "greaterOrEqual"))
  766. {
  767. return new TypeMissmatch(path,
  768. zValue,
  769. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  770. 0);
  771. }
  772. if (zConstraints->hasAttribute("less")
  773. && number >= (double)zConstraints->getAttributeValue("less"))
  774. {
  775. return new TypeMissmatch(path,
  776. zValue,
  777. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  778. 0);
  779. }
  780. if (zConstraints->hasAttribute("greater")
  781. && number <= (double)zConstraints->getAttributeValue("greater"))
  782. {
  783. return new TypeMissmatch(path,
  784. zValue,
  785. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  786. 0);
  787. }
  788. }
  789. break;
  790. case AbstractType::STRING:
  791. if (pathToValidate->isValid())
  792. {
  793. return new ValidationPathNotFound(
  794. path, zValue, pathToValidate->toString());
  795. }
  796. if (!zConstraints->getName().istGleich("string"))
  797. {
  798. return new TypeMissmatch(path,
  799. zValue,
  800. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  801. 0);
  802. }
  803. else
  804. {
  805. Text string = zValue->asAbstractString()->getString();
  806. if (zConstraints->hasAttribute("equals")
  807. && !zConstraints->getAttributeValue("equals").istGleich(string))
  808. {
  809. return new TypeMissmatch(path,
  810. zValue,
  811. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  812. 0);
  813. }
  814. if (zConstraints->hasAttribute("contains")
  815. && string.hat(
  816. zConstraints->getAttributeValue("contains").getText()))
  817. {
  818. return new TypeMissmatch(path,
  819. zValue,
  820. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  821. 0);
  822. }
  823. if (zConstraints->hasAttribute("startsWith")
  824. && string.positionVon(
  825. zConstraints->getAttributeValue("startsWith").getText())
  826. == 0)
  827. {
  828. return new TypeMissmatch(path,
  829. zValue,
  830. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  831. 0);
  832. }
  833. if (zConstraints->hasAttribute("endsWith")
  834. && string.positionVon(
  835. zConstraints->getAttributeValue("endsWith").getText())
  836. == string.getLength()
  837. - zConstraints->getAttributeValue("endsWith")
  838. .getLength())
  839. {
  840. return new TypeMissmatch(path,
  841. zValue,
  842. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  843. 0);
  844. }
  845. if (zConstraints->hasAttribute("oneOf"))
  846. {
  847. JSON::JSONArray* array = JSON::Parser::getValue(
  848. zConstraints->getAttributeValue("oneOf"))
  849. ->asArray();
  850. bool ok = 0;
  851. for (JSON::JSONValue* v : *array)
  852. {
  853. if (v->asString()->getString().istGleich(string))
  854. {
  855. ok = 1;
  856. break;
  857. }
  858. }
  859. array->release();
  860. if (!ok)
  861. {
  862. return new TypeMissmatch(path,
  863. zValue,
  864. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  865. 0);
  866. }
  867. }
  868. }
  869. break;
  870. case AbstractType::ARRAY:
  871. if (!zConstraints->getName().istGleich("array"))
  872. {
  873. return new TypeMissmatch(path,
  874. zValue,
  875. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  876. 0);
  877. }
  878. else
  879. {
  880. if (pathToValidate->isValid())
  881. {
  882. if (!pathToValidate->isArrayElement())
  883. {
  884. return new ValidationPathNotFound(
  885. path, zValue, pathToValidate->toString());
  886. }
  887. else
  888. {
  889. int index = pathToValidate->getArrayIndex();
  890. const AbstractArray* array = zValue->asAbstractArray();
  891. if (index >= array->getLength())
  892. {
  893. return new ValidationPathNotFound(
  894. path, zValue, pathToValidate->toString());
  895. }
  896. Text p = path;
  897. p += "[";
  898. p += index;
  899. p += "]";
  900. pathToValidate->next();
  901. return validateMultipleTypes(pathToValidate,
  902. array->zAbstractValue(index),
  903. zConstraints,
  904. p);
  905. }
  906. }
  907. const AbstractArray* array = zValue->asAbstractArray();
  908. for (int i = 0; i < array->getLength(); i++)
  909. {
  910. Text p = path;
  911. p += "[";
  912. p += i;
  913. p += "]";
  914. ValidationResult* res = validateMultipleTypes(
  915. pathToValidate, array->zAbstractValue(i), zConstraints, p);
  916. if (!res->isValid())
  917. {
  918. return new TypeMissmatch(path,
  919. zValue,
  920. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  921. res);
  922. }
  923. res->release();
  924. }
  925. }
  926. break;
  927. case AbstractType::OBJECT:
  928. if (zConstraints->getName().istGleich("objectRef"))
  929. {
  930. Text id = zConstraints->getAttributeValue("ref");
  931. XML::Element* e = typeConstraints->z(id, id.getLength());
  932. if (e)
  933. {
  934. zConstraints = e;
  935. }
  936. }
  937. if (!zConstraints->getName().istGleich("object"))
  938. {
  939. return new TypeMissmatch(path,
  940. zValue,
  941. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  942. 0);
  943. }
  944. else
  945. {
  946. if (pathToValidate->isValid())
  947. {
  948. if (!pathToValidate->isObjectAttribute())
  949. {
  950. return new ValidationPathNotFound(
  951. path, zValue, pathToValidate->toString());
  952. }
  953. else
  954. {
  955. Text key = pathToValidate->getObjectAttribute();
  956. const AbstractObject* obj = zValue->asAbstractObject();
  957. if (!obj->hasValue(key))
  958. {
  959. return new ValidationPathNotFound(
  960. path, zValue, pathToValidate->toString());
  961. }
  962. Text p = path;
  963. p += ".";
  964. p += key;
  965. if (!zConstraints->selectChildsByAttribute("name", key)
  966. .exists())
  967. {
  968. if (!zConstraints
  969. ->getAttributeValue(
  970. "allowAdditionalAttributes")
  971. .istGleich("true"))
  972. {
  973. return new TypeMissmatch(path,
  974. zValue,
  975. dynamic_cast<XML::Element*>(
  976. zConstraints->getThis()),
  977. new UnknownValue(p, obj->zAbstractValue(key)));
  978. }
  979. }
  980. else
  981. {
  982. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  983. "name", key);
  984. pathToValidate->next();
  985. return validateMultipleTypes(pathToValidate,
  986. obj->zAbstractValue(key),
  987. tmp.begin().val(),
  988. p);
  989. }
  990. }
  991. }
  992. const AbstractObject* obj = zValue->asAbstractObject();
  993. for (int i = 0; i < obj->getFieldCount(); i++)
  994. {
  995. Text key = obj->getFieldKey(i);
  996. Text p = path;
  997. p += ".";
  998. p += key;
  999. if (!zConstraints->selectChildsByAttribute("name", key)
  1000. .exists())
  1001. {
  1002. if (!zConstraints
  1003. ->getAttributeValue("allowAdditionalAttributes")
  1004. .istGleich("true"))
  1005. {
  1006. return new TypeMissmatch(path,
  1007. zValue,
  1008. dynamic_cast<XML::Element*>(
  1009. zConstraints->getThis()),
  1010. new UnknownValue(p, obj->zAbstractValue(i)));
  1011. }
  1012. }
  1013. else
  1014. {
  1015. XML::Editor tmp
  1016. = zConstraints->selectChildsByAttribute("name", key);
  1017. ValidationResult* res
  1018. = validateMultipleTypes(pathToValidate,
  1019. obj->zAbstractValue(i),
  1020. tmp.begin().val(),
  1021. p);
  1022. if (!res->isValid())
  1023. {
  1024. return new TypeMissmatch(path,
  1025. zValue,
  1026. dynamic_cast<XML::Element*>(
  1027. zConstraints->getThis()),
  1028. res);
  1029. }
  1030. res->release();
  1031. }
  1032. }
  1033. for (XML::Element* constraint : zConstraints->selectChildren())
  1034. {
  1035. if (!obj->hasValue(constraint->getAttributeValue("name")))
  1036. {
  1037. XML::Editor tmp = constraint->selectChildren();
  1038. bool optional = true;
  1039. std::function<void(XML::Element*)> checkOptional;
  1040. checkOptional = [&optional, &checkOptional](
  1041. XML::Element* zElement) {
  1042. if (zElement->getName().istGleich("oneOf"))
  1043. {
  1044. XML::Editor tmp = zElement->selectChildren();
  1045. tmp.forEach(checkOptional);
  1046. }
  1047. else
  1048. {
  1049. optional &= zElement->hasAttribute("optional")
  1050. && zElement->getAttributeValue("optional")
  1051. .istGleich("true");
  1052. }
  1053. };
  1054. tmp.forEach(checkOptional);
  1055. if (!optional)
  1056. {
  1057. Text p = path;
  1058. p += ".";
  1059. p += constraint->getAttributeValue("name");
  1060. if (constraint->getChildCount() != 1)
  1061. return new TypeMissmatch(path,
  1062. zValue,
  1063. dynamic_cast<XML::Element*>(
  1064. zConstraints->getThis()),
  1065. new MissingOneOf(p, tmp));
  1066. return new TypeMissmatch(path,
  1067. zValue,
  1068. dynamic_cast<XML::Element*>(
  1069. zConstraints->getThis()),
  1070. new MissingValue(p,
  1071. dynamic_cast<XML::Element*>(
  1072. tmp.begin()->getThis())));
  1073. }
  1074. }
  1075. }
  1076. }
  1077. break;
  1078. }
  1079. return new ValidValue(path, zValue);
  1080. }
  1081. ValidationResult* DataValidator::validateMultipleTypes(
  1082. ElementPath* pathToValidate,
  1083. AbstractElement* zChildValue,
  1084. XML::Element* zPossibleChildConstraints,
  1085. Text childPath) const
  1086. {
  1087. if (zPossibleChildConstraints->getChildCount() == 1
  1088. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1089. {
  1090. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1091. return validate(pathToValidate,
  1092. zChildValue,
  1093. children.begin().val(),
  1094. childPath); // only one type is possible
  1095. }
  1096. bool hasTypeAttr = 0;
  1097. RCArray<XML::Element> possibleConstraints;
  1098. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1099. { // try to find the correct constraints based on the type attribute
  1100. hasTypeAttr = 1;
  1101. Text typeAttr
  1102. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1103. if (zChildValue->getType() == AbstractType::OBJECT)
  1104. {
  1105. const AbstractObject* obj = zChildValue->asAbstractObject();
  1106. if (obj->hasValue(typeAttr))
  1107. {
  1108. AbstractElement* typeV = obj->zAbstractValue(typeAttr);
  1109. ElementPath* tmp = ElementPathBuilder().build();
  1110. for (XML::Element* constraint :
  1111. zPossibleChildConstraints->selectChildsByName("object")
  1112. .whereChildWithAttributeExists("name", typeAttr))
  1113. {
  1114. XML::Editor nameChildren
  1115. = constraint->selectChildsByAttribute("name", typeAttr);
  1116. XML::Element* typeAttrContraints
  1117. = nameChildren.begin().val();
  1118. ValidationResult* res = validateMultipleTypes(tmp,
  1119. typeV,
  1120. typeAttrContraints,
  1121. childPath + "." + typeAttr);
  1122. if (res->isValid())
  1123. possibleConstraints.add(
  1124. dynamic_cast<XML::Element*>(constraint->getThis()));
  1125. res->release();
  1126. }
  1127. tmp->release();
  1128. }
  1129. }
  1130. }
  1131. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1132. return validate(pathToValidate,
  1133. zChildValue,
  1134. possibleConstraints.begin().val(),
  1135. childPath); // if the type is clear
  1136. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1137. { // more then one type is possible
  1138. RCArray<ValidationResult> invalidResults;
  1139. int index = pathToValidate->getCurrentPathElementIndex();
  1140. for (XML::Element* constraint : possibleConstraints)
  1141. {
  1142. pathToValidate->setCurrentPathElementIndex(index);
  1143. ValidationResult* res
  1144. = validate(pathToValidate, zChildValue, constraint, childPath);
  1145. invalidResults.add(res);
  1146. if (res->isValid()) return new ValidValue(childPath, zChildValue);
  1147. }
  1148. return new NoTypeMatching(
  1149. childPath, zChildValue, possibleConstraints, invalidResults);
  1150. }
  1151. // try all types
  1152. possibleConstraints.leeren();
  1153. RCArray<ValidationResult> invalidResults;
  1154. int index = pathToValidate->getCurrentPathElementIndex();
  1155. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1156. {
  1157. pathToValidate->setCurrentPathElementIndex(index);
  1158. ValidationResult* res
  1159. = validate(pathToValidate, zChildValue, constraint, childPath);
  1160. invalidResults.add(res);
  1161. if (res->isValid()) return new ValidValue(childPath, zChildValue);
  1162. possibleConstraints.add(
  1163. dynamic_cast<XML::Element*>(constraint->getThis()));
  1164. }
  1165. pathToValidate->setCurrentPathElementIndex(index);
  1166. if (pathToValidate->isValid())
  1167. {
  1168. return new ValidationPathNotFound(
  1169. childPath, zChildValue, pathToValidate->toString());
  1170. }
  1171. return new NoTypeMatching(
  1172. childPath, zChildValue, possibleConstraints, invalidResults);
  1173. }
  1174. StringValidationBuilder<DataValidator>* DataValidator::buildForString()
  1175. {
  1176. return new StringValidationBuilder<DataValidator>(
  1177. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1178. }
  1179. NumberValidationBuilder<DataValidator>* DataValidator::buildForNumber()
  1180. {
  1181. return new NumberValidationBuilder<DataValidator>(
  1182. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1183. }
  1184. BoolValidationBuilder<DataValidator>* DataValidator::buildForBool()
  1185. {
  1186. return new BoolValidationBuilder<DataValidator>(
  1187. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1188. }
  1189. ObjectValidationBuilder<DataValidator>* DataValidator::buildForObject()
  1190. {
  1191. return new ObjectValidationBuilder<DataValidator>(
  1192. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1193. }
  1194. ArrayValidationBuilder<DataValidator>* DataValidator::buildForArray()
  1195. {
  1196. return new ArrayValidationBuilder<DataValidator>(
  1197. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1198. }
  1199. DataValidator* DataValidator::buildForObjectReference(Text objectId)
  1200. {
  1201. return new DataValidator(
  1202. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1203. }
  1204. OneOfValidationBuilder<DataValidator>* DataValidator::buildForOneOf()
  1205. {
  1206. return new OneOfValidationBuilder<DataValidator>(
  1207. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1208. }