DataValidator.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. #pragma once
  2. #include <source_location>
  3. #include "AbstractElement.h"
  4. #include "Array.h"
  5. #include "JSON.h"
  6. #include "Trie.h"
  7. #include "XML.h"
  8. namespace Framework
  9. {
  10. namespace Validator
  11. {
  12. class ValidationResult : public Framework::ReferenceCounter
  13. {
  14. public:
  15. __declspec(dllexport) ValidationResult();
  16. __declspec(dllexport) virtual ~ValidationResult();
  17. virtual bool isValid() const = 0;
  18. __declspec(dllexport) void logInvalidInfo(
  19. std::source_location location
  20. = std::source_location::current()) const;
  21. virtual Text getInvalidInfo(int indent = 0) const = 0;
  22. virtual JSON::JSONValue* getValidPart(
  23. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  24. = 0;
  25. virtual Text getPath() const = 0;
  26. virtual void addBasePath(Text basePath) = 0;
  27. virtual bool isDifferent(const ValidationResult* zResult) const = 0;
  28. };
  29. class TypeMissmatch : public ValidationResult
  30. {
  31. private:
  32. Text path;
  33. AbstractElement* foundValue;
  34. XML::Element* expected;
  35. ValidationResult* reason;
  36. public:
  37. __declspec(dllexport) TypeMissmatch(Text path,
  38. AbstractElement* foundValue,
  39. XML::Element* expected,
  40. ValidationResult* reason);
  41. __declspec(dllexport) ~TypeMissmatch();
  42. __declspec(dllexport) bool isValid() const override;
  43. __declspec(dllexport) Text getInvalidInfo(
  44. int indent) const override;
  45. protected:
  46. __declspec(dllexport) JSON::JSONValue* getValidPart(
  47. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  48. override;
  49. __declspec(dllexport) Text getPath() const override;
  50. __declspec(dllexport) bool isDifferent(
  51. const ValidationResult* zResult) const override;
  52. __declspec(dllexport) void addBasePath(Text basePath) override;
  53. };
  54. class UnknownValue : public ValidationResult
  55. {
  56. private:
  57. Text path;
  58. AbstractElement* foundValue;
  59. public:
  60. __declspec(dllexport) UnknownValue(
  61. Text path, AbstractElement* foundValue);
  62. __declspec(dllexport) ~UnknownValue();
  63. __declspec(dllexport) bool isValid() const override;
  64. __declspec(dllexport) Text getInvalidInfo(
  65. int indent) const override;
  66. __declspec(dllexport) JSON::JSONValue* getValidPart(
  67. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  68. override;
  69. __declspec(dllexport) Text getPath() const override;
  70. __declspec(dllexport) bool isDifferent(
  71. const ValidationResult* zResult) const override;
  72. __declspec(dllexport) void addBasePath(Text basePath) override;
  73. };
  74. class MissingValue : public ValidationResult
  75. {
  76. private:
  77. Text path;
  78. XML::Element* expected;
  79. public:
  80. __declspec(dllexport) MissingValue(
  81. Text path, XML::Element* expected);
  82. __declspec(dllexport) ~MissingValue();
  83. __declspec(dllexport) bool isValid() const override;
  84. __declspec(dllexport) Text getInvalidInfo(
  85. int indent) const override;
  86. __declspec(dllexport) JSON::JSONValue* getValidPart(
  87. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  88. override;
  89. __declspec(dllexport) Text getPath() const override;
  90. __declspec(dllexport) bool isDifferent(
  91. const ValidationResult* zResult) const override;
  92. __declspec(dllexport) void addBasePath(Text basePath) override;
  93. };
  94. class MissingOneOf : public ValidationResult
  95. {
  96. private:
  97. Text path;
  98. RCArray<XML::Element> expected;
  99. public:
  100. __declspec(dllexport) MissingOneOf(Text path, XML::Editor expected);
  101. __declspec(dllexport) ~MissingOneOf();
  102. __declspec(dllexport) bool isValid() const override;
  103. __declspec(dllexport) Text getInvalidInfo(
  104. int indent) const override;
  105. __declspec(dllexport) JSON::JSONValue* getValidPart(
  106. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  107. override;
  108. __declspec(dllexport) Text getPath() const override;
  109. __declspec(dllexport) bool isDifferent(
  110. const ValidationResult* zResult) const override;
  111. __declspec(dllexport) void addBasePath(Text basePath) override;
  112. };
  113. class NoTypeMatching : public ValidationResult
  114. {
  115. private:
  116. Text path;
  117. AbstractElement* foundValue;
  118. RCArray<XML::Element> expected;
  119. RCArray<ValidationResult> reasons;
  120. public:
  121. __declspec(dllexport) NoTypeMatching(Text path,
  122. AbstractElement* foundValue,
  123. RCArray<XML::Element>& expected,
  124. RCArray<ValidationResult>& reasons);
  125. __declspec(dllexport) ~NoTypeMatching();
  126. __declspec(dllexport) bool isValid() const override;
  127. __declspec(dllexport) Text getInvalidInfo(
  128. int indent) const override;
  129. __declspec(dllexport) JSON::JSONValue* getValidPart(
  130. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  131. override;
  132. __declspec(dllexport) Text getPath() const override;
  133. __declspec(dllexport) bool isDifferent(
  134. const ValidationResult* zResult) const override;
  135. __declspec(dllexport) void addBasePath(Text basePath) override;
  136. };
  137. class ValidationPathNotFound : public ValidationResult
  138. {
  139. private:
  140. Text path;
  141. AbstractElement* foundValue;
  142. Text validationPath;
  143. public:
  144. __declspec(dllexport) ValidationPathNotFound(
  145. Text path, AbstractElement* foundValue, Text validationPath);
  146. __declspec(dllexport) ~ValidationPathNotFound();
  147. __declspec(dllexport) bool isValid() const override;
  148. __declspec(dllexport) Text getInvalidInfo(
  149. int indent) const override;
  150. __declspec(dllexport) JSON::JSONValue* getValidPart(
  151. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  152. override;
  153. __declspec(dllexport) Text getPath() const override;
  154. __declspec(dllexport) bool isDifferent(
  155. const ValidationResult* zResult) const override;
  156. __declspec(dllexport) void addBasePath(Text basePath) override;
  157. };
  158. class ValidValue : public ValidationResult
  159. {
  160. private:
  161. Text path;
  162. AbstractElement* value;
  163. public:
  164. __declspec(dllexport) ValidValue(Text path, AbstractElement* value);
  165. __declspec(dllexport) ~ValidValue();
  166. __declspec(dllexport) bool isValid() const override;
  167. __declspec(dllexport) Text getInvalidInfo(
  168. int indent) const override;
  169. __declspec(dllexport) JSON::JSONValue* getValidPart(
  170. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  171. override;
  172. __declspec(dllexport) Text getPath() const override;
  173. __declspec(dllexport) bool isDifferent(
  174. const ValidationResult* zResult) const override;
  175. __declspec(dllexport) void addBasePath(Text basePath) override;
  176. };
  177. template<typename T> class StringValidationBuilder;
  178. template<typename T> class NumberValidationBuilder;
  179. template<typename T> class BoolValidationBuilder;
  180. template<typename T> class ObjectValidationBuilder;
  181. template<typename T> class ArrayValidationBuilder;
  182. template<typename T> class OneOfValidationBuilder;
  183. class DataValidator : public Framework::ReferenceCounter
  184. {
  185. private:
  186. XML::Element* constraints;
  187. RCTrie<XML::Element>* typeConstraints;
  188. public:
  189. __declspec(dllexport) DataValidator(XML::Element* constraints);
  190. __declspec(dllexport) DataValidator(XML::Element* constraints,
  191. RCTrie<XML::Element>* typeConstraints);
  192. __declspec(dllexport) ~DataValidator();
  193. __declspec(dllexport) ValidationResult* validate(
  194. AbstractElement* zValue) const;
  195. __declspec(dllexport) ValidationResult* validate(
  196. ElementPath* path, AbstractElement* zValue) const;
  197. __declspec(dllexport) bool isValid(AbstractElement* zValue) const;
  198. /**
  199. * returns the valid part of the json by performing the
  200. * following operations in the specified order untill no further
  201. * changes are made:
  202. * - invalid or unknown object properties are removed
  203. * - missing object properties with default values are added if
  204. * missing
  205. * - invalid array elements are removed
  206. *
  207. * @param zValue the json value to to extract the valid part
  208. * without increased reference counter
  209. * @return the valid part or 0 if no valid part exists
  210. */
  211. __declspec(dllexport) JSON::JSONValue* getValidParts(
  212. JSON::JSONValue* zValue,
  213. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  214. const;
  215. __declspec(dllexport) XML::Element* zConstraints();
  216. /**
  217. * returns the json schema for the constraints
  218. */
  219. __declspec(dllexport) JSON::JSONObject* getJsonSchema() const;
  220. /**
  221. * updates the validator for the datatype with a specified reference
  222. * id.
  223. *
  224. * \param id the reference id of the datatype
  225. * \param validator the validator that will validate a type with the
  226. * given reference id
  227. */
  228. __declspec(dllexport) void updateValidator(
  229. Text id, DataValidator* validator);
  230. private:
  231. __declspec(dllexport) ValidationResult* validate(
  232. ElementPath* pathToValidate,
  233. AbstractElement* zValue,
  234. XML::Element* zConstraints,
  235. Text path) const;
  236. __declspec(dllexport) ValidationResult* validateMultipleTypes(
  237. ElementPath* pathToValidate,
  238. AbstractElement* zChildValue,
  239. XML::Element* zPossibleChildConstraints,
  240. Text childPath) const;
  241. __declspec(dllexport) JSON::JSONObject* getJsonSchema(
  242. XML::Element* zConstraint, JSON::JSONObject* zDefs) const;
  243. public:
  244. __declspec(dllexport) static StringValidationBuilder<DataValidator>*
  245. buildForString();
  246. __declspec(dllexport) static NumberValidationBuilder<DataValidator>*
  247. buildForNumber();
  248. __declspec(dllexport) static BoolValidationBuilder<DataValidator>*
  249. buildForBool();
  250. __declspec(dllexport) static ObjectValidationBuilder<DataValidator>*
  251. buildForObject();
  252. __declspec(dllexport) static ArrayValidationBuilder<DataValidator>*
  253. buildForArray();
  254. __declspec(dllexport) static OneOfValidationBuilder<DataValidator>*
  255. buildForOneOf();
  256. __declspec(dllexport) static DataValidator* buildForReference(
  257. Text id);
  258. };
  259. template<typename T> class StringValidationBuilder
  260. {
  261. private:
  262. XML::Element element;
  263. std::function<T*(XML::Element& element)> builder;
  264. public:
  265. StringValidationBuilder(
  266. std::function<T*(XML::Element& element)> builder)
  267. : element("<string/>"),
  268. builder(builder)
  269. {}
  270. StringValidationBuilder<T>* setReferenceId(Text id)
  271. {
  272. element.setAttribute("id", id);
  273. return this;
  274. }
  275. StringValidationBuilder<T>* withExactMatch(Text value)
  276. {
  277. element.setAttribute("equals", value);
  278. return this;
  279. }
  280. StringValidationBuilder<T>* whichContainsMatch(Text value)
  281. {
  282. element.setAttribute("contains", value);
  283. return this;
  284. }
  285. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  286. {
  287. element.setAttribute("startsWith", value);
  288. return this;
  289. }
  290. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  291. {
  292. element.setAttribute("endsWith", value);
  293. return this;
  294. }
  295. StringValidationBuilder<T>* whichIsOneOf(RCArray<Text> values)
  296. {
  297. JSON::JSONArray arr;
  298. for (Text* str : values)
  299. arr.addValue(new JSON::JSONString(str->getText()));
  300. element.setAttribute("oneOf", arr.toString());
  301. return this;
  302. }
  303. StringValidationBuilder<T>* whichIsOneOf(
  304. std::initializer_list<Text> values)
  305. {
  306. JSON::JSONArray arr;
  307. for (Text str : values)
  308. arr.addValue(new JSON::JSONString(str));
  309. element.setAttribute("oneOf", arr.toString());
  310. return this;
  311. }
  312. StringValidationBuilder<T>* whichIs(Text value)
  313. {
  314. JSON::JSONArray arr;
  315. arr.addValue(new JSON::JSONString(value));
  316. element.setAttribute("oneOf", arr.toString());
  317. return this;
  318. }
  319. StringValidationBuilder<T>* withDefault(Text value)
  320. {
  321. element.setAttribute(
  322. "default", JSON::JSONString(value).toString());
  323. return this;
  324. }
  325. StringValidationBuilder<T>* withDefaultNull()
  326. {
  327. element.setAttribute("default", "null");
  328. return this;
  329. }
  330. StringValidationBuilder<T>* whichCanBeNull()
  331. {
  332. element.setAttribute("nullable", "true");
  333. return this;
  334. }
  335. StringValidationBuilder<T>* whichIsOptional()
  336. {
  337. element.setAttribute("optional", "true");
  338. return this;
  339. }
  340. T* finishString()
  341. {
  342. T* result = builder(element);
  343. delete this;
  344. return result;
  345. }
  346. };
  347. template<typename T> class NumberValidationBuilder
  348. {
  349. private:
  350. XML::Element element;
  351. std::function<T*(XML::Element& element)> builder;
  352. public:
  353. NumberValidationBuilder(
  354. std::function<T*(XML::Element& element)> builder)
  355. : element("<number/>"),
  356. builder(builder)
  357. {}
  358. NumberValidationBuilder<T>* setReferenceId(Text id)
  359. {
  360. element.setAttribute("id", id);
  361. return this;
  362. }
  363. NumberValidationBuilder<T>* whichIs(double value)
  364. {
  365. element.setAttribute("equals", Text(value));
  366. return this;
  367. }
  368. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  369. {
  370. element.setAttribute("lessOrEqual", Text(value));
  371. return this;
  372. }
  373. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  374. {
  375. element.setAttribute("greaterOrEqual", Text(value));
  376. return this;
  377. }
  378. NumberValidationBuilder<T>* whichIsLessThen(double value)
  379. {
  380. element.setAttribute("less", Text(value));
  381. return this;
  382. }
  383. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  384. {
  385. element.setAttribute("greater", Text(value));
  386. return this;
  387. }
  388. NumberValidationBuilder<T>* withDefault(double value)
  389. {
  390. element.setAttribute(
  391. "default", JSON::JSONNumber(value).toString());
  392. return this;
  393. }
  394. NumberValidationBuilder<T>* withDefaultNull()
  395. {
  396. element.setAttribute("default", "null");
  397. return this;
  398. }
  399. NumberValidationBuilder<T>* whichCanBeNull()
  400. {
  401. element.setAttribute("nullable", "true");
  402. return this;
  403. }
  404. NumberValidationBuilder<T>* whichIsOptional()
  405. {
  406. element.setAttribute("optional", "true");
  407. return this;
  408. }
  409. T* finishNumber()
  410. {
  411. T* result = builder(element);
  412. delete this;
  413. return result;
  414. }
  415. };
  416. template<typename T> class BoolValidationBuilder
  417. {
  418. private:
  419. XML::Element element;
  420. std::function<T*(XML::Element& element)> builder;
  421. public:
  422. BoolValidationBuilder(
  423. std::function<T*(XML::Element& element)> builder)
  424. : element("<bool/>"),
  425. builder(builder)
  426. {}
  427. BoolValidationBuilder<T>* setReferenceId(Text id)
  428. {
  429. element.setAttribute("id", id);
  430. return this;
  431. }
  432. BoolValidationBuilder<T>* whichIs(bool value)
  433. {
  434. element.setAttribute("equals", value ? "true" : "false");
  435. return this;
  436. }
  437. BoolValidationBuilder<T>* withDefault(bool value)
  438. {
  439. element.setAttribute(
  440. "default", JSON::JSONBool(value).toString());
  441. return this;
  442. }
  443. BoolValidationBuilder<T>* withDefaultNull()
  444. {
  445. element.setAttribute("default", "null");
  446. return this;
  447. }
  448. BoolValidationBuilder<T>* whichCanBeNull()
  449. {
  450. element.setAttribute("nullable", "true");
  451. return this;
  452. }
  453. BoolValidationBuilder<T>* whichIsOptional()
  454. {
  455. element.setAttribute("optional", "true");
  456. return this;
  457. }
  458. T* finishBool()
  459. {
  460. T* result = builder(element);
  461. delete this;
  462. return result;
  463. }
  464. };
  465. template<typename T> class ArrayValidationBuilder;
  466. template<typename T> class ObjectValidationBuilder
  467. {
  468. private:
  469. XML::Element element;
  470. std::function<T*(XML::Element& element)> builder;
  471. public:
  472. ObjectValidationBuilder(
  473. std::function<T*(XML::Element& element)> builder)
  474. : element("<object></object>"),
  475. builder(builder)
  476. {}
  477. ObjectValidationBuilder<T>* setReferenceId(Text id)
  478. {
  479. element.setAttribute("id", id);
  480. return this;
  481. }
  482. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  483. withRequiredNumber(Text name)
  484. {
  485. return new NumberValidationBuilder<ObjectValidationBuilder<T>>(
  486. [this, name](XML::Element& e) {
  487. if (!element.selectChildsByAttribute("name", name)
  488. .exists())
  489. {
  490. XML::Element* attr
  491. = new XML::Element("<value></value>");
  492. attr->setAttribute("name", name);
  493. element.addChild(attr);
  494. }
  495. element.selectChildsByAttribute("name", name)
  496. .addChild(e.dublicate());
  497. return this;
  498. });
  499. }
  500. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  501. withOptionalNumber(Text name)
  502. {
  503. return withRequiredNumber(name)->whichIsOptional();
  504. }
  505. StringValidationBuilder<ObjectValidationBuilder<T>>*
  506. withRequiredString(Text name)
  507. {
  508. return new StringValidationBuilder<ObjectValidationBuilder<T>>(
  509. [this, name](XML::Element& e) {
  510. if (!element.selectChildsByAttribute("name", name)
  511. .exists())
  512. {
  513. XML::Element* attr
  514. = new XML::Element("<value></value>");
  515. attr->setAttribute("name", name);
  516. element.addChild(attr);
  517. }
  518. element.selectChildsByAttribute("name", name)
  519. .addChild(e.dublicate());
  520. return this;
  521. });
  522. }
  523. StringValidationBuilder<ObjectValidationBuilder<T>>*
  524. withOptionalString(Text name)
  525. {
  526. return withRequiredString(name)->whichIsOptional();
  527. }
  528. BoolValidationBuilder<ObjectValidationBuilder<T>>* withRequiredBool(
  529. Text name)
  530. {
  531. return new BoolValidationBuilder<ObjectValidationBuilder<T>>(
  532. [this, name](XML::Element& e) {
  533. if (!element.selectChildsByAttribute("name", name)
  534. .exists())
  535. {
  536. XML::Element* attr
  537. = new XML::Element("<value></value>");
  538. attr->setAttribute("name", name);
  539. element.addChild(attr);
  540. }
  541. element.selectChildsByAttribute("name", name)
  542. .addChild(e.dublicate());
  543. return this;
  544. });
  545. }
  546. BoolValidationBuilder<ObjectValidationBuilder<T>>* withOptionalBool(
  547. Text name)
  548. {
  549. return withRequiredBool(name)->whichIsOptional();
  550. }
  551. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  552. withRequiredArray(Text name)
  553. {
  554. return new ArrayValidationBuilder<ObjectValidationBuilder<T>>(
  555. [this, name](XML::Element& e) {
  556. if (!element.selectChildsByAttribute("name", name)
  557. .exists())
  558. {
  559. XML::Element* attr
  560. = new XML::Element("<value></value>");
  561. attr->setAttribute("name", name);
  562. element.addChild(attr);
  563. }
  564. element.selectChildsByAttribute("name", name)
  565. .addChild(e.dublicate());
  566. return this;
  567. });
  568. }
  569. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  570. withOptionalArray(Text name)
  571. {
  572. return withRequiredArray(name)->whichIsOptional();
  573. }
  574. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  575. withRequiredObject(Text name)
  576. {
  577. return new ObjectValidationBuilder<ObjectValidationBuilder<T>>(
  578. [this, name](XML::Element& e) {
  579. if (!element.selectChildsByAttribute("name", name)
  580. .exists())
  581. {
  582. XML::Element* attr
  583. = new XML::Element("<value></value>");
  584. attr->setAttribute("name", name);
  585. element.addChild(attr);
  586. }
  587. element.selectChildsByAttribute("name", name)
  588. .addChild(e.dublicate());
  589. return this;
  590. });
  591. }
  592. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  593. withOptionalObject(Text name)
  594. {
  595. return withRequiredObject(name)->whichIsOptional();
  596. }
  597. ObjectValidationBuilder<T>* withRequiredAttribute(
  598. Text name, DataValidator* validator)
  599. {
  600. if (!element.selectChildsByAttribute("name", name).exists())
  601. {
  602. XML::Element* attr = new XML::Element("<value></value>");
  603. attr->setAttribute("name", name);
  604. element.addChild(attr);
  605. }
  606. element.selectChildsByAttribute("name", name)
  607. .addChild(validator->zConstraints()->dublicate());
  608. validator->release();
  609. return this;
  610. }
  611. ObjectValidationBuilder<T>* withOptionalAttribute(
  612. Text name, DataValidator* validator)
  613. {
  614. return withRequiredAttribute(name, validator, false, true);
  615. }
  616. ObjectValidationBuilder<T>* withRequiredAttribute(Text name,
  617. DataValidator* validator,
  618. bool canBeNull,
  619. bool optional)
  620. {
  621. if (!element.selectChildsByAttribute("name", name).exists())
  622. {
  623. XML::Element* attr = new XML::Element("<value></value>");
  624. attr->setAttribute("name", name);
  625. element.addChild(attr);
  626. }
  627. XML::Element* element = validator->zConstraints()->dublicate();
  628. element->setAttribute("nullable", canBeNull ? "true" : "false");
  629. element->setAttribute("optional", optional ? "true" : "false");
  630. this->element.selectChildsByAttribute("name", name)
  631. .addChild(element);
  632. validator->release();
  633. return this;
  634. }
  635. ObjectValidationBuilder<T>* withDefault(JSON::JSONObject* obj)
  636. {
  637. element.setAttribute("default", obj->toString());
  638. obj->release();
  639. return this;
  640. }
  641. ObjectValidationBuilder<T>* withDefaultNull()
  642. {
  643. element.setAttribute("default", "null");
  644. return this;
  645. }
  646. ObjectValidationBuilder<T>* whichCanBeNull()
  647. {
  648. element.setAttribute("nullable", "true");
  649. return this;
  650. }
  651. ObjectValidationBuilder<T>* whichIsOptional()
  652. {
  653. element.setAttribute("optional", "true");
  654. return this;
  655. }
  656. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  657. Text valueName, Text attributeName)
  658. {
  659. if (!element.selectChildsByAttribute("name", valueName)
  660. .exists())
  661. {
  662. XML::Element* attr = new XML::Element("<value></value>");
  663. attr->setAttribute("name", valueName);
  664. element.addChild(attr);
  665. }
  666. element.selectChildsByAttribute("name", valueName)
  667. .setAttribute("typeSpecifiedBy", attributeName);
  668. return this;
  669. }
  670. ObjectValidationBuilder<T>* removeInvalidEntries()
  671. {
  672. element.setAttribute("removeInvalidEntries", "true");
  673. return this;
  674. }
  675. ObjectValidationBuilder<T>* allowAdditionalAttriutes()
  676. {
  677. element.setAttribute("allowAdditionalAttributes", "true");
  678. return this;
  679. }
  680. T* finishObject()
  681. {
  682. T* result = builder(element);
  683. delete this;
  684. return result;
  685. }
  686. };
  687. template<typename T> class ArrayValidationBuilder
  688. {
  689. private:
  690. XML::Element element;
  691. std::function<T*(XML::Element& element)> builder;
  692. public:
  693. ArrayValidationBuilder(
  694. std::function<T*(XML::Element& element)> builder)
  695. : element("<array></array>"),
  696. builder(builder)
  697. {}
  698. StringValidationBuilder<ArrayValidationBuilder<T>>*
  699. addAcceptedStringInArray()
  700. {
  701. return new StringValidationBuilder<ArrayValidationBuilder<T>>(
  702. [this](XML::Element& e) {
  703. element.addChild(e.dublicate());
  704. return this;
  705. });
  706. }
  707. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  708. addAcceptedNumberInArray()
  709. {
  710. return new NumberValidationBuilder<ArrayValidationBuilder<T>>(
  711. [this](XML::Element& e) {
  712. element.addChild(e.dublicate());
  713. return this;
  714. });
  715. }
  716. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  717. addAcceptedBooleanInArray()
  718. {
  719. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  720. [this](XML::Element& e) {
  721. element.addChild(e.dublicate());
  722. return this;
  723. });
  724. }
  725. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  726. addAcceptedObjectInArray()
  727. {
  728. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>(
  729. [this](XML::Element& e) {
  730. element.addChild(e.dublicate());
  731. return this;
  732. });
  733. }
  734. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  735. addAcceptedArrayInArray()
  736. {
  737. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>(
  738. [this](XML::Element& e) {
  739. element.addChild(e.dublicate());
  740. return this;
  741. });
  742. }
  743. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  744. DataValidator* validator)
  745. {
  746. element.addChild(validator->zConstraints()->dublicate());
  747. validator->release();
  748. return this;
  749. }
  750. ArrayValidationBuilder<T>* acceptNullsInArray()
  751. {
  752. element.setAttribute("nullsEnabled", "true");
  753. return this;
  754. }
  755. ArrayValidationBuilder<T>* withDefault(JSON::JSONArray* array)
  756. {
  757. element.setAttribute("default", array->toString());
  758. array->release();
  759. return this;
  760. }
  761. ArrayValidationBuilder<T>* withDefaultNull()
  762. {
  763. element.setAttribute("default", "null");
  764. return this;
  765. }
  766. ArrayValidationBuilder<T>* whichCanBeNull()
  767. {
  768. element.setAttribute("nullable", "true");
  769. return this;
  770. }
  771. ArrayValidationBuilder<T>* whichIsOptional()
  772. {
  773. element.setAttribute("optional", "true");
  774. return this;
  775. }
  776. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  777. {
  778. element.setAttribute("typeSpecifiedBy", name);
  779. return this;
  780. }
  781. ArrayValidationBuilder<T>* removeInvalidEntries()
  782. {
  783. element.setAttribute("removeInvalidEntries", "true");
  784. return this;
  785. }
  786. ArrayValidationBuilder<T>* withMinSize(int size)
  787. {
  788. element.setAttribute("minSize", Text(size));
  789. return this;
  790. }
  791. ArrayValidationBuilder<T>* withMaxSize(int size)
  792. {
  793. element.setAttribute("maxSize", Text(size));
  794. return this;
  795. }
  796. ArrayValidationBuilder<T>* withRequiredSize(int size)
  797. {
  798. element.setAttribute("minSize", Text(size));
  799. element.setAttribute("maxSize", Text(size));
  800. return this;
  801. }
  802. T* finishArray()
  803. {
  804. T* result = builder(element);
  805. delete this;
  806. return result;
  807. }
  808. };
  809. template<typename T> class OneOfValidationBuilder
  810. {
  811. private:
  812. XML::Element element;
  813. std::function<T*(XML::Element& element)> builder;
  814. public:
  815. OneOfValidationBuilder(
  816. std::function<T*(XML::Element& element)> builder)
  817. : element("<oneOf></oneOf>"),
  818. builder(builder)
  819. {}
  820. StringValidationBuilder<OneOfValidationBuilder<T>>*
  821. addAcceptedString()
  822. {
  823. return new StringValidationBuilder<OneOfValidationBuilder<T>>(
  824. [this](XML::Element& e) {
  825. element.addChild(e.dublicate());
  826. return this;
  827. });
  828. }
  829. NumberValidationBuilder<OneOfValidationBuilder<T>>*
  830. addAcceptedNumber()
  831. {
  832. return new NumberValidationBuilder<OneOfValidationBuilder<T>>(
  833. [this](XML::Element& e) {
  834. element.addChild(e.dublicate());
  835. return this;
  836. });
  837. }
  838. BoolValidationBuilder<OneOfValidationBuilder<T>>*
  839. addAcceptedBoolean()
  840. {
  841. return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
  842. [this](XML::Element& e) {
  843. element.addChild(e.dublicate());
  844. return this;
  845. });
  846. }
  847. ObjectValidationBuilder<OneOfValidationBuilder<T>>*
  848. addAcceptedObject()
  849. {
  850. return new ObjectValidationBuilder<OneOfValidationBuilder<T>>(
  851. [this](XML::Element& e) {
  852. element.addChild(e.dublicate());
  853. return this;
  854. });
  855. }
  856. ArrayValidationBuilder<OneOfValidationBuilder<T>>*
  857. addAcceptedArray()
  858. {
  859. return new ArrayValidationBuilder<OneOfValidationBuilder<T>>(
  860. [this](XML::Element& e) {
  861. element.addChild(e.dublicate());
  862. return this;
  863. });
  864. }
  865. OneOfValidationBuilder<T>* addAcceptedType(DataValidator* validator)
  866. {
  867. element.addChild(validator->zConstraints()->dublicate());
  868. validator->release();
  869. return this;
  870. }
  871. OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  872. {
  873. element.setAttribute("typeSpecifiedBy", name);
  874. return this;
  875. }
  876. T* finishOneOf()
  877. {
  878. T* result = builder(element);
  879. delete this;
  880. return result;
  881. }
  882. };
  883. } // namespace Validator
  884. } // namespace Framework