DataValidator.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. #include "DataValidator.h"
  2. #include "Logging.h"
  3. #include "Regex.h"
  4. using namespace Framework;
  5. using namespace Validator;
  6. #pragma region ValidationResult
  7. ValidationResult::ValidationResult()
  8. : ReferenceCounter()
  9. {}
  10. ValidationResult::~ValidationResult() {}
  11. void ValidationResult::logInvalidInfo(std::source_location location) const
  12. {
  13. Logging::error(location) << getInvalidInfo(0).getText();
  14. }
  15. #pragma region TypeMissmatch
  16. Framework::Validator::TypeMissmatch::TypeMissmatch(Text path,
  17. AbstractElement* foundValue,
  18. XML::Element* expected,
  19. ValidationResult* reason)
  20. : ValidationResult()
  21. {
  22. this->path = path;
  23. this->foundValue = foundValue;
  24. this->expected = expected;
  25. this->reason = reason;
  26. }
  27. TypeMissmatch::~TypeMissmatch()
  28. {
  29. expected->release();
  30. if (reason) reason->release();
  31. foundValue->release();
  32. }
  33. bool TypeMissmatch::isValid() const
  34. {
  35. return 0;
  36. }
  37. Text TypeMissmatch::getInvalidInfo(int indent) const
  38. {
  39. Text ind = "";
  40. ind.fillText(' ', indent);
  41. Text error = "";
  42. error.append() << ind.getText() << "Type missmatch at path '"
  43. << path.getText() << "'. Value\n"
  44. << ind.getText() << foundValue->toString().getText() << "\n"
  45. << ind.getText() << "does not match expected type\n";
  46. if (reason)
  47. {
  48. error.append() << ind.getText() << "Reason for type mismatch:\n"
  49. << reason->getInvalidInfo(indent + 4);
  50. }
  51. else
  52. {
  53. error.append() << ind.getText() << expected->toString().getText()
  54. << "\n";
  55. }
  56. return error;
  57. }
  58. JSON::JSONValue* TypeMissmatch::getValidPart(
  59. RCArray<ValidationResult>* removedPartsValidationResults)
  60. {
  61. if (reason)
  62. {
  63. RCArray<ValidationResult> temporaryInvalidParts;
  64. JSON::JSONValue* valid = reason->getValidPart(&temporaryInvalidParts);
  65. Text* p = reason->getPath().getTeilText(path.getLength());
  66. if (foundValue->getType() == AbstractType::ARRAY)
  67. {
  68. if (!valid
  69. && (!expected->hasAttribute("removeInvalidEntries")
  70. || !expected->getAttributeValue("removeInvalidEntries")
  71. .istGleich("true")))
  72. {
  73. if (removedPartsValidationResults)
  74. {
  75. if (temporaryInvalidParts.getEintragAnzahl() == 1)
  76. {
  77. if (reason) reason->release();
  78. reason = temporaryInvalidParts.get(0);
  79. }
  80. else
  81. {
  82. for (ValidationResult* res : temporaryInvalidParts)
  83. {
  84. if (res->isDifferent(this))
  85. {
  86. removedPartsValidationResults->add(
  87. dynamic_cast<ValidationResult*>(
  88. res->getThis()));
  89. }
  90. }
  91. }
  92. removedPartsValidationResults->add(
  93. dynamic_cast<ValidationResult*>(getThis()));
  94. }
  95. p->release();
  96. return 0;
  97. }
  98. if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
  99. {
  100. Text* it = p->getTeilText(1, p->getLength() - 1);
  101. int i = (int)*it;
  102. it->release();
  103. if (i >= 0 && foundValue->asAbstractArray()->getLength() > i)
  104. {
  105. if (removedPartsValidationResults)
  106. {
  107. for (ValidationResult* res : temporaryInvalidParts)
  108. {
  109. removedPartsValidationResults->add(
  110. dynamic_cast<ValidationResult*>(
  111. res->getThis()));
  112. }
  113. }
  114. JSON::JSONValue* foundValueJson
  115. = dynamic_cast<JSON::JSONValue*>(foundValue);
  116. if (foundValueJson)
  117. {
  118. JSON::JSONValue* tmp = foundValueJson->clone();
  119. if (valid)
  120. tmp->asArray()->setValue(
  121. i, dynamic_cast<JSON::JSONValue*>(valid));
  122. else
  123. tmp->asArray()->removeValue(i);
  124. ValidationResult* res = DataValidator(
  125. dynamic_cast<XML::Element*>(expected->getThis()))
  126. .validate(tmp);
  127. res->addBasePath(path);
  128. if (res->isValid())
  129. {
  130. res->release();
  131. p->release();
  132. return tmp;
  133. }
  134. else if (res->isDifferent(this) || !valid)
  135. {
  136. p->release();
  137. JSON::JSONValue* result = res->getValidPart(
  138. removedPartsValidationResults);
  139. tmp->release();
  140. res->release();
  141. return result;
  142. }
  143. tmp->release();
  144. res->release();
  145. }
  146. }
  147. }
  148. }
  149. else if (foundValue->getType() == AbstractType::OBJECT)
  150. {
  151. if (!valid
  152. && (!expected->hasAttribute("removeInvalidEntries")
  153. || !expected->getAttributeValue("removeInvalidEntries")
  154. .istGleich("true")))
  155. {
  156. if (removedPartsValidationResults)
  157. {
  158. if (temporaryInvalidParts.getEintragAnzahl() == 1)
  159. {
  160. if (reason) reason->release();
  161. reason = temporaryInvalidParts.get(0);
  162. }
  163. else
  164. {
  165. for (ValidationResult* res : temporaryInvalidParts)
  166. {
  167. if (res->isDifferent(this))
  168. {
  169. removedPartsValidationResults->add(
  170. dynamic_cast<ValidationResult*>(
  171. res->getThis()));
  172. }
  173. }
  174. }
  175. removedPartsValidationResults->add(
  176. dynamic_cast<ValidationResult*>(getThis()));
  177. }
  178. p->release();
  179. return 0;
  180. }
  181. if (p->hatAt(0, "."))
  182. {
  183. if (removedPartsValidationResults)
  184. {
  185. for (ValidationResult* res : temporaryInvalidParts)
  186. {
  187. removedPartsValidationResults->add(
  188. dynamic_cast<ValidationResult*>(res->getThis()));
  189. }
  190. }
  191. Text* at = p->getTeilText(1);
  192. Text attr = *at;
  193. at->release();
  194. JSON::JSONValue* foundValueJson
  195. = dynamic_cast<JSON::JSONValue*>(foundValue);
  196. if (foundValueJson)
  197. {
  198. JSON::JSONValue* tmp = foundValueJson->clone();
  199. tmp->asObject()->removeValue(attr);
  200. if (valid)
  201. tmp->asObject()->addValue(
  202. attr, dynamic_cast<JSON::JSONValue*>(valid));
  203. ValidationResult* res = DataValidator(
  204. dynamic_cast<XML::Element*>(expected->getThis()))
  205. .validate(tmp);
  206. res->addBasePath(path);
  207. if (res->isValid())
  208. {
  209. res->release();
  210. p->release();
  211. return tmp;
  212. }
  213. else if (res->isDifferent(this) || !valid)
  214. {
  215. p->release();
  216. JSON::JSONValue* result
  217. = res->getValidPart(removedPartsValidationResults);
  218. tmp->release();
  219. res->release();
  220. return result;
  221. }
  222. tmp->release();
  223. res->release();
  224. }
  225. }
  226. }
  227. p->release();
  228. if (valid) valid->release();
  229. }
  230. if (removedPartsValidationResults)
  231. {
  232. removedPartsValidationResults->add(
  233. dynamic_cast<ValidationResult*>(getThis()));
  234. }
  235. return 0;
  236. }
  237. Text TypeMissmatch::getPath() const
  238. {
  239. return path;
  240. }
  241. bool TypeMissmatch::isDifferent(const ValidationResult* zResult) const
  242. {
  243. const TypeMissmatch* casted = dynamic_cast<const TypeMissmatch*>(zResult);
  244. if (casted == 0) return 1;
  245. if (!casted->getPath().istGleich(path)) return 1;
  246. return reason->isDifferent(casted->reason);
  247. }
  248. void TypeMissmatch::addBasePath(Text basePath)
  249. {
  250. path = basePath + path;
  251. if (reason) reason->addBasePath(basePath);
  252. }
  253. #pragma endregion Cotent
  254. #pragma region UnknownValue
  255. UnknownValue::UnknownValue(Text path, AbstractElement* foundValue)
  256. : ValidationResult()
  257. {
  258. this->path = path;
  259. this->foundValue = foundValue;
  260. }
  261. UnknownValue::~UnknownValue()
  262. {
  263. foundValue->release();
  264. }
  265. bool UnknownValue::isValid() const
  266. {
  267. return 0;
  268. }
  269. Text UnknownValue::getInvalidInfo(int indent) const
  270. {
  271. Text ind = "";
  272. ind.fillText(' ', indent);
  273. Text error = "";
  274. error.append() << ind.getText() << "Unknown Value at '" << path.getText()
  275. << "'. Value found:\n"
  276. << ind.getText() << foundValue->toString().getText() << "\n";
  277. return error;
  278. }
  279. JSON::JSONValue* UnknownValue::getValidPart(
  280. RCArray<ValidationResult>* removedPartsValidationResults)
  281. {
  282. if (removedPartsValidationResults)
  283. {
  284. removedPartsValidationResults->add(
  285. dynamic_cast<ValidationResult*>(getThis()));
  286. }
  287. return 0;
  288. }
  289. Text UnknownValue::getPath() const
  290. {
  291. return path;
  292. }
  293. bool UnknownValue::isDifferent(const ValidationResult* zResult) const
  294. {
  295. const UnknownValue* casted = dynamic_cast<const UnknownValue*>(zResult);
  296. if (casted == 0) return 1;
  297. if (!casted->getPath().istGleich(path)) return 1;
  298. return 0;
  299. }
  300. void UnknownValue::addBasePath(Text basePath)
  301. {
  302. path = basePath + path;
  303. }
  304. #pragma endregion Cotent
  305. #pragma region MissingValue
  306. MissingValue::MissingValue(Text path, XML::Element* expected)
  307. : ValidationResult()
  308. {
  309. this->path = path;
  310. this->expected = expected;
  311. }
  312. MissingValue::~MissingValue()
  313. {
  314. expected->release();
  315. }
  316. bool MissingValue::isValid() const
  317. {
  318. return 0;
  319. }
  320. Text MissingValue::getInvalidInfo(int indent) const
  321. {
  322. Text ind = "";
  323. ind.fillText(' ', indent);
  324. Text error = "";
  325. error.append() << ind.getText() << "Missing Value at '" << path.getText()
  326. << "'. Expected type:\n"
  327. << ind.getText() << expected->toString().getText() << "\n";
  328. return error;
  329. }
  330. JSON::JSONValue* MissingValue::getValidPart(
  331. RCArray<ValidationResult>* removedPartsValidationResults)
  332. {
  333. if (expected->hasAttribute("default"))
  334. {
  335. JSON::JSONValue* def
  336. = JSON::Parser::getValue(expected->getAttributeValue("default"));
  337. ValidationResult* res
  338. = DataValidator(dynamic_cast<XML::Element*>(expected->getThis()))
  339. .validate(def);
  340. res->addBasePath(path);
  341. if (res->isValid())
  342. {
  343. res->release();
  344. return def;
  345. }
  346. else if (res->isDifferent(this))
  347. {
  348. def->release();
  349. JSON::JSONValue* result
  350. = res->getValidPart(removedPartsValidationResults);
  351. res->release();
  352. return result;
  353. }
  354. def->release();
  355. }
  356. if (removedPartsValidationResults)
  357. {
  358. removedPartsValidationResults->add(
  359. dynamic_cast<ValidationResult*>(getThis()));
  360. }
  361. return 0;
  362. }
  363. Text MissingValue::getPath() const
  364. {
  365. return path;
  366. }
  367. bool MissingValue::isDifferent(const ValidationResult* zResult) const
  368. {
  369. const MissingValue* casted = dynamic_cast<const MissingValue*>(zResult);
  370. if (casted == 0) return 1;
  371. if (!casted->getPath().istGleich(path)) return 1;
  372. return 0;
  373. }
  374. void MissingValue::addBasePath(Text basePath)
  375. {
  376. path = basePath + path;
  377. }
  378. #pragma endregion Cotent
  379. #pragma region MissingOneOf
  380. MissingOneOf::MissingOneOf(Text path, XML::Editor expected)
  381. : ValidationResult()
  382. {
  383. this->path = path;
  384. for (XML::Element* e : expected)
  385. this->expected.add(dynamic_cast<XML::Element*>(e->getThis()));
  386. }
  387. MissingOneOf::~MissingOneOf() {}
  388. bool MissingOneOf::isValid() const
  389. {
  390. return 0;
  391. }
  392. Text MissingOneOf::getInvalidInfo(int indent) const
  393. {
  394. Text ind = "";
  395. ind.fillText(' ', indent);
  396. Text error = "";
  397. error.append() << ind.getText() << "Missing Value at '" << path.getText()
  398. << "'. The value should have one of the specified types:\n";
  399. ind += " ";
  400. for (XML::Element* e : expected)
  401. {
  402. error.append() << ind.getText() << e->toString().getText() << "\n";
  403. }
  404. return error;
  405. }
  406. JSON::JSONValue* MissingOneOf::getValidPart(
  407. RCArray<ValidationResult>* removedPartsValidationResults)
  408. {
  409. for (XML::Element* e : expected)
  410. {
  411. if (e->hasAttribute("default"))
  412. {
  413. JSON::JSONValue* defaultValue
  414. = JSON::Parser::getValue(e->getAttributeValue("default"));
  415. if (defaultValue)
  416. {
  417. JSON::JSONValue* valid
  418. = DataValidator(dynamic_cast<XML::Element*>(e->getThis()))
  419. .getValidParts(
  420. defaultValue, removedPartsValidationResults);
  421. defaultValue->release();
  422. if (valid)
  423. {
  424. return valid;
  425. }
  426. }
  427. }
  428. }
  429. if (removedPartsValidationResults)
  430. {
  431. removedPartsValidationResults->add(
  432. dynamic_cast<ValidationResult*>(getThis()));
  433. }
  434. // multiple possibilities are undecidable
  435. return 0;
  436. }
  437. Text MissingOneOf::getPath() const
  438. {
  439. return path;
  440. }
  441. bool MissingOneOf::isDifferent(const ValidationResult* zResult) const
  442. {
  443. const MissingOneOf* casted = dynamic_cast<const MissingOneOf*>(zResult);
  444. if (casted == 0) return 1;
  445. if (!casted->getPath().istGleich(path)) return 1;
  446. return 0;
  447. }
  448. void MissingOneOf::addBasePath(Text basePath)
  449. {
  450. path = basePath + path;
  451. }
  452. #pragma endregion Content
  453. #pragma region NoTypeMatching
  454. NoTypeMatching::NoTypeMatching(Text path,
  455. AbstractElement* foundValue,
  456. RCArray<XML::Element>& expected,
  457. RCArray<ValidationResult>& reasons)
  458. : ValidationResult()
  459. {
  460. this->path = path;
  461. this->foundValue = foundValue;
  462. this->expected = expected;
  463. this->reasons = reasons;
  464. }
  465. NoTypeMatching::~NoTypeMatching()
  466. {
  467. foundValue->release();
  468. }
  469. bool NoTypeMatching::isValid() const
  470. {
  471. return 0;
  472. }
  473. Text NoTypeMatching::getInvalidInfo(int indent) const
  474. {
  475. Text ind = "";
  476. ind.fillText(' ', indent);
  477. Text error = "";
  478. error.append() << ind.getText() << "Found Value at '" << path.getText()
  479. << "' did not match any of the specified types\n";
  480. Text ind2 = ind + " ";
  481. error.append() << ind.getText() << "Reasons:\n";
  482. for (ValidationResult* reason : reasons)
  483. {
  484. error += reason->getInvalidInfo(indent + 4);
  485. }
  486. return error;
  487. }
  488. JSON::JSONValue* NoTypeMatching::getValidPart(
  489. RCArray<ValidationResult>* removedPartsValidationResults)
  490. {
  491. RCArray<ValidationResult> tempErrors;
  492. for (ValidationResult* reason : reasons)
  493. {
  494. JSON::JSONValue* result = reason->getValidPart(&tempErrors);
  495. if (result)
  496. {
  497. return result;
  498. }
  499. }
  500. if (removedPartsValidationResults)
  501. {
  502. removedPartsValidationResults->add(
  503. dynamic_cast<ValidationResult*>(getThis()));
  504. reasons.leeren();
  505. for (ValidationResult* error : tempErrors)
  506. {
  507. reasons.add(dynamic_cast<ValidationResult*>(error->getThis()));
  508. }
  509. }
  510. // multiple possibilities are undecidable
  511. return 0;
  512. }
  513. Text NoTypeMatching::getPath() const
  514. {
  515. return path;
  516. }
  517. bool NoTypeMatching::isDifferent(const ValidationResult* zResult) const
  518. {
  519. const NoTypeMatching* casted = dynamic_cast<const NoTypeMatching*>(zResult);
  520. if (casted == 0) return 1;
  521. if (!casted->getPath().istGleich(path)) return 1;
  522. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  523. {
  524. if (reasons.z(i)->isDifferent(casted->reasons.z(i))) return 1;
  525. }
  526. return 0;
  527. }
  528. void NoTypeMatching::addBasePath(Text basePath)
  529. {
  530. path = basePath + path;
  531. for (ValidationResult* reason : reasons)
  532. {
  533. reason->addBasePath(basePath);
  534. }
  535. }
  536. #pragma endregion Content
  537. #pragma region ValidationPathNotFound
  538. ValidationPathNotFound::ValidationPathNotFound(
  539. Text path, AbstractElement* foundValue, Text validationPath)
  540. : ValidationResult()
  541. {
  542. this->path = path;
  543. this->foundValue = foundValue;
  544. this->validationPath = validationPath;
  545. }
  546. ValidationPathNotFound::~ValidationPathNotFound()
  547. {
  548. foundValue->release();
  549. }
  550. bool ValidationPathNotFound::isValid() const
  551. {
  552. return false;
  553. }
  554. Text ValidationPathNotFound::getInvalidInfo(int indent) const
  555. {
  556. Text result;
  557. result.append() << "Expected to validate path '" << validationPath.getText()
  558. << "' but at path '" << path.getText() << "' the value "
  559. << foundValue->toString().getText() << " was found.";
  560. return result;
  561. }
  562. JSON::JSONValue* ValidationPathNotFound::getValidPart(
  563. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  564. {
  565. return 0;
  566. }
  567. Text ValidationPathNotFound::getPath() const
  568. {
  569. return path;
  570. }
  571. bool ValidationPathNotFound::isDifferent(const ValidationResult* zResult) const
  572. {
  573. const ValidationPathNotFound* other
  574. = dynamic_cast<const ValidationPathNotFound*>(zResult);
  575. if (other == 0) return 1;
  576. if (!other->path.istGleich(path)) return 1;
  577. return 0;
  578. }
  579. void ValidationPathNotFound::addBasePath(Text basePath)
  580. {
  581. path = basePath + path;
  582. }
  583. #pragma endregion Content
  584. #pragma region ValidValue
  585. ValidValue::ValidValue(Text path, AbstractElement* value)
  586. : ValidationResult()
  587. {
  588. this->path = path;
  589. this->value = value;
  590. }
  591. ValidValue::~ValidValue()
  592. {
  593. value->release();
  594. }
  595. bool ValidValue::isValid() const
  596. {
  597. return 1;
  598. }
  599. Text ValidValue::getInvalidInfo(int indent) const
  600. {
  601. return "";
  602. }
  603. JSON::JSONValue* ValidValue::getValidPart(
  604. RCArray<ValidationResult>* removedPartsValidationResults)
  605. {
  606. JSON::JSONValue* json = dynamic_cast<JSON::JSONValue*>(value);
  607. return json ? json->clone() : 0;
  608. }
  609. Text ValidValue::getPath() const
  610. {
  611. return path;
  612. }
  613. bool ValidValue::isDifferent(const ValidationResult* zResult) const
  614. {
  615. const ValidValue* casted = dynamic_cast<const ValidValue*>(zResult);
  616. if (casted == 0) return 1;
  617. if (!casted->getPath().istGleich(path)) return 1;
  618. return 0;
  619. }
  620. void ValidValue::addBasePath(Text basePath)
  621. {
  622. path = basePath + path;
  623. }
  624. #pragma endregion Content
  625. #pragma endregion Content
  626. #pragma region DataValidator
  627. DataValidator::DataValidator(XML::Element* constraints)
  628. : ReferenceCounter(),
  629. constraints(constraints),
  630. typeConstraints(new RCTrie<XML::Element>())
  631. {
  632. for (XML::Element* e :
  633. constraints->select().selectAllElements().whereAttributeExists("id"))
  634. {
  635. Framework::Text id = e->getAttributeValue("id");
  636. typeConstraints->set(
  637. id, id.getLength(), dynamic_cast<XML::Element*>(e->getThis()));
  638. }
  639. }
  640. DataValidator::DataValidator(
  641. XML::Element* constraints, RCTrie<XML::Element>* typeConstraints)
  642. : ReferenceCounter(),
  643. constraints(constraints),
  644. typeConstraints(typeConstraints)
  645. {}
  646. DataValidator::~DataValidator()
  647. {
  648. constraints->release();
  649. typeConstraints->release();
  650. }
  651. ValidationResult* DataValidator::validate(AbstractElement* zValue) const
  652. {
  653. return validate(ElementPathBuilder().build(), zValue);
  654. }
  655. ValidationResult* Framework::Validator::DataValidator::validate(
  656. ElementPath* path, AbstractElement* zValue) const
  657. {
  658. ValidationResult* result = validate(path, zValue, constraints, "");
  659. path->release();
  660. return result;
  661. }
  662. bool DataValidator::isValid(AbstractElement* zValue) const
  663. {
  664. ValidationResult* res = validate(zValue);
  665. if (res->isValid())
  666. {
  667. res->release();
  668. return 1;
  669. }
  670. res->release();
  671. return 0;
  672. }
  673. JSON::JSONValue* DataValidator::getValidParts(JSON::JSONValue* zValue,
  674. RCArray<ValidationResult>* removedPartsValidationResults) const
  675. {
  676. ValidationResult* res = validate(zValue);
  677. if (res->isValid())
  678. {
  679. res->release();
  680. return zValue->clone();
  681. }
  682. JSON::JSONValue* valid = res->getValidPart(removedPartsValidationResults);
  683. res->release();
  684. return valid;
  685. }
  686. XML::Element* DataValidator::zConstraints()
  687. {
  688. return constraints;
  689. }
  690. JSON::JSONObject* Framework::Validator::DataValidator::getJsonSchema() const
  691. {
  692. JSON::JSONObject* zDefs = new JSON::JSONObject();
  693. JSON::JSONObject* result = getJsonSchema(constraints, zDefs);
  694. if (zDefs->getFieldCount() > 0)
  695. {
  696. result->addValue("$defs", zDefs);
  697. }
  698. else
  699. {
  700. zDefs->release();
  701. }
  702. result->addValue("$schema",
  703. new JSON::JSONString("https://json-schema.org/draft-04/schema#"));
  704. return result;
  705. }
  706. void Framework::Validator::DataValidator::updateValidator(
  707. Text id, DataValidator* validator)
  708. {
  709. typeConstraints->set(id, id.getLength(), validator->constraints);
  710. validator->release();
  711. }
  712. ValidationResult* DataValidator::validate(ElementPath* pathToValidate,
  713. AbstractElement* zValue,
  714. XML::Element* zConstraints,
  715. Text path) const
  716. {
  717. if (zConstraints->getName().istGleich("oneOf"))
  718. {
  719. return validateMultipleTypes(
  720. pathToValidate, zValue, zConstraints, path);
  721. }
  722. if (zConstraints->getName().istGleich("ref"))
  723. {
  724. Text id = zConstraints->getAttributeValue("ref");
  725. XML::Element* e = typeConstraints->z(id, id.getLength());
  726. if (e)
  727. {
  728. zConstraints = e;
  729. }
  730. }
  731. switch (zValue->getType())
  732. {
  733. case AbstractType::NULL_:
  734. if (pathToValidate->isValid())
  735. {
  736. return new ValidationPathNotFound(path,
  737. dynamic_cast<AbstractElement*>(zValue->getThis()),
  738. pathToValidate->toString());
  739. }
  740. if (!zConstraints->hasAttribute("nullable")
  741. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  742. {
  743. return new TypeMissmatch(path,
  744. dynamic_cast<AbstractElement*>(zValue->getThis()),
  745. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  746. 0);
  747. }
  748. break;
  749. case AbstractType::BOOLEAN:
  750. if (pathToValidate->isValid())
  751. {
  752. return new ValidationPathNotFound(path,
  753. dynamic_cast<AbstractElement*>(zValue->getThis()),
  754. pathToValidate->toString());
  755. }
  756. if (!zConstraints->getName().istGleich("bool"))
  757. {
  758. return new TypeMissmatch(path,
  759. dynamic_cast<AbstractElement*>(zValue->getThis()),
  760. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  761. 0);
  762. }
  763. else if (zConstraints->hasAttribute("equals"))
  764. {
  765. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  766. == !zValue->asAbstractBool()->getBool())
  767. {
  768. return new TypeMissmatch(path,
  769. dynamic_cast<AbstractElement*>(zValue->getThis()),
  770. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  771. 0);
  772. }
  773. }
  774. break;
  775. case AbstractType::NUMBER:
  776. if (pathToValidate->isValid())
  777. {
  778. return new ValidationPathNotFound(path,
  779. dynamic_cast<AbstractElement*>(zValue->getThis()),
  780. pathToValidate->toString());
  781. }
  782. if (!zConstraints->getName().istGleich("number"))
  783. {
  784. return new TypeMissmatch(path,
  785. dynamic_cast<AbstractElement*>(zValue->getThis()),
  786. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  787. 0);
  788. }
  789. else
  790. {
  791. double number = zValue->asAbstractNumber()->getNumber();
  792. if (zConstraints->hasAttribute("equals")
  793. && (double)zConstraints->getAttributeValue("equals") != number)
  794. {
  795. return new TypeMissmatch(path,
  796. dynamic_cast<AbstractElement*>(zValue->getThis()),
  797. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  798. 0);
  799. }
  800. if (zConstraints->hasAttribute("lessOrEqual")
  801. && number
  802. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  803. {
  804. return new TypeMissmatch(path,
  805. dynamic_cast<AbstractElement*>(zValue->getThis()),
  806. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  807. 0);
  808. }
  809. if (zConstraints->hasAttribute("greaterOrEqual")
  810. && number < (double)zConstraints->getAttributeValue(
  811. "greaterOrEqual"))
  812. {
  813. return new TypeMissmatch(path,
  814. dynamic_cast<AbstractElement*>(zValue->getThis()),
  815. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  816. 0);
  817. }
  818. if (zConstraints->hasAttribute("less")
  819. && number >= (double)zConstraints->getAttributeValue("less"))
  820. {
  821. return new TypeMissmatch(path,
  822. dynamic_cast<AbstractElement*>(zValue->getThis()),
  823. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  824. 0);
  825. }
  826. if (zConstraints->hasAttribute("greater")
  827. && number <= (double)zConstraints->getAttributeValue("greater"))
  828. {
  829. return new TypeMissmatch(path,
  830. dynamic_cast<AbstractElement*>(zValue->getThis()),
  831. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  832. 0);
  833. }
  834. }
  835. break;
  836. case AbstractType::STRING:
  837. if (pathToValidate->isValid())
  838. {
  839. return new ValidationPathNotFound(path,
  840. dynamic_cast<AbstractElement*>(zValue->getThis()),
  841. pathToValidate->toString());
  842. }
  843. if (!zConstraints->getName().istGleich("string"))
  844. {
  845. return new TypeMissmatch(path,
  846. dynamic_cast<AbstractElement*>(zValue->getThis()),
  847. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  848. 0);
  849. }
  850. else
  851. {
  852. Text string = zValue->asAbstractString()->getString();
  853. if (zConstraints->hasAttribute("equals")
  854. && !zConstraints->getAttributeValue("equals").istGleich(string))
  855. {
  856. return new TypeMissmatch(path,
  857. dynamic_cast<AbstractElement*>(zValue->getThis()),
  858. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  859. 0);
  860. }
  861. if (zConstraints->hasAttribute("contains")
  862. && !string.hat(
  863. zConstraints->getAttributeValue("contains").getText()))
  864. {
  865. return new TypeMissmatch(path,
  866. dynamic_cast<AbstractElement*>(zValue->getThis()),
  867. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  868. 0);
  869. }
  870. if (zConstraints->hasAttribute("startsWith")
  871. && string.positionVon(
  872. zConstraints->getAttributeValue("startsWith").getText())
  873. != 0)
  874. {
  875. return new TypeMissmatch(path,
  876. dynamic_cast<AbstractElement*>(zValue->getThis()),
  877. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  878. 0);
  879. }
  880. if (zConstraints->hasAttribute("endsWith")
  881. && !string.hatAt(
  882. string.getLength()
  883. - zConstraints->getAttributeValue("endsWith")
  884. .getLength(),
  885. zConstraints->getAttributeValue("endsWith").getText()))
  886. {
  887. return new TypeMissmatch(path,
  888. dynamic_cast<AbstractElement*>(zValue->getThis()),
  889. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  890. 0);
  891. }
  892. if (zConstraints->hasAttribute("oneOf"))
  893. {
  894. JSON::JSONArray* array = JSON::Parser::getValue(
  895. zConstraints->getAttributeValue("oneOf"))
  896. ->asArray();
  897. bool ok = 0;
  898. for (JSON::JSONValue* v : *array)
  899. {
  900. if (v->asString()->getString().istGleich(string))
  901. {
  902. ok = 1;
  903. break;
  904. }
  905. }
  906. array->release();
  907. if (!ok)
  908. {
  909. return new TypeMissmatch(path,
  910. dynamic_cast<AbstractElement*>(zValue->getThis()),
  911. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  912. 0);
  913. }
  914. }
  915. }
  916. break;
  917. case AbstractType::ARRAY:
  918. if (!zConstraints->getName().istGleich("array"))
  919. {
  920. return new TypeMissmatch(path,
  921. dynamic_cast<AbstractElement*>(zValue->getThis()),
  922. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  923. 0);
  924. }
  925. else
  926. {
  927. if (pathToValidate->isValid())
  928. {
  929. if (!pathToValidate->isArrayElement())
  930. {
  931. return new ValidationPathNotFound(path,
  932. dynamic_cast<AbstractElement*>(zValue->getThis()),
  933. pathToValidate->toString());
  934. }
  935. else
  936. {
  937. int index = pathToValidate->getArrayIndex();
  938. const AbstractArray* array = zValue->asAbstractArray();
  939. if (index >= array->getLength())
  940. {
  941. return new ValidationPathNotFound(path,
  942. dynamic_cast<AbstractElement*>(zValue->getThis()),
  943. pathToValidate->toString());
  944. }
  945. Text p = path;
  946. p += "[";
  947. p += index;
  948. p += "]";
  949. pathToValidate->next();
  950. return validateMultipleTypes(pathToValidate,
  951. array->zAbstractValue(index),
  952. zConstraints,
  953. p);
  954. }
  955. }
  956. const AbstractArray* array = zValue->asAbstractArray();
  957. if (zConstraints->hasAttribute("minSize")
  958. && (int)zConstraints->getAttributeValue("minSize")
  959. > array->getLength())
  960. {
  961. return new TypeMissmatch(path,
  962. dynamic_cast<AbstractElement*>(zValue->getThis()),
  963. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  964. 0);
  965. }
  966. if (zConstraints->hasAttribute("maxSize")
  967. && (int)zConstraints->getAttributeValue("maxSize")
  968. < array->getLength())
  969. {
  970. return new TypeMissmatch(path,
  971. dynamic_cast<AbstractElement*>(zValue->getThis()),
  972. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  973. 0);
  974. }
  975. for (int i = 0; i < array->getLength(); i++)
  976. {
  977. Text p = path;
  978. p += "[";
  979. p += i;
  980. p += "]";
  981. ValidationResult* res = validateMultipleTypes(
  982. pathToValidate, array->zAbstractValue(i), zConstraints, p);
  983. if (!res->isValid())
  984. {
  985. return new TypeMissmatch(path,
  986. dynamic_cast<AbstractElement*>(zValue->getThis()),
  987. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  988. res);
  989. }
  990. }
  991. }
  992. break;
  993. case AbstractType::OBJECT:
  994. if (!zConstraints->getName().istGleich("object"))
  995. {
  996. return new TypeMissmatch(path,
  997. dynamic_cast<AbstractElement*>(zValue->getThis()),
  998. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  999. 0);
  1000. }
  1001. else
  1002. {
  1003. if (pathToValidate->isValid())
  1004. {
  1005. if (!pathToValidate->isObjectAttribute())
  1006. {
  1007. return new ValidationPathNotFound(path,
  1008. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1009. pathToValidate->toString());
  1010. }
  1011. else
  1012. {
  1013. Text key = pathToValidate->getObjectAttribute();
  1014. const AbstractObject* obj = zValue->asAbstractObject();
  1015. if (!obj->hasValue(key))
  1016. {
  1017. return new ValidationPathNotFound(path,
  1018. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1019. pathToValidate->toString());
  1020. }
  1021. Text p = path;
  1022. p += ".";
  1023. p += key;
  1024. if (!zConstraints->selectChildsByAttribute("name", key)
  1025. .exists())
  1026. {
  1027. if (!zConstraints
  1028. ->getAttributeValue("allowAdditionalAttributes")
  1029. .istGleich("true"))
  1030. {
  1031. return new TypeMissmatch(path,
  1032. dynamic_cast<AbstractElement*>(
  1033. zValue->getThis()),
  1034. dynamic_cast<XML::Element*>(
  1035. zConstraints->getThis()),
  1036. new UnknownValue(p,
  1037. dynamic_cast<AbstractElement*>(
  1038. obj->zAbstractValue(key)->getThis())));
  1039. }
  1040. }
  1041. else
  1042. {
  1043. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1044. "name", key);
  1045. pathToValidate->next();
  1046. return validateMultipleTypes(pathToValidate,
  1047. obj->zAbstractValue(key),
  1048. tmp.begin().val(),
  1049. p);
  1050. }
  1051. }
  1052. }
  1053. const AbstractObject* obj = zValue->asAbstractObject();
  1054. for (int i = 0; i < obj->getFieldCount(); i++)
  1055. {
  1056. Text key = obj->getFieldKey(i);
  1057. Text p = path;
  1058. p += ".";
  1059. p += key;
  1060. if (!zConstraints->selectChildsByAttribute("name", key)
  1061. .exists())
  1062. {
  1063. if (!zConstraints
  1064. ->getAttributeValue("allowAdditionalAttributes")
  1065. .istGleich("true"))
  1066. {
  1067. return new TypeMissmatch(path,
  1068. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1069. dynamic_cast<XML::Element*>(
  1070. zConstraints->getThis()),
  1071. new UnknownValue(p,
  1072. dynamic_cast<AbstractElement*>(
  1073. obj->zAbstractValue(i)->getThis())));
  1074. }
  1075. }
  1076. else
  1077. {
  1078. XML::Editor tmp
  1079. = zConstraints->selectChildsByAttribute("name", key);
  1080. ValidationResult* res
  1081. = validateMultipleTypes(pathToValidate,
  1082. obj->zAbstractValue(i),
  1083. tmp.begin().val(),
  1084. p);
  1085. if (!res->isValid())
  1086. {
  1087. return new TypeMissmatch(path,
  1088. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1089. dynamic_cast<XML::Element*>(
  1090. zConstraints->getThis()),
  1091. res);
  1092. }
  1093. res->release();
  1094. }
  1095. }
  1096. for (XML::Element* constraint : zConstraints->selectChildren())
  1097. {
  1098. if (!obj->hasValue(constraint->getAttributeValue("name")))
  1099. {
  1100. XML::Editor tmp = constraint->selectChildren();
  1101. bool optional = true;
  1102. std::function<void(XML::Element*)> checkOptional;
  1103. checkOptional = [&optional, &checkOptional](
  1104. XML::Element* zElement) {
  1105. if (zElement->getName().istGleich("oneOf")
  1106. && (!zElement->hasAttribute("optional")
  1107. || !zElement->getAttributeValue("optional")
  1108. .istGleich("true")))
  1109. {
  1110. XML::Editor tmp = zElement->selectChildren();
  1111. tmp.forEach(checkOptional);
  1112. }
  1113. else
  1114. {
  1115. optional &= zElement->hasAttribute("optional")
  1116. && zElement->getAttributeValue("optional")
  1117. .istGleich("true");
  1118. }
  1119. };
  1120. tmp.forEach(checkOptional);
  1121. if (!optional)
  1122. {
  1123. Text p = path;
  1124. p += ".";
  1125. p += constraint->getAttributeValue("name");
  1126. if (constraint->getChildCount() != 1)
  1127. return new TypeMissmatch(path,
  1128. dynamic_cast<AbstractElement*>(
  1129. zValue->getThis()),
  1130. dynamic_cast<XML::Element*>(
  1131. zConstraints->getThis()),
  1132. new MissingOneOf(p, tmp));
  1133. return new TypeMissmatch(path,
  1134. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1135. dynamic_cast<XML::Element*>(
  1136. zConstraints->getThis()),
  1137. new MissingValue(p,
  1138. dynamic_cast<XML::Element*>(
  1139. tmp.begin()->getThis())));
  1140. }
  1141. }
  1142. }
  1143. }
  1144. break;
  1145. }
  1146. return new ValidValue(
  1147. path, dynamic_cast<AbstractElement*>(zValue->getThis()));
  1148. }
  1149. ValidationResult* DataValidator::validateMultipleTypes(
  1150. ElementPath* pathToValidate,
  1151. AbstractElement* zChildValue,
  1152. XML::Element* zPossibleChildConstraints,
  1153. Text childPath) const
  1154. {
  1155. if (zPossibleChildConstraints->getChildCount() == 1
  1156. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1157. {
  1158. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1159. return validate(pathToValidate,
  1160. zChildValue,
  1161. children.begin().val(),
  1162. childPath); // only one type is possible
  1163. }
  1164. bool hasTypeAttr = 0;
  1165. RCArray<XML::Element> possibleConstraints;
  1166. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1167. { // try to find the correct constraints based on the type attribute
  1168. hasTypeAttr = 1;
  1169. Text typeAttr
  1170. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1171. if (zChildValue->getType() == AbstractType::OBJECT)
  1172. {
  1173. const AbstractObject* obj = zChildValue->asAbstractObject();
  1174. if (obj->hasValue(typeAttr))
  1175. {
  1176. AbstractElement* typeV = obj->zAbstractValue(typeAttr);
  1177. ElementPath* tmp = ElementPathBuilder().build();
  1178. for (XML::Element* constraint :
  1179. zPossibleChildConstraints->selectChildsByName("object")
  1180. .whereChildWithAttributeExists("name", typeAttr))
  1181. {
  1182. XML::Editor nameChildren
  1183. = constraint->selectChildsByAttribute("name", typeAttr);
  1184. XML::Element* typeAttrContraints
  1185. = nameChildren.begin().val();
  1186. ValidationResult* res = validateMultipleTypes(tmp,
  1187. typeV,
  1188. typeAttrContraints,
  1189. childPath + "." + typeAttr);
  1190. if (res->isValid())
  1191. possibleConstraints.add(
  1192. dynamic_cast<XML::Element*>(constraint->getThis()));
  1193. res->release();
  1194. }
  1195. tmp->release();
  1196. }
  1197. }
  1198. }
  1199. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1200. return validate(pathToValidate,
  1201. zChildValue,
  1202. possibleConstraints.begin().val(),
  1203. childPath); // if the type is clear
  1204. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1205. { // more then one type is possible
  1206. RCArray<ValidationResult> invalidResults;
  1207. int index = pathToValidate->getCurrentPathElementIndex();
  1208. for (XML::Element* constraint : possibleConstraints)
  1209. {
  1210. pathToValidate->setCurrentPathElementIndex(index);
  1211. ValidationResult* res
  1212. = validate(pathToValidate, zChildValue, constraint, childPath);
  1213. invalidResults.add(res);
  1214. if (res->isValid())
  1215. return new ValidValue(childPath,
  1216. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1217. }
  1218. return new NoTypeMatching(childPath,
  1219. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1220. possibleConstraints,
  1221. invalidResults);
  1222. }
  1223. // try all types
  1224. possibleConstraints.leeren();
  1225. RCArray<ValidationResult> invalidResults;
  1226. int index = pathToValidate->getCurrentPathElementIndex();
  1227. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1228. {
  1229. pathToValidate->setCurrentPathElementIndex(index);
  1230. ValidationResult* res
  1231. = validate(pathToValidate, zChildValue, constraint, childPath);
  1232. invalidResults.add(res);
  1233. if (res->isValid())
  1234. return new ValidValue(childPath,
  1235. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1236. possibleConstraints.add(
  1237. dynamic_cast<XML::Element*>(constraint->getThis()));
  1238. }
  1239. pathToValidate->setCurrentPathElementIndex(index);
  1240. if (pathToValidate->isValid())
  1241. {
  1242. return new ValidationPathNotFound(childPath,
  1243. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1244. pathToValidate->toString());
  1245. }
  1246. return new NoTypeMatching(childPath,
  1247. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1248. possibleConstraints,
  1249. invalidResults);
  1250. }
  1251. JSON::JSONObject* Framework::Validator::DataValidator::getJsonSchema(
  1252. XML::Element* zConstraint, JSON::JSONObject* zDefs) const
  1253. {
  1254. JSON::JSONObject* result = new JSON::JSONObject();
  1255. if (zConstraint->hasAttribute("id"))
  1256. {
  1257. Text id = zConstraint->getAttributeValue("id");
  1258. id.ersetzen(" ", "_");
  1259. if (zDefs->hasValue(id))
  1260. {
  1261. Text jsonRef = Text("#/$defs/") + id;
  1262. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1263. return result;
  1264. }
  1265. zDefs->addValue(id, new JSON::JSONValue()); // avoid endless recursion
  1266. // for cyclic datastructures
  1267. }
  1268. if (zConstraint->getName().istGleich("bool"))
  1269. {
  1270. if (zConstraint->hasAttribute("equals"))
  1271. {
  1272. JSON::JSONArray* array = new JSON::JSONArray();
  1273. array->addValue(new JSON::JSONBool(
  1274. zConstraint->getAttributeValue("equals").istGleich("true")));
  1275. result->addValue("enum", array);
  1276. }
  1277. else
  1278. {
  1279. result->addValue("type", new JSON::JSONString("boolean"));
  1280. }
  1281. }
  1282. else if (zConstraint->getName().istGleich("number"))
  1283. {
  1284. if (zConstraint->hasAttribute("equals"))
  1285. {
  1286. JSON::JSONArray* array = new JSON::JSONArray();
  1287. array->addValue(new JSON::JSONNumber(
  1288. (double)zConstraint->getAttributeValue("equals")));
  1289. result->addValue("enum", array);
  1290. }
  1291. else
  1292. {
  1293. result->addValue("type", new JSON::JSONString("number"));
  1294. if (zConstraint->hasAttribute("lessOrEqual"))
  1295. {
  1296. result->addValue("maximum",
  1297. new JSON::JSONNumber(
  1298. (double)zConstraint->getAttributeValue("lessOrEqual")));
  1299. }
  1300. if (zConstraint->hasAttribute("greaterOrEqual"))
  1301. {
  1302. result->addValue("minimum",
  1303. new JSON::JSONNumber((double)zConstraint->getAttributeValue(
  1304. "greaterOrEqual")));
  1305. }
  1306. if (zConstraint->hasAttribute("less"))
  1307. {
  1308. result->addValue("maximum",
  1309. new JSON::JSONNumber(
  1310. (double)zConstraint->getAttributeValue("less")));
  1311. result->addValue("exclusiveMaximum", new JSON::JSONBool(true));
  1312. }
  1313. if (zConstraint->hasAttribute("greater"))
  1314. {
  1315. result->addValue("minimum",
  1316. new JSON::JSONNumber(
  1317. (double)zConstraint->getAttributeValue("greater")));
  1318. result->addValue("exclusiveMinimum", new JSON::JSONBool(true));
  1319. }
  1320. }
  1321. }
  1322. else if (zConstraint->getName().istGleich("string"))
  1323. {
  1324. if (zConstraint->hasAttribute("equals"))
  1325. {
  1326. JSON::JSONArray* array = new JSON::JSONArray();
  1327. array->addValue(
  1328. new JSON::JSONString(zConstraint->getAttributeValue("equals")));
  1329. result->addValue("enum", array);
  1330. }
  1331. else if (zConstraint->hasAttribute("oneOf"))
  1332. {
  1333. JSON::JSONArray* array = JSON::Parser::getValue(
  1334. zConstraint->getAttributeValue("oneOf"))
  1335. ->asArray();
  1336. result->addValue("enum", array);
  1337. }
  1338. else
  1339. {
  1340. result->addValue("type", new JSON::JSONString("string"));
  1341. if (zConstraint->hasAttribute("contains"))
  1342. {
  1343. result->addValue("pattern",
  1344. new JSON::JSONString(
  1345. Text(".*")
  1346. + Regex::quote(
  1347. zConstraint->getAttributeValue("contains"))
  1348. + ".*"));
  1349. }
  1350. if (zConstraint->hasAttribute("startsWith"))
  1351. {
  1352. result->addValue("pattern",
  1353. new JSON::JSONString(
  1354. Text("^")
  1355. + Regex::quote(
  1356. zConstraint->getAttributeValue("startsWith"))
  1357. + ".*"));
  1358. }
  1359. if (zConstraint->hasAttribute("endsWith"))
  1360. {
  1361. result->addValue("pattern",
  1362. new JSON::JSONString(
  1363. Text(".*")
  1364. + Regex::quote(
  1365. zConstraint->getAttributeValue("endsWith"))
  1366. + "$"));
  1367. }
  1368. }
  1369. }
  1370. else if (zConstraint->getName().istGleich("ref"))
  1371. {
  1372. Text id = zConstraint->getAttributeValue("ref");
  1373. XML::Element* e = typeConstraints->z(id, id.getLength());
  1374. id.ersetzen(" ", "_");
  1375. if (!zDefs->hasValue(id))
  1376. {
  1377. JSON::JSONObject* def = getJsonSchema(e, zDefs);
  1378. if (!def->hasValue(id))
  1379. {
  1380. zDefs->addValue(id, def);
  1381. }
  1382. else
  1383. {
  1384. // def was already added by a child
  1385. def->release();
  1386. }
  1387. }
  1388. Text jsonRef = Text("#/$defs/") + id;
  1389. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1390. }
  1391. else if (zConstraint->getName().istGleich("object"))
  1392. {
  1393. result->addValue("type", new JSON::JSONString("object"));
  1394. result->addValue("additionalProperties",
  1395. new JSON::JSONBool(
  1396. zConstraint->getAttributeValue("allowAdditionalAttributes")
  1397. .istGleich("true")));
  1398. JSON::JSONObject* properties = new JSON::JSONObject();
  1399. JSON::JSONArray* required = new JSON::JSONArray();
  1400. for (XML::Element* e : zConstraint->selectChildren())
  1401. {
  1402. JSON::JSONObject* prop = new JSON::JSONObject();
  1403. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1404. bool isRequired = true;
  1405. for (XML::Element* child : e->selectChildren())
  1406. {
  1407. oneOf->addValue(getJsonSchema(child, zDefs));
  1408. isRequired
  1409. &= !child->getAttributeValue("optional").istGleich("true")
  1410. && !child->hasAttribute("default");
  1411. }
  1412. prop->addValue("oneOf", oneOf);
  1413. properties->addValue(e->getAttributeValue("name"), prop);
  1414. if (isRequired)
  1415. {
  1416. required->addValue(
  1417. new JSON::JSONString(e->getAttributeValue("name")));
  1418. }
  1419. }
  1420. result->addValue("properties", properties);
  1421. result->addValue("required", required);
  1422. }
  1423. else if (zConstraint->getName().istGleich("array"))
  1424. {
  1425. result->addValue("type", new JSON::JSONString("array"));
  1426. JSON::JSONObject* items = new JSON::JSONObject();
  1427. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1428. for (XML::Element* e : zConstraint->selectChildren())
  1429. {
  1430. oneOf->addValue(getJsonSchema(e, zDefs));
  1431. }
  1432. if (zConstraint->hasAttribute("minSize"))
  1433. {
  1434. result->addValue("minItems",
  1435. new JSON::JSONNumber(
  1436. (double)zConstraint->getAttributeValue("minSize")));
  1437. }
  1438. if (zConstraint->hasAttribute("maxSize"))
  1439. {
  1440. result->addValue("maxItems",
  1441. new JSON::JSONNumber(
  1442. (double)zConstraint->getAttributeValue("maxSize")));
  1443. }
  1444. items->addValue("oneOf", oneOf);
  1445. result->addValue("items", items);
  1446. }
  1447. else if (zConstraint->getName().istGleich("oneOf"))
  1448. {
  1449. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1450. for (XML::Element* e : zConstraint->selectChildren())
  1451. {
  1452. oneOf->addValue(getJsonSchema(e, zDefs));
  1453. }
  1454. result->addValue("oneOf", oneOf);
  1455. }
  1456. if (zConstraint->hasAttribute("nullable")
  1457. && zConstraint->getAttributeValue("nullable").istGleich("true"))
  1458. {
  1459. if (!result->hasValue("type"))
  1460. {
  1461. if (result->hasValue("enum"))
  1462. {
  1463. result->zValue("enum")->asArray()->addValue(
  1464. new JSON::JSONValue());
  1465. }
  1466. else
  1467. {
  1468. JSON::JSONObject* oneOf = new JSON::JSONObject();
  1469. JSON::JSONArray* array = new JSON::JSONArray();
  1470. array->addValue(result);
  1471. JSON::JSONObject* nullType = new JSON::JSONObject();
  1472. nullType->addValue("type", new JSON::JSONString("null"));
  1473. array->addValue(nullType);
  1474. oneOf->addValue("oneOf", array);
  1475. result = oneOf;
  1476. }
  1477. }
  1478. else
  1479. {
  1480. if (result->zValue("type")->getType() == AbstractType::ARRAY)
  1481. {
  1482. result->zValue("type")->asArray()->addValue(
  1483. new JSON::JSONString("null"));
  1484. }
  1485. else
  1486. {
  1487. JSON::JSONArray* array = new JSON::JSONArray();
  1488. array->addValue(result->getValue("type"));
  1489. array->addValue(new JSON::JSONString("null"));
  1490. result->removeValue("type");
  1491. result->addValue("type", array);
  1492. }
  1493. }
  1494. }
  1495. if (zConstraint->hasAttribute("id"))
  1496. {
  1497. Text id = zConstraint->getAttributeValue("id");
  1498. id.ersetzen(" ", "_");
  1499. zDefs->removeValue(id);
  1500. zDefs->addValue(id, result);
  1501. result = new JSON::JSONObject();
  1502. Text jsonRef = Text("#/$defs/") + id;
  1503. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1504. }
  1505. return result;
  1506. }
  1507. StringValidationBuilder<DataValidator>* DataValidator::buildForString()
  1508. {
  1509. return new StringValidationBuilder<DataValidator>(
  1510. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1511. }
  1512. NumberValidationBuilder<DataValidator>* DataValidator::buildForNumber()
  1513. {
  1514. return new NumberValidationBuilder<DataValidator>(
  1515. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1516. }
  1517. BoolValidationBuilder<DataValidator>* DataValidator::buildForBool()
  1518. {
  1519. return new BoolValidationBuilder<DataValidator>(
  1520. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1521. }
  1522. ObjectValidationBuilder<DataValidator>* DataValidator::buildForObject()
  1523. {
  1524. return new ObjectValidationBuilder<DataValidator>(
  1525. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1526. }
  1527. ArrayValidationBuilder<DataValidator>* DataValidator::buildForArray()
  1528. {
  1529. return new ArrayValidationBuilder<DataValidator>(
  1530. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1531. }
  1532. DataValidator* DataValidator::buildForReference(Text id)
  1533. {
  1534. return new DataValidator(
  1535. new XML::Element(Text("<ref ref=\"") + id + Text("\"/>")));
  1536. }
  1537. OneOfValidationBuilder<DataValidator>* DataValidator::buildForOneOf()
  1538. {
  1539. return new OneOfValidationBuilder<DataValidator>(
  1540. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1541. }