DataValidator.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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. return result;
  703. }
  704. void Framework::Validator::DataValidator::updateValidator(
  705. Text id, DataValidator* validator)
  706. {
  707. typeConstraints->set(id, id.getLength(), validator->constraints);
  708. validator->release();
  709. }
  710. ValidationResult* DataValidator::validate(ElementPath* pathToValidate,
  711. AbstractElement* zValue,
  712. XML::Element* zConstraints,
  713. Text path) const
  714. {
  715. if (zConstraints->getName().istGleich("oneOf"))
  716. {
  717. return validateMultipleTypes(
  718. pathToValidate, zValue, zConstraints, path);
  719. }
  720. if (zConstraints->getName().istGleich("ref"))
  721. {
  722. Text id = zConstraints->getAttributeValue("ref");
  723. XML::Element* e = typeConstraints->z(id, id.getLength());
  724. if (e)
  725. {
  726. zConstraints = e;
  727. }
  728. }
  729. switch (zValue->getType())
  730. {
  731. case AbstractType::NULL_:
  732. if (pathToValidate->isValid())
  733. {
  734. return new ValidationPathNotFound(path,
  735. dynamic_cast<AbstractElement*>(zValue->getThis()),
  736. pathToValidate->toString());
  737. }
  738. if (!zConstraints->hasAttribute("nullable")
  739. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  740. {
  741. return new TypeMissmatch(path,
  742. dynamic_cast<AbstractElement*>(zValue->getThis()),
  743. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  744. 0);
  745. }
  746. break;
  747. case AbstractType::BOOLEAN:
  748. if (pathToValidate->isValid())
  749. {
  750. return new ValidationPathNotFound(path,
  751. dynamic_cast<AbstractElement*>(zValue->getThis()),
  752. pathToValidate->toString());
  753. }
  754. if (!zConstraints->getName().istGleich("bool"))
  755. {
  756. return new TypeMissmatch(path,
  757. dynamic_cast<AbstractElement*>(zValue->getThis()),
  758. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  759. 0);
  760. }
  761. else if (zConstraints->hasAttribute("equals"))
  762. {
  763. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  764. == !zValue->asAbstractBool()->getBool())
  765. {
  766. return new TypeMissmatch(path,
  767. dynamic_cast<AbstractElement*>(zValue->getThis()),
  768. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  769. 0);
  770. }
  771. }
  772. break;
  773. case AbstractType::NUMBER:
  774. if (pathToValidate->isValid())
  775. {
  776. return new ValidationPathNotFound(path,
  777. dynamic_cast<AbstractElement*>(zValue->getThis()),
  778. pathToValidate->toString());
  779. }
  780. if (!zConstraints->getName().istGleich("number"))
  781. {
  782. return new TypeMissmatch(path,
  783. dynamic_cast<AbstractElement*>(zValue->getThis()),
  784. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  785. 0);
  786. }
  787. else
  788. {
  789. double number = zValue->asAbstractNumber()->getNumber();
  790. if (zConstraints->hasAttribute("equals")
  791. && (double)zConstraints->getAttributeValue("equals") != number)
  792. {
  793. return new TypeMissmatch(path,
  794. dynamic_cast<AbstractElement*>(zValue->getThis()),
  795. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  796. 0);
  797. }
  798. if (zConstraints->hasAttribute("lessOrEqual")
  799. && number
  800. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  801. {
  802. return new TypeMissmatch(path,
  803. dynamic_cast<AbstractElement*>(zValue->getThis()),
  804. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  805. 0);
  806. }
  807. if (zConstraints->hasAttribute("greaterOrEqual")
  808. && number < (double)zConstraints->getAttributeValue(
  809. "greaterOrEqual"))
  810. {
  811. return new TypeMissmatch(path,
  812. dynamic_cast<AbstractElement*>(zValue->getThis()),
  813. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  814. 0);
  815. }
  816. if (zConstraints->hasAttribute("less")
  817. && number >= (double)zConstraints->getAttributeValue("less"))
  818. {
  819. return new TypeMissmatch(path,
  820. dynamic_cast<AbstractElement*>(zValue->getThis()),
  821. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  822. 0);
  823. }
  824. if (zConstraints->hasAttribute("greater")
  825. && number <= (double)zConstraints->getAttributeValue("greater"))
  826. {
  827. return new TypeMissmatch(path,
  828. dynamic_cast<AbstractElement*>(zValue->getThis()),
  829. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  830. 0);
  831. }
  832. }
  833. break;
  834. case AbstractType::STRING:
  835. if (pathToValidate->isValid())
  836. {
  837. return new ValidationPathNotFound(path,
  838. dynamic_cast<AbstractElement*>(zValue->getThis()),
  839. pathToValidate->toString());
  840. }
  841. if (!zConstraints->getName().istGleich("string"))
  842. {
  843. return new TypeMissmatch(path,
  844. dynamic_cast<AbstractElement*>(zValue->getThis()),
  845. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  846. 0);
  847. }
  848. else
  849. {
  850. Text string = zValue->asAbstractString()->getString();
  851. if (zConstraints->hasAttribute("equals")
  852. && !zConstraints->getAttributeValue("equals").istGleich(string))
  853. {
  854. return new TypeMissmatch(path,
  855. dynamic_cast<AbstractElement*>(zValue->getThis()),
  856. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  857. 0);
  858. }
  859. if (zConstraints->hasAttribute("contains")
  860. && string.hat(
  861. zConstraints->getAttributeValue("contains").getText()))
  862. {
  863. return new TypeMissmatch(path,
  864. dynamic_cast<AbstractElement*>(zValue->getThis()),
  865. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  866. 0);
  867. }
  868. if (zConstraints->hasAttribute("startsWith")
  869. && string.positionVon(
  870. zConstraints->getAttributeValue("startsWith").getText())
  871. == 0)
  872. {
  873. return new TypeMissmatch(path,
  874. dynamic_cast<AbstractElement*>(zValue->getThis()),
  875. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  876. 0);
  877. }
  878. if (zConstraints->hasAttribute("endsWith")
  879. && string.positionVon(
  880. zConstraints->getAttributeValue("endsWith").getText())
  881. == string.getLength()
  882. - zConstraints->getAttributeValue("endsWith")
  883. .getLength())
  884. {
  885. return new TypeMissmatch(path,
  886. dynamic_cast<AbstractElement*>(zValue->getThis()),
  887. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  888. 0);
  889. }
  890. if (zConstraints->hasAttribute("oneOf"))
  891. {
  892. JSON::JSONArray* array = JSON::Parser::getValue(
  893. zConstraints->getAttributeValue("oneOf"))
  894. ->asArray();
  895. bool ok = 0;
  896. for (JSON::JSONValue* v : *array)
  897. {
  898. if (v->asString()->getString().istGleich(string))
  899. {
  900. ok = 1;
  901. break;
  902. }
  903. }
  904. array->release();
  905. if (!ok)
  906. {
  907. return new TypeMissmatch(path,
  908. dynamic_cast<AbstractElement*>(zValue->getThis()),
  909. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  910. 0);
  911. }
  912. }
  913. }
  914. break;
  915. case AbstractType::ARRAY:
  916. if (!zConstraints->getName().istGleich("array"))
  917. {
  918. return new TypeMissmatch(path,
  919. dynamic_cast<AbstractElement*>(zValue->getThis()),
  920. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  921. 0);
  922. }
  923. else
  924. {
  925. if (pathToValidate->isValid())
  926. {
  927. if (!pathToValidate->isArrayElement())
  928. {
  929. return new ValidationPathNotFound(path,
  930. dynamic_cast<AbstractElement*>(zValue->getThis()),
  931. pathToValidate->toString());
  932. }
  933. else
  934. {
  935. int index = pathToValidate->getArrayIndex();
  936. const AbstractArray* array = zValue->asAbstractArray();
  937. if (index >= array->getLength())
  938. {
  939. return new ValidationPathNotFound(path,
  940. dynamic_cast<AbstractElement*>(zValue->getThis()),
  941. pathToValidate->toString());
  942. }
  943. Text p = path;
  944. p += "[";
  945. p += index;
  946. p += "]";
  947. pathToValidate->next();
  948. return validateMultipleTypes(pathToValidate,
  949. array->zAbstractValue(index),
  950. zConstraints,
  951. p);
  952. }
  953. }
  954. const AbstractArray* array = zValue->asAbstractArray();
  955. for (int i = 0; i < array->getLength(); i++)
  956. {
  957. Text p = path;
  958. p += "[";
  959. p += i;
  960. p += "]";
  961. ValidationResult* res = validateMultipleTypes(
  962. pathToValidate, array->zAbstractValue(i), zConstraints, p);
  963. if (!res->isValid())
  964. {
  965. return new TypeMissmatch(path,
  966. dynamic_cast<AbstractElement*>(zValue->getThis()),
  967. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  968. res);
  969. }
  970. res->release();
  971. }
  972. }
  973. break;
  974. case AbstractType::OBJECT:
  975. if (!zConstraints->getName().istGleich("object"))
  976. {
  977. return new TypeMissmatch(path,
  978. dynamic_cast<AbstractElement*>(zValue->getThis()),
  979. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  980. 0);
  981. }
  982. else
  983. {
  984. if (pathToValidate->isValid())
  985. {
  986. if (!pathToValidate->isObjectAttribute())
  987. {
  988. return new ValidationPathNotFound(path,
  989. dynamic_cast<AbstractElement*>(zValue->getThis()),
  990. pathToValidate->toString());
  991. }
  992. else
  993. {
  994. Text key = pathToValidate->getObjectAttribute();
  995. const AbstractObject* obj = zValue->asAbstractObject();
  996. if (!obj->hasValue(key))
  997. {
  998. return new ValidationPathNotFound(path,
  999. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1000. pathToValidate->toString());
  1001. }
  1002. Text p = path;
  1003. p += ".";
  1004. p += key;
  1005. if (!zConstraints->selectChildsByAttribute("name", key)
  1006. .exists())
  1007. {
  1008. if (!zConstraints
  1009. ->getAttributeValue(
  1010. "allowAdditionalAttributes")
  1011. .istGleich("true"))
  1012. {
  1013. return new TypeMissmatch(path,
  1014. dynamic_cast<AbstractElement*>(
  1015. zValue->getThis()),
  1016. dynamic_cast<XML::Element*>(
  1017. zConstraints->getThis()),
  1018. new UnknownValue(p,
  1019. dynamic_cast<AbstractElement*>(
  1020. obj->zAbstractValue(key)->getThis())));
  1021. }
  1022. }
  1023. else
  1024. {
  1025. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1026. "name", key);
  1027. pathToValidate->next();
  1028. return validateMultipleTypes(pathToValidate,
  1029. obj->zAbstractValue(key),
  1030. tmp.begin().val(),
  1031. p);
  1032. }
  1033. }
  1034. }
  1035. const AbstractObject* obj = zValue->asAbstractObject();
  1036. for (int i = 0; i < obj->getFieldCount(); i++)
  1037. {
  1038. Text key = obj->getFieldKey(i);
  1039. Text p = path;
  1040. p += ".";
  1041. p += key;
  1042. if (!zConstraints->selectChildsByAttribute("name", key)
  1043. .exists())
  1044. {
  1045. if (!zConstraints
  1046. ->getAttributeValue("allowAdditionalAttributes")
  1047. .istGleich("true"))
  1048. {
  1049. return new TypeMissmatch(path,
  1050. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1051. dynamic_cast<XML::Element*>(
  1052. zConstraints->getThis()),
  1053. new UnknownValue(p,
  1054. dynamic_cast<AbstractElement*>(
  1055. obj->zAbstractValue(i)->getThis())));
  1056. }
  1057. }
  1058. else
  1059. {
  1060. XML::Editor tmp
  1061. = zConstraints->selectChildsByAttribute("name", key);
  1062. ValidationResult* res
  1063. = validateMultipleTypes(pathToValidate,
  1064. obj->zAbstractValue(i),
  1065. tmp.begin().val(),
  1066. p);
  1067. if (!res->isValid())
  1068. {
  1069. return new TypeMissmatch(path,
  1070. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1071. dynamic_cast<XML::Element*>(
  1072. zConstraints->getThis()),
  1073. res);
  1074. }
  1075. res->release();
  1076. }
  1077. }
  1078. for (XML::Element* constraint : zConstraints->selectChildren())
  1079. {
  1080. if (!obj->hasValue(constraint->getAttributeValue("name")))
  1081. {
  1082. XML::Editor tmp = constraint->selectChildren();
  1083. bool optional = true;
  1084. std::function<void(XML::Element*)> checkOptional;
  1085. checkOptional = [&optional, &checkOptional](
  1086. XML::Element* zElement) {
  1087. if (zElement->getName().istGleich("oneOf"))
  1088. {
  1089. XML::Editor tmp = zElement->selectChildren();
  1090. tmp.forEach(checkOptional);
  1091. }
  1092. else
  1093. {
  1094. optional &= zElement->hasAttribute("optional")
  1095. && zElement->getAttributeValue("optional")
  1096. .istGleich("true");
  1097. }
  1098. };
  1099. tmp.forEach(checkOptional);
  1100. if (!optional)
  1101. {
  1102. Text p = path;
  1103. p += ".";
  1104. p += constraint->getAttributeValue("name");
  1105. if (constraint->getChildCount() != 1)
  1106. return new TypeMissmatch(path,
  1107. dynamic_cast<AbstractElement*>(
  1108. zValue->getThis()),
  1109. dynamic_cast<XML::Element*>(
  1110. zConstraints->getThis()),
  1111. new MissingOneOf(p, tmp));
  1112. return new TypeMissmatch(path,
  1113. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1114. dynamic_cast<XML::Element*>(
  1115. zConstraints->getThis()),
  1116. new MissingValue(p,
  1117. dynamic_cast<XML::Element*>(
  1118. tmp.begin()->getThis())));
  1119. }
  1120. }
  1121. }
  1122. }
  1123. break;
  1124. }
  1125. return new ValidValue(
  1126. path, dynamic_cast<AbstractElement*>(zValue->getThis()));
  1127. }
  1128. ValidationResult* DataValidator::validateMultipleTypes(
  1129. ElementPath* pathToValidate,
  1130. AbstractElement* zChildValue,
  1131. XML::Element* zPossibleChildConstraints,
  1132. Text childPath) const
  1133. {
  1134. if (zPossibleChildConstraints->getChildCount() == 1
  1135. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1136. {
  1137. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1138. return validate(pathToValidate,
  1139. zChildValue,
  1140. children.begin().val(),
  1141. childPath); // only one type is possible
  1142. }
  1143. bool hasTypeAttr = 0;
  1144. RCArray<XML::Element> possibleConstraints;
  1145. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1146. { // try to find the correct constraints based on the type attribute
  1147. hasTypeAttr = 1;
  1148. Text typeAttr
  1149. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1150. if (zChildValue->getType() == AbstractType::OBJECT)
  1151. {
  1152. const AbstractObject* obj = zChildValue->asAbstractObject();
  1153. if (obj->hasValue(typeAttr))
  1154. {
  1155. AbstractElement* typeV = obj->zAbstractValue(typeAttr);
  1156. ElementPath* tmp = ElementPathBuilder().build();
  1157. for (XML::Element* constraint :
  1158. zPossibleChildConstraints->selectChildsByName("object")
  1159. .whereChildWithAttributeExists("name", typeAttr))
  1160. {
  1161. XML::Editor nameChildren
  1162. = constraint->selectChildsByAttribute("name", typeAttr);
  1163. XML::Element* typeAttrContraints
  1164. = nameChildren.begin().val();
  1165. ValidationResult* res = validateMultipleTypes(tmp,
  1166. typeV,
  1167. typeAttrContraints,
  1168. childPath + "." + typeAttr);
  1169. if (res->isValid())
  1170. possibleConstraints.add(
  1171. dynamic_cast<XML::Element*>(constraint->getThis()));
  1172. res->release();
  1173. }
  1174. tmp->release();
  1175. }
  1176. }
  1177. }
  1178. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1179. return validate(pathToValidate,
  1180. zChildValue,
  1181. possibleConstraints.begin().val(),
  1182. childPath); // if the type is clear
  1183. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1184. { // more then one type is possible
  1185. RCArray<ValidationResult> invalidResults;
  1186. int index = pathToValidate->getCurrentPathElementIndex();
  1187. for (XML::Element* constraint : possibleConstraints)
  1188. {
  1189. pathToValidate->setCurrentPathElementIndex(index);
  1190. ValidationResult* res
  1191. = validate(pathToValidate, zChildValue, constraint, childPath);
  1192. invalidResults.add(res);
  1193. if (res->isValid())
  1194. return new ValidValue(childPath,
  1195. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1196. }
  1197. return new NoTypeMatching(childPath,
  1198. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1199. possibleConstraints,
  1200. invalidResults);
  1201. }
  1202. // try all types
  1203. possibleConstraints.leeren();
  1204. RCArray<ValidationResult> invalidResults;
  1205. int index = pathToValidate->getCurrentPathElementIndex();
  1206. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1207. {
  1208. pathToValidate->setCurrentPathElementIndex(index);
  1209. ValidationResult* res
  1210. = validate(pathToValidate, zChildValue, constraint, childPath);
  1211. invalidResults.add(res);
  1212. if (res->isValid())
  1213. return new ValidValue(childPath,
  1214. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1215. possibleConstraints.add(
  1216. dynamic_cast<XML::Element*>(constraint->getThis()));
  1217. }
  1218. pathToValidate->setCurrentPathElementIndex(index);
  1219. if (pathToValidate->isValid())
  1220. {
  1221. return new ValidationPathNotFound(childPath,
  1222. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1223. pathToValidate->toString());
  1224. }
  1225. return new NoTypeMatching(childPath,
  1226. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1227. possibleConstraints,
  1228. invalidResults);
  1229. }
  1230. JSON::JSONObject* Framework::Validator::DataValidator::getJsonSchema(
  1231. XML::Element* zConstraint, JSON::JSONObject* zDefs) const
  1232. {
  1233. JSON::JSONObject* result = new JSON::JSONObject();
  1234. if (zConstraint->hasAttribute("id")
  1235. && zDefs->hasValue(zConstraint->getAttributeValue("id")))
  1236. {
  1237. Text jsonRef = Text("#/$defs/") + zConstraint->getAttributeValue("id");
  1238. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1239. }
  1240. else
  1241. {
  1242. if (zConstraint->getName().istGleich("bool"))
  1243. {
  1244. if (zConstraint->hasAttribute("equals"))
  1245. {
  1246. JSON::JSONArray* array = new JSON::JSONArray();
  1247. array->addValue(new JSON::JSONBool(
  1248. zConstraint->getAttributeValue("equals").istGleich(
  1249. "true")));
  1250. result->addValue("enum", array);
  1251. }
  1252. else
  1253. {
  1254. result->addValue("type", new JSON::JSONString("boolean"));
  1255. }
  1256. }
  1257. else if (zConstraint->getName().istGleich("number"))
  1258. {
  1259. if (zConstraint->hasAttribute("equals"))
  1260. {
  1261. JSON::JSONArray* array = new JSON::JSONArray();
  1262. array->addValue(new JSON::JSONNumber(
  1263. (double)zConstraint->getAttributeValue("equals")));
  1264. result->addValue("enum", array);
  1265. }
  1266. else
  1267. {
  1268. result->addValue("type", new JSON::JSONString("number"));
  1269. if (zConstraint->hasAttribute("lessOrEqual"))
  1270. {
  1271. result->addValue("maximum",
  1272. new JSON::JSONNumber(
  1273. (double)zConstraint->getAttributeValue(
  1274. "lessOrEqual")));
  1275. }
  1276. if (zConstraint->hasAttribute("greaterOrEqual"))
  1277. {
  1278. result->addValue("minimum",
  1279. new JSON::JSONNumber(
  1280. (double)zConstraint->getAttributeValue(
  1281. "greaterOrEqual")));
  1282. }
  1283. if (zConstraint->hasAttribute("less"))
  1284. {
  1285. result->addValue("exclusiveMaximum",
  1286. new JSON::JSONNumber(
  1287. (double)zConstraint->getAttributeValue("less")));
  1288. }
  1289. if (zConstraint->hasAttribute("greater"))
  1290. {
  1291. result->addValue("exclusiveMinimum",
  1292. new JSON::JSONNumber(
  1293. (double)zConstraint->getAttributeValue("greater")));
  1294. }
  1295. }
  1296. }
  1297. else if (zConstraint->getName().istGleich("string"))
  1298. {
  1299. if (zConstraint->hasAttribute("equals"))
  1300. {
  1301. JSON::JSONArray* array = new JSON::JSONArray();
  1302. array->addValue(new JSON::JSONString(
  1303. zConstraint->getAttributeValue("equals")));
  1304. result->addValue("enum", array);
  1305. }
  1306. else if (zConstraint->hasAttribute("oneOf"))
  1307. {
  1308. JSON::JSONArray* array = JSON::Parser::getValue(
  1309. zConstraint->getAttributeValue("oneOf"))
  1310. ->asArray();
  1311. result->addValue("enum", array);
  1312. }
  1313. else
  1314. {
  1315. result->addValue("type", new JSON::JSONString("string"));
  1316. if (zConstraint->hasAttribute("contains"))
  1317. {
  1318. result->addValue("pattern",
  1319. new JSON::JSONString(
  1320. Text(".*")
  1321. + Regex::quote(
  1322. zConstraint->getAttributeValue("contains"))
  1323. + ".*"));
  1324. }
  1325. if (zConstraint->hasAttribute("startsWith"))
  1326. {
  1327. result->addValue("pattern",
  1328. new JSON::JSONString(
  1329. Text("^")
  1330. + Regex::quote(
  1331. zConstraint->getAttributeValue("startsWith"))
  1332. + ".*"));
  1333. }
  1334. if (zConstraint->hasAttribute("endsWith"))
  1335. {
  1336. result->addValue("pattern",
  1337. new JSON::JSONString(
  1338. Text(".*")
  1339. + Regex::quote(
  1340. zConstraint->getAttributeValue("endsWith"))
  1341. + "$"));
  1342. }
  1343. }
  1344. }
  1345. else if (zConstraint->getName().istGleich("ref"))
  1346. {
  1347. Text id = zConstraint->getAttributeValue("ref");
  1348. XML::Element* e = typeConstraints->z(id, id.getLength());
  1349. if (!zDefs->hasValue(id))
  1350. {
  1351. zDefs->addValue(
  1352. id, new JSON::JSONValue()); // avoid endless recursion for
  1353. // cyclic datastructures
  1354. JSON::JSONObject* def = getJsonSchema(e, zDefs);
  1355. zDefs->removeValue(id);
  1356. zDefs->addValue(id, def);
  1357. }
  1358. Text jsonRef = Text("#/$defs/") + id;
  1359. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1360. }
  1361. else if (zConstraint->getName().istGleich("object"))
  1362. {
  1363. result->addValue("type", new JSON::JSONString("object"));
  1364. result->addValue("additionalProperties",
  1365. new JSON::JSONBool(
  1366. zConstraint->getAttributeValue("allowAdditionalAttributes")
  1367. .istGleich("true")));
  1368. JSON::JSONObject* properties = new JSON::JSONObject();
  1369. JSON::JSONArray* required = new JSON::JSONArray();
  1370. for (XML::Element* e : zConstraint->selectChildren())
  1371. {
  1372. JSON::JSONObject* prop = getJsonSchema(e, zDefs);
  1373. properties->addValue(e->getAttributeValue("name"), prop);
  1374. if (!e->getAttributeValue("optional").istGleich("true"))
  1375. {
  1376. required->addValue(
  1377. new JSON::JSONString(e->getAttributeValue("name")));
  1378. }
  1379. }
  1380. result->addValue("properties", properties);
  1381. result->addValue("required", required);
  1382. }
  1383. else if (zConstraint->getName().istGleich("array"))
  1384. {
  1385. result->addValue("type", new JSON::JSONString("array"));
  1386. JSON::JSONObject* items = new JSON::JSONObject();
  1387. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1388. for (XML::Element* e : zConstraint->selectChildren())
  1389. {
  1390. oneOf->addValue(getJsonSchema(e, zDefs));
  1391. }
  1392. items->addValue("oneOf", oneOf);
  1393. result->addValue("items", items);
  1394. }
  1395. else if (zConstraint->getName().istGleich("oneOf"))
  1396. {
  1397. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1398. for (XML::Element* e : zConstraint->selectChildren())
  1399. {
  1400. oneOf->addValue(getJsonSchema(e, zDefs));
  1401. }
  1402. result->addValue("oneOf", oneOf);
  1403. }
  1404. if (zConstraint->hasAttribute("nullable")
  1405. && zConstraint->getAttributeValue("nullable").istGleich("true"))
  1406. {
  1407. if (!result->hasValue("type"))
  1408. {
  1409. if (result->hasValue("enum"))
  1410. {
  1411. result->zValue("enum")->asArray()->addValue(
  1412. new JSON::JSONValue());
  1413. }
  1414. else
  1415. {
  1416. JSON::JSONObject* oneOf = new JSON::JSONObject();
  1417. JSON::JSONArray* array = new JSON::JSONArray();
  1418. array->addValue(result);
  1419. JSON::JSONObject* nullType = new JSON::JSONObject();
  1420. nullType->addValue("type", new JSON::JSONString("null"));
  1421. array->addValue(nullType);
  1422. oneOf->addValue("oneOf", array);
  1423. result = oneOf;
  1424. }
  1425. }
  1426. else
  1427. {
  1428. if (result->zValue("Type")->getType() == AbstractType::ARRAY)
  1429. {
  1430. result->zValue("Type")->asArray()->addValue(
  1431. new JSON::JSONString("null"));
  1432. }
  1433. else
  1434. {
  1435. JSON::JSONArray* array = new JSON::JSONArray();
  1436. array->addValue(result->getValue("Type"));
  1437. array->addValue(new JSON::JSONString("null"));
  1438. result->removeValue("type");
  1439. result->addValue("type", array);
  1440. }
  1441. }
  1442. }
  1443. if (zConstraint->hasAttribute("id"))
  1444. {
  1445. zDefs->addValue(zConstraint->getAttributeValue("id"), result);
  1446. result = new JSON::JSONObject();
  1447. Text jsonRef
  1448. = Text("#/$defs/") + zConstraint->getAttributeValue("id");
  1449. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1450. }
  1451. }
  1452. return result;
  1453. }
  1454. StringValidationBuilder<DataValidator>* DataValidator::buildForString()
  1455. {
  1456. return new StringValidationBuilder<DataValidator>(
  1457. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1458. }
  1459. NumberValidationBuilder<DataValidator>* DataValidator::buildForNumber()
  1460. {
  1461. return new NumberValidationBuilder<DataValidator>(
  1462. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1463. }
  1464. BoolValidationBuilder<DataValidator>* DataValidator::buildForBool()
  1465. {
  1466. return new BoolValidationBuilder<DataValidator>(
  1467. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1468. }
  1469. ObjectValidationBuilder<DataValidator>* DataValidator::buildForObject()
  1470. {
  1471. return new ObjectValidationBuilder<DataValidator>(
  1472. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1473. }
  1474. ArrayValidationBuilder<DataValidator>* DataValidator::buildForArray()
  1475. {
  1476. return new ArrayValidationBuilder<DataValidator>(
  1477. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1478. }
  1479. DataValidator* DataValidator::buildForReference(Text id)
  1480. {
  1481. return new DataValidator(
  1482. new XML::Element(Text("<ref ref=\"") + id + Text("\"/>")));
  1483. }
  1484. OneOfValidationBuilder<DataValidator>* DataValidator::buildForOneOf()
  1485. {
  1486. return new OneOfValidationBuilder<DataValidator>(
  1487. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1488. }