DataValidator.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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.positionVon(
  882. zConstraints->getAttributeValue("endsWith").getText())
  883. == string.getLength()
  884. - zConstraints->getAttributeValue("endsWith")
  885. .getLength())
  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. for (int i = 0; i < array->getLength(); i++)
  958. {
  959. Text p = path;
  960. p += "[";
  961. p += i;
  962. p += "]";
  963. ValidationResult* res = validateMultipleTypes(
  964. pathToValidate, array->zAbstractValue(i), zConstraints, p);
  965. if (!res->isValid())
  966. {
  967. return new TypeMissmatch(path,
  968. dynamic_cast<AbstractElement*>(zValue->getThis()),
  969. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  970. res);
  971. }
  972. res->release();
  973. }
  974. }
  975. break;
  976. case AbstractType::OBJECT:
  977. if (!zConstraints->getName().istGleich("object"))
  978. {
  979. return new TypeMissmatch(path,
  980. dynamic_cast<AbstractElement*>(zValue->getThis()),
  981. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  982. 0);
  983. }
  984. else
  985. {
  986. if (pathToValidate->isValid())
  987. {
  988. if (!pathToValidate->isObjectAttribute())
  989. {
  990. return new ValidationPathNotFound(path,
  991. dynamic_cast<AbstractElement*>(zValue->getThis()),
  992. pathToValidate->toString());
  993. }
  994. else
  995. {
  996. Text key = pathToValidate->getObjectAttribute();
  997. const AbstractObject* obj = zValue->asAbstractObject();
  998. if (!obj->hasValue(key))
  999. {
  1000. return new ValidationPathNotFound(path,
  1001. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1002. pathToValidate->toString());
  1003. }
  1004. Text p = path;
  1005. p += ".";
  1006. p += key;
  1007. if (!zConstraints->selectChildsByAttribute("name", key)
  1008. .exists())
  1009. {
  1010. if (!zConstraints
  1011. ->getAttributeValue("allowAdditionalAttributes")
  1012. .istGleich("true"))
  1013. {
  1014. return new TypeMissmatch(path,
  1015. dynamic_cast<AbstractElement*>(
  1016. zValue->getThis()),
  1017. dynamic_cast<XML::Element*>(
  1018. zConstraints->getThis()),
  1019. new UnknownValue(p,
  1020. dynamic_cast<AbstractElement*>(
  1021. obj->zAbstractValue(key)->getThis())));
  1022. }
  1023. }
  1024. else
  1025. {
  1026. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1027. "name", key);
  1028. pathToValidate->next();
  1029. return validateMultipleTypes(pathToValidate,
  1030. obj->zAbstractValue(key),
  1031. tmp.begin().val(),
  1032. p);
  1033. }
  1034. }
  1035. }
  1036. const AbstractObject* obj = zValue->asAbstractObject();
  1037. for (int i = 0; i < obj->getFieldCount(); i++)
  1038. {
  1039. Text key = obj->getFieldKey(i);
  1040. Text p = path;
  1041. p += ".";
  1042. p += key;
  1043. if (!zConstraints->selectChildsByAttribute("name", key)
  1044. .exists())
  1045. {
  1046. if (!zConstraints
  1047. ->getAttributeValue("allowAdditionalAttributes")
  1048. .istGleich("true"))
  1049. {
  1050. return new TypeMissmatch(path,
  1051. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1052. dynamic_cast<XML::Element*>(
  1053. zConstraints->getThis()),
  1054. new UnknownValue(p,
  1055. dynamic_cast<AbstractElement*>(
  1056. obj->zAbstractValue(i)->getThis())));
  1057. }
  1058. }
  1059. else
  1060. {
  1061. XML::Editor tmp
  1062. = zConstraints->selectChildsByAttribute("name", key);
  1063. ValidationResult* res
  1064. = validateMultipleTypes(pathToValidate,
  1065. obj->zAbstractValue(i),
  1066. tmp.begin().val(),
  1067. p);
  1068. if (!res->isValid())
  1069. {
  1070. return new TypeMissmatch(path,
  1071. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1072. dynamic_cast<XML::Element*>(
  1073. zConstraints->getThis()),
  1074. res);
  1075. }
  1076. res->release();
  1077. }
  1078. }
  1079. for (XML::Element* constraint : zConstraints->selectChildren())
  1080. {
  1081. if (!obj->hasValue(constraint->getAttributeValue("name")))
  1082. {
  1083. XML::Editor tmp = constraint->selectChildren();
  1084. bool optional = true;
  1085. std::function<void(XML::Element*)> checkOptional;
  1086. checkOptional = [&optional, &checkOptional](
  1087. XML::Element* zElement) {
  1088. if (zElement->getName().istGleich("oneOf")
  1089. && (!zElement->hasAttribute("optional")
  1090. || !zElement->getAttributeValue("optional")
  1091. .istGleich("true")))
  1092. {
  1093. XML::Editor tmp = zElement->selectChildren();
  1094. tmp.forEach(checkOptional);
  1095. }
  1096. else
  1097. {
  1098. optional &= zElement->hasAttribute("optional")
  1099. && zElement->getAttributeValue("optional")
  1100. .istGleich("true");
  1101. }
  1102. };
  1103. tmp.forEach(checkOptional);
  1104. if (!optional)
  1105. {
  1106. Text p = path;
  1107. p += ".";
  1108. p += constraint->getAttributeValue("name");
  1109. if (constraint->getChildCount() != 1)
  1110. return new TypeMissmatch(path,
  1111. dynamic_cast<AbstractElement*>(
  1112. zValue->getThis()),
  1113. dynamic_cast<XML::Element*>(
  1114. zConstraints->getThis()),
  1115. new MissingOneOf(p, tmp));
  1116. return new TypeMissmatch(path,
  1117. dynamic_cast<AbstractElement*>(zValue->getThis()),
  1118. dynamic_cast<XML::Element*>(
  1119. zConstraints->getThis()),
  1120. new MissingValue(p,
  1121. dynamic_cast<XML::Element*>(
  1122. tmp.begin()->getThis())));
  1123. }
  1124. }
  1125. }
  1126. }
  1127. break;
  1128. }
  1129. return new ValidValue(
  1130. path, dynamic_cast<AbstractElement*>(zValue->getThis()));
  1131. }
  1132. ValidationResult* DataValidator::validateMultipleTypes(
  1133. ElementPath* pathToValidate,
  1134. AbstractElement* zChildValue,
  1135. XML::Element* zPossibleChildConstraints,
  1136. Text childPath) const
  1137. {
  1138. if (zPossibleChildConstraints->getChildCount() == 1
  1139. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1140. {
  1141. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1142. return validate(pathToValidate,
  1143. zChildValue,
  1144. children.begin().val(),
  1145. childPath); // only one type is possible
  1146. }
  1147. bool hasTypeAttr = 0;
  1148. RCArray<XML::Element> possibleConstraints;
  1149. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1150. { // try to find the correct constraints based on the type attribute
  1151. hasTypeAttr = 1;
  1152. Text typeAttr
  1153. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1154. if (zChildValue->getType() == AbstractType::OBJECT)
  1155. {
  1156. const AbstractObject* obj = zChildValue->asAbstractObject();
  1157. if (obj->hasValue(typeAttr))
  1158. {
  1159. AbstractElement* typeV = obj->zAbstractValue(typeAttr);
  1160. ElementPath* tmp = ElementPathBuilder().build();
  1161. for (XML::Element* constraint :
  1162. zPossibleChildConstraints->selectChildsByName("object")
  1163. .whereChildWithAttributeExists("name", typeAttr))
  1164. {
  1165. XML::Editor nameChildren
  1166. = constraint->selectChildsByAttribute("name", typeAttr);
  1167. XML::Element* typeAttrContraints
  1168. = nameChildren.begin().val();
  1169. ValidationResult* res = validateMultipleTypes(tmp,
  1170. typeV,
  1171. typeAttrContraints,
  1172. childPath + "." + typeAttr);
  1173. if (res->isValid())
  1174. possibleConstraints.add(
  1175. dynamic_cast<XML::Element*>(constraint->getThis()));
  1176. res->release();
  1177. }
  1178. tmp->release();
  1179. }
  1180. }
  1181. }
  1182. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1183. return validate(pathToValidate,
  1184. zChildValue,
  1185. possibleConstraints.begin().val(),
  1186. childPath); // if the type is clear
  1187. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1188. { // more then one type is possible
  1189. RCArray<ValidationResult> invalidResults;
  1190. int index = pathToValidate->getCurrentPathElementIndex();
  1191. for (XML::Element* constraint : possibleConstraints)
  1192. {
  1193. pathToValidate->setCurrentPathElementIndex(index);
  1194. ValidationResult* res
  1195. = validate(pathToValidate, zChildValue, constraint, childPath);
  1196. invalidResults.add(res);
  1197. if (res->isValid())
  1198. return new ValidValue(childPath,
  1199. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1200. }
  1201. return new NoTypeMatching(childPath,
  1202. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1203. possibleConstraints,
  1204. invalidResults);
  1205. }
  1206. // try all types
  1207. possibleConstraints.leeren();
  1208. RCArray<ValidationResult> invalidResults;
  1209. int index = pathToValidate->getCurrentPathElementIndex();
  1210. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1211. {
  1212. pathToValidate->setCurrentPathElementIndex(index);
  1213. ValidationResult* res
  1214. = validate(pathToValidate, zChildValue, constraint, childPath);
  1215. invalidResults.add(res);
  1216. if (res->isValid())
  1217. return new ValidValue(childPath,
  1218. dynamic_cast<AbstractElement*>(zChildValue->getThis()));
  1219. possibleConstraints.add(
  1220. dynamic_cast<XML::Element*>(constraint->getThis()));
  1221. }
  1222. pathToValidate->setCurrentPathElementIndex(index);
  1223. if (pathToValidate->isValid())
  1224. {
  1225. return new ValidationPathNotFound(childPath,
  1226. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1227. pathToValidate->toString());
  1228. }
  1229. return new NoTypeMatching(childPath,
  1230. dynamic_cast<AbstractElement*>(zChildValue->getThis()),
  1231. possibleConstraints,
  1232. invalidResults);
  1233. }
  1234. JSON::JSONObject* Framework::Validator::DataValidator::getJsonSchema(
  1235. XML::Element* zConstraint, JSON::JSONObject* zDefs) const
  1236. {
  1237. JSON::JSONObject* result = new JSON::JSONObject();
  1238. if (zConstraint->hasAttribute("id"))
  1239. {
  1240. Text id = zConstraint->getAttributeValue("id");
  1241. id.ersetzen(" ", "_");
  1242. if (zDefs->hasValue(id))
  1243. {
  1244. Text jsonRef = Text("#/$defs/") + id;
  1245. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1246. return result;
  1247. }
  1248. zDefs->addValue(id, new JSON::JSONValue()); // avoid endless recursion
  1249. // for cyclic datastructures
  1250. }
  1251. if (zConstraint->getName().istGleich("bool"))
  1252. {
  1253. if (zConstraint->hasAttribute("equals"))
  1254. {
  1255. JSON::JSONArray* array = new JSON::JSONArray();
  1256. array->addValue(new JSON::JSONBool(
  1257. zConstraint->getAttributeValue("equals").istGleich("true")));
  1258. result->addValue("enum", array);
  1259. }
  1260. else
  1261. {
  1262. result->addValue("type", new JSON::JSONString("boolean"));
  1263. }
  1264. }
  1265. else if (zConstraint->getName().istGleich("number"))
  1266. {
  1267. if (zConstraint->hasAttribute("equals"))
  1268. {
  1269. JSON::JSONArray* array = new JSON::JSONArray();
  1270. array->addValue(new JSON::JSONNumber(
  1271. (double)zConstraint->getAttributeValue("equals")));
  1272. result->addValue("enum", array);
  1273. }
  1274. else
  1275. {
  1276. result->addValue("type", new JSON::JSONString("number"));
  1277. if (zConstraint->hasAttribute("lessOrEqual"))
  1278. {
  1279. result->addValue("maximum",
  1280. new JSON::JSONNumber(
  1281. (double)zConstraint->getAttributeValue("lessOrEqual")));
  1282. }
  1283. if (zConstraint->hasAttribute("greaterOrEqual"))
  1284. {
  1285. result->addValue("minimum",
  1286. new JSON::JSONNumber((double)zConstraint->getAttributeValue(
  1287. "greaterOrEqual")));
  1288. }
  1289. if (zConstraint->hasAttribute("less"))
  1290. {
  1291. result->addValue("maximum",
  1292. new JSON::JSONNumber(
  1293. (double)zConstraint->getAttributeValue("less")));
  1294. result->addValue("exclusiveMaximum", new JSON::JSONBool(true));
  1295. }
  1296. if (zConstraint->hasAttribute("greater"))
  1297. {
  1298. result->addValue("minimum",
  1299. new JSON::JSONNumber(
  1300. (double)zConstraint->getAttributeValue("greater")));
  1301. result->addValue("exclusiveMinimum", new JSON::JSONBool(true));
  1302. }
  1303. }
  1304. }
  1305. else if (zConstraint->getName().istGleich("string"))
  1306. {
  1307. if (zConstraint->hasAttribute("equals"))
  1308. {
  1309. JSON::JSONArray* array = new JSON::JSONArray();
  1310. array->addValue(
  1311. new JSON::JSONString(zConstraint->getAttributeValue("equals")));
  1312. result->addValue("enum", array);
  1313. }
  1314. else if (zConstraint->hasAttribute("oneOf"))
  1315. {
  1316. JSON::JSONArray* array = JSON::Parser::getValue(
  1317. zConstraint->getAttributeValue("oneOf"))
  1318. ->asArray();
  1319. result->addValue("enum", array);
  1320. }
  1321. else
  1322. {
  1323. result->addValue("type", new JSON::JSONString("string"));
  1324. if (zConstraint->hasAttribute("contains"))
  1325. {
  1326. result->addValue("pattern",
  1327. new JSON::JSONString(
  1328. Text(".*")
  1329. + Regex::quote(
  1330. zConstraint->getAttributeValue("contains"))
  1331. + ".*"));
  1332. }
  1333. if (zConstraint->hasAttribute("startsWith"))
  1334. {
  1335. result->addValue("pattern",
  1336. new JSON::JSONString(
  1337. Text("^")
  1338. + Regex::quote(
  1339. zConstraint->getAttributeValue("startsWith"))
  1340. + ".*"));
  1341. }
  1342. if (zConstraint->hasAttribute("endsWith"))
  1343. {
  1344. result->addValue("pattern",
  1345. new JSON::JSONString(
  1346. Text(".*")
  1347. + Regex::quote(
  1348. zConstraint->getAttributeValue("endsWith"))
  1349. + "$"));
  1350. }
  1351. }
  1352. }
  1353. else if (zConstraint->getName().istGleich("ref"))
  1354. {
  1355. Text id = zConstraint->getAttributeValue("ref");
  1356. XML::Element* e = typeConstraints->z(id, id.getLength());
  1357. id.ersetzen(" ", "_");
  1358. if (!zDefs->hasValue(id))
  1359. {
  1360. JSON::JSONObject* def = getJsonSchema(e, zDefs);
  1361. if (!def->hasValue(id))
  1362. {
  1363. zDefs->addValue(id, def);
  1364. }
  1365. else
  1366. {
  1367. // def was already added by a child
  1368. def->release();
  1369. }
  1370. }
  1371. Text jsonRef = Text("#/$defs/") + id;
  1372. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1373. }
  1374. else if (zConstraint->getName().istGleich("object"))
  1375. {
  1376. result->addValue("type", new JSON::JSONString("object"));
  1377. result->addValue("additionalProperties",
  1378. new JSON::JSONBool(
  1379. zConstraint->getAttributeValue("allowAdditionalAttributes")
  1380. .istGleich("true")));
  1381. JSON::JSONObject* properties = new JSON::JSONObject();
  1382. JSON::JSONArray* required = new JSON::JSONArray();
  1383. for (XML::Element* e : zConstraint->selectChildren())
  1384. {
  1385. JSON::JSONObject* prop = new JSON::JSONObject();
  1386. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1387. bool isRequired = true;
  1388. for (XML::Element* child : e->selectChildren())
  1389. {
  1390. oneOf->addValue(getJsonSchema(child, zDefs));
  1391. isRequired
  1392. &= !child->getAttributeValue("optional").istGleich("true")
  1393. && !child->hasAttribute("default");
  1394. }
  1395. prop->addValue("oneOf", oneOf);
  1396. properties->addValue(e->getAttributeValue("name"), prop);
  1397. if (isRequired)
  1398. {
  1399. required->addValue(
  1400. new JSON::JSONString(e->getAttributeValue("name")));
  1401. }
  1402. }
  1403. result->addValue("properties", properties);
  1404. result->addValue("required", required);
  1405. }
  1406. else if (zConstraint->getName().istGleich("array"))
  1407. {
  1408. result->addValue("type", new JSON::JSONString("array"));
  1409. JSON::JSONObject* items = new JSON::JSONObject();
  1410. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1411. for (XML::Element* e : zConstraint->selectChildren())
  1412. {
  1413. oneOf->addValue(getJsonSchema(e, zDefs));
  1414. }
  1415. items->addValue("oneOf", oneOf);
  1416. result->addValue("items", items);
  1417. }
  1418. else if (zConstraint->getName().istGleich("oneOf"))
  1419. {
  1420. JSON::JSONArray* oneOf = new JSON::JSONArray();
  1421. for (XML::Element* e : zConstraint->selectChildren())
  1422. {
  1423. oneOf->addValue(getJsonSchema(e, zDefs));
  1424. }
  1425. result->addValue("oneOf", oneOf);
  1426. }
  1427. if (zConstraint->hasAttribute("nullable")
  1428. && zConstraint->getAttributeValue("nullable").istGleich("true"))
  1429. {
  1430. if (!result->hasValue("type"))
  1431. {
  1432. if (result->hasValue("enum"))
  1433. {
  1434. result->zValue("enum")->asArray()->addValue(
  1435. new JSON::JSONValue());
  1436. }
  1437. else
  1438. {
  1439. JSON::JSONObject* oneOf = new JSON::JSONObject();
  1440. JSON::JSONArray* array = new JSON::JSONArray();
  1441. array->addValue(result);
  1442. JSON::JSONObject* nullType = new JSON::JSONObject();
  1443. nullType->addValue("type", new JSON::JSONString("null"));
  1444. array->addValue(nullType);
  1445. oneOf->addValue("oneOf", array);
  1446. result = oneOf;
  1447. }
  1448. }
  1449. else
  1450. {
  1451. if (result->zValue("type")->getType() == AbstractType::ARRAY)
  1452. {
  1453. result->zValue("type")->asArray()->addValue(
  1454. new JSON::JSONString("null"));
  1455. }
  1456. else
  1457. {
  1458. JSON::JSONArray* array = new JSON::JSONArray();
  1459. array->addValue(result->getValue("type"));
  1460. array->addValue(new JSON::JSONString("null"));
  1461. result->removeValue("type");
  1462. result->addValue("type", array);
  1463. }
  1464. }
  1465. }
  1466. if (zConstraint->hasAttribute("id"))
  1467. {
  1468. Text id = zConstraint->getAttributeValue("id");
  1469. id.ersetzen(" ", "_");
  1470. zDefs->removeValue(id);
  1471. zDefs->addValue(id, result);
  1472. result = new JSON::JSONObject();
  1473. Text jsonRef = Text("#/$defs/") + id;
  1474. result->addValue("$ref", new JSON::JSONString(jsonRef));
  1475. }
  1476. return result;
  1477. }
  1478. StringValidationBuilder<DataValidator>* DataValidator::buildForString()
  1479. {
  1480. return new StringValidationBuilder<DataValidator>(
  1481. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1482. }
  1483. NumberValidationBuilder<DataValidator>* DataValidator::buildForNumber()
  1484. {
  1485. return new NumberValidationBuilder<DataValidator>(
  1486. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1487. }
  1488. BoolValidationBuilder<DataValidator>* DataValidator::buildForBool()
  1489. {
  1490. return new BoolValidationBuilder<DataValidator>(
  1491. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1492. }
  1493. ObjectValidationBuilder<DataValidator>* DataValidator::buildForObject()
  1494. {
  1495. return new ObjectValidationBuilder<DataValidator>(
  1496. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1497. }
  1498. ArrayValidationBuilder<DataValidator>* DataValidator::buildForArray()
  1499. {
  1500. return new ArrayValidationBuilder<DataValidator>(
  1501. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1502. }
  1503. DataValidator* DataValidator::buildForReference(Text id)
  1504. {
  1505. return new DataValidator(
  1506. new XML::Element(Text("<ref ref=\"") + id + Text("\"/>")));
  1507. }
  1508. OneOfValidationBuilder<DataValidator>* DataValidator::buildForOneOf()
  1509. {
  1510. return new OneOfValidationBuilder<DataValidator>(
  1511. [](XML::Element& e) { return new DataValidator(e.dublicate()); });
  1512. }