DataValidator.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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* zFoundValue,
  39. XML::Element* expected,
  40. ValidationResult* reason);
  41. __declspec(dllexport) ~TypeMissmatch();
  42. __declspec(dllexport) bool isValid() const override;
  43. __declspec(dllexport) Text
  44. getInvalidInfo(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)
  61. UnknownValue(Text path, AbstractElement* zFoundValue);
  62. __declspec(dllexport) ~UnknownValue();
  63. __declspec(dllexport) bool isValid() const override;
  64. __declspec(dllexport) Text
  65. getInvalidInfo(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)
  81. MissingValue(Text path, XML::Element* expected);
  82. __declspec(dllexport) ~MissingValue();
  83. __declspec(dllexport) bool isValid() const override;
  84. __declspec(dllexport) Text
  85. getInvalidInfo(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
  104. getInvalidInfo(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* zFoundValue,
  123. RCArray<XML::Element>& expected,
  124. RCArray<ValidationResult>& reasons);
  125. __declspec(dllexport) ~NoTypeMatching();
  126. __declspec(dllexport) bool isValid() const override;
  127. __declspec(dllexport) Text
  128. getInvalidInfo(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* zFoundValue, Text validationPath);
  146. __declspec(dllexport) ~ValidationPathNotFound();
  147. __declspec(dllexport) bool isValid() const override;
  148. __declspec(dllexport) Text
  149. getInvalidInfo(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)
  165. ValidValue(Text path, AbstractElement* zValue);
  166. __declspec(dllexport) ~ValidValue();
  167. __declspec(dllexport) bool isValid() const override;
  168. __declspec(dllexport) Text
  169. getInvalidInfo(int indent) const override;
  170. __declspec(dllexport) JSON::JSONValue* getValidPart(
  171. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  172. override;
  173. __declspec(dllexport) Text getPath() const override;
  174. __declspec(dllexport) bool isDifferent(
  175. const ValidationResult* zResult) const override;
  176. __declspec(dllexport) void addBasePath(Text basePath) override;
  177. };
  178. template<typename T> class StringValidationBuilder;
  179. template<typename T> class NumberValidationBuilder;
  180. template<typename T> class BoolValidationBuilder;
  181. template<typename T> class ObjectValidationBuilder;
  182. template<typename T> class ArrayValidationBuilder;
  183. template<typename T> class OneOfValidationBuilder;
  184. class DataValidator : public Framework::ReferenceCounter
  185. {
  186. private:
  187. XML::Element* constraints;
  188. RCTrie<XML::Element>* typeConstraints;
  189. public:
  190. __declspec(dllexport) DataValidator(XML::Element* constraints);
  191. __declspec(dllexport) DataValidator(XML::Element* constraints,
  192. RCTrie<XML::Element>* typeConstraints);
  193. __declspec(dllexport) ~DataValidator();
  194. __declspec(dllexport) ValidationResult* validate(
  195. AbstractElement* zValue) const;
  196. __declspec(dllexport) ValidationResult* validate(
  197. ElementPath* path, AbstractElement* zValue) const;
  198. __declspec(dllexport) bool isValid(AbstractElement* zValue) const;
  199. /**
  200. * returns the valid part of the json by performing the
  201. * following operations in the specified order untill no further
  202. * changes are made:
  203. * - invalid or unknown object properties are removed
  204. * - missing object properties with default values are added if
  205. * missing
  206. * - invalid array elements are removed
  207. *
  208. * @param zValue the json value to to extract the valid part
  209. * without increased reference counter
  210. * @return the valid part or 0 if no valid part exists
  211. */
  212. __declspec(dllexport) JSON::JSONValue* getValidParts(
  213. JSON::JSONValue* zValue,
  214. RCArray<ValidationResult>* zRemovedPartsValidationResults)
  215. const;
  216. __declspec(dllexport) XML::Element* zConstraints();
  217. private:
  218. __declspec(dllexport) ValidationResult* validate(
  219. ElementPath* pathToValidate,
  220. AbstractElement* zValue,
  221. XML::Element* zConstraints,
  222. Text path) const;
  223. __declspec(dllexport) ValidationResult* validateMultipleTypes(
  224. ElementPath* pathToValidate,
  225. AbstractElement* zChildValue,
  226. XML::Element* zPossibleChildConstraints,
  227. Text childPath) const;
  228. public:
  229. __declspec(dllexport) static StringValidationBuilder<
  230. DataValidator>* buildForString();
  231. __declspec(dllexport) static NumberValidationBuilder<
  232. DataValidator>* buildForNumber();
  233. __declspec(dllexport) static BoolValidationBuilder<
  234. DataValidator>* buildForBool();
  235. __declspec(dllexport) static ObjectValidationBuilder<
  236. DataValidator>* buildForObject();
  237. __declspec(dllexport) static ArrayValidationBuilder<
  238. DataValidator>* buildForArray();
  239. __declspec(dllexport) static OneOfValidationBuilder<
  240. DataValidator>* buildForOneOf();
  241. __declspec(dllexport) static DataValidator* buildForObjectReference(
  242. Text objectId);
  243. };
  244. template<typename T> class StringValidationBuilder
  245. {
  246. private:
  247. XML::Element element;
  248. std::function<T*(XML::Element& element)> builder;
  249. public:
  250. StringValidationBuilder(
  251. std::function<T*(XML::Element& element)> builder)
  252. : element("<string/>"),
  253. builder(builder)
  254. {}
  255. StringValidationBuilder<T>* withExactMatch(Text value)
  256. {
  257. element.setAttribute("equals", value);
  258. return this;
  259. }
  260. StringValidationBuilder<T>* whichContainsMatch(Text value)
  261. {
  262. element.setAttribute("contains", value);
  263. return this;
  264. }
  265. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  266. {
  267. element.setAttribute("startsWith", value);
  268. return this;
  269. }
  270. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  271. {
  272. element.setAttribute("endsWith", value);
  273. return this;
  274. }
  275. StringValidationBuilder<T>* whichIsOneOf(RCArray<Text> values)
  276. {
  277. JSON::JSONArray arr;
  278. for (Text* str : values)
  279. arr.addValue(new JSON::JSONString(str->getText()));
  280. element.setAttribute("oneOf", arr.toString());
  281. return this;
  282. }
  283. StringValidationBuilder<T>* whichIsOneOf(
  284. std::initializer_list<Text> values)
  285. {
  286. JSON::JSONArray arr;
  287. for (Text str : values)
  288. arr.addValue(new JSON::JSONString(str));
  289. element.setAttribute("oneOf", arr.toString());
  290. return this;
  291. }
  292. StringValidationBuilder<T>* whichIs(Text value)
  293. {
  294. JSON::JSONArray arr;
  295. arr.addValue(new JSON::JSONString(value));
  296. element.setAttribute("oneOf", arr.toString());
  297. return this;
  298. }
  299. StringValidationBuilder<T>* withDefault(Text value)
  300. {
  301. element.setAttribute(
  302. "default", JSON::JSONString(value).toString());
  303. return this;
  304. }
  305. StringValidationBuilder<T>* withDefaultNull()
  306. {
  307. element.setAttribute("default", "null");
  308. return this;
  309. }
  310. StringValidationBuilder<T>* whichCanBeNull()
  311. {
  312. element.setAttribute("nullable", "true");
  313. return this;
  314. }
  315. StringValidationBuilder<T>* whichIsOptional()
  316. {
  317. element.setAttribute("optional", "true");
  318. return this;
  319. }
  320. T* finishString()
  321. {
  322. T* result = builder(element);
  323. delete this;
  324. return result;
  325. }
  326. };
  327. template<typename T> class NumberValidationBuilder
  328. {
  329. private:
  330. XML::Element element;
  331. std::function<T*(XML::Element& element)> builder;
  332. public:
  333. NumberValidationBuilder(
  334. std::function<T*(XML::Element& element)> builder)
  335. : element("<number/>"),
  336. builder(builder)
  337. {}
  338. NumberValidationBuilder<T>* whichIs(double value)
  339. {
  340. element.setAttribute("equals", Text(value));
  341. return this;
  342. }
  343. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  344. {
  345. element.setAttribute("lessOrEqual", Text(value));
  346. return this;
  347. }
  348. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  349. {
  350. element.setAttribute("greaterOrEqual", Text(value));
  351. return this;
  352. }
  353. NumberValidationBuilder<T>* whichIsLessThen(double value)
  354. {
  355. element.setAttribute("less", Text(value));
  356. return this;
  357. }
  358. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  359. {
  360. element.setAttribute("greater", Text(value));
  361. return this;
  362. }
  363. NumberValidationBuilder<T>* withDefault(double value)
  364. {
  365. element.setAttribute(
  366. "default", JSON::JSONNumber(value).toString());
  367. return this;
  368. }
  369. NumberValidationBuilder<T>* withDefaultNull()
  370. {
  371. element.setAttribute("default", "null");
  372. return this;
  373. }
  374. NumberValidationBuilder<T>* whichCanBeNull()
  375. {
  376. element.setAttribute("nullable", "true");
  377. return this;
  378. }
  379. NumberValidationBuilder<T>* whichIsOptional()
  380. {
  381. element.setAttribute("optional", "true");
  382. return this;
  383. }
  384. T* finishNumber()
  385. {
  386. T* result = builder(element);
  387. delete this;
  388. return result;
  389. }
  390. };
  391. template<typename T> class BoolValidationBuilder
  392. {
  393. private:
  394. XML::Element element;
  395. std::function<T*(XML::Element& element)> builder;
  396. public:
  397. BoolValidationBuilder(
  398. std::function<T*(XML::Element& element)> builder)
  399. : element("<bool/>"),
  400. builder(builder)
  401. {}
  402. BoolValidationBuilder<T>* whichIs(bool value)
  403. {
  404. element.setAttribute("equals", value ? "true" : "false");
  405. return this;
  406. }
  407. BoolValidationBuilder<T>* withDefault(bool value)
  408. {
  409. element.setAttribute(
  410. "default", JSON::JSONBool(value).toString());
  411. return this;
  412. }
  413. BoolValidationBuilder<T>* withDefaultNull()
  414. {
  415. element.setAttribute("default", "null");
  416. return this;
  417. }
  418. BoolValidationBuilder<T>* whichCanBeNull()
  419. {
  420. element.setAttribute("nullable", "true");
  421. return this;
  422. }
  423. BoolValidationBuilder<T>* whichIsOptional()
  424. {
  425. element.setAttribute("optional", "true");
  426. return this;
  427. }
  428. T* finishBool()
  429. {
  430. T* result = builder(element);
  431. delete this;
  432. return result;
  433. }
  434. };
  435. template<typename T> class ArrayValidationBuilder;
  436. template<typename T> class ObjectValidationBuilder
  437. {
  438. private:
  439. XML::Element element;
  440. std::function<T*(XML::Element& element)> builder;
  441. public:
  442. ObjectValidationBuilder(
  443. std::function<T*(XML::Element& element)> builder)
  444. : element("<object></object>"),
  445. builder(builder)
  446. {}
  447. ObjectValidationBuilder<T>* setObjectReferenceId(Text id)
  448. {
  449. element.setAttribute("id", id);
  450. return this;
  451. }
  452. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  453. withRequiredNumber(Text name)
  454. {
  455. return new NumberValidationBuilder<ObjectValidationBuilder<T>>(
  456. [this, name](XML::Element& e) {
  457. if (!element.selectChildsByAttribute("name", name)
  458. .exists())
  459. {
  460. XML::Element* attr
  461. = new XML::Element("<value></value>");
  462. attr->setAttribute("name", name);
  463. element.addChild(attr);
  464. }
  465. element.selectChildsByAttribute("name", name)
  466. .addChild(e.dublicate());
  467. return this;
  468. });
  469. }
  470. StringValidationBuilder<ObjectValidationBuilder<T>>*
  471. withRequiredString(Text name)
  472. {
  473. return new StringValidationBuilder<ObjectValidationBuilder<T>>(
  474. [this, name](XML::Element& e) {
  475. if (!element.selectChildsByAttribute("name", name)
  476. .exists())
  477. {
  478. XML::Element* attr
  479. = new XML::Element("<value></value>");
  480. attr->setAttribute("name", name);
  481. element.addChild(attr);
  482. }
  483. element.selectChildsByAttribute("name", name)
  484. .addChild(e.dublicate());
  485. return this;
  486. });
  487. }
  488. BoolValidationBuilder<ObjectValidationBuilder<T>>* withRequiredBool(
  489. Text name)
  490. {
  491. return new BoolValidationBuilder<ObjectValidationBuilder<T>>(
  492. [this, name](XML::Element& e) {
  493. if (!element.selectChildsByAttribute("name", name)
  494. .exists())
  495. {
  496. XML::Element* attr
  497. = new XML::Element("<value></value>");
  498. attr->setAttribute("name", name);
  499. element.addChild(attr);
  500. }
  501. element.selectChildsByAttribute("name", name)
  502. .addChild(e.dublicate());
  503. return this;
  504. });
  505. }
  506. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  507. withRequiredArray(Text name)
  508. {
  509. return new ArrayValidationBuilder<ObjectValidationBuilder<T>>(
  510. [this, name](XML::Element& e) {
  511. if (!element.selectChildsByAttribute("name", name)
  512. .exists())
  513. {
  514. XML::Element* attr
  515. = new XML::Element("<value></value>");
  516. attr->setAttribute("name", name);
  517. element.addChild(attr);
  518. }
  519. element.selectChildsByAttribute("name", name)
  520. .addChild(e.dublicate());
  521. return this;
  522. });
  523. }
  524. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  525. withRequiredObject(Text name)
  526. {
  527. return new ObjectValidationBuilder<ObjectValidationBuilder<T>>(
  528. [this, name](XML::Element& e) {
  529. if (!element.selectChildsByAttribute("name", name)
  530. .exists())
  531. {
  532. XML::Element* attr
  533. = new XML::Element("<value></value>");
  534. attr->setAttribute("name", name);
  535. element.addChild(attr);
  536. }
  537. element.selectChildsByAttribute("name", name)
  538. .addChild(e.dublicate());
  539. return this;
  540. });
  541. }
  542. ObjectValidationBuilder<T>* withRequiredAttribute(
  543. Text name, DataValidator* validator)
  544. {
  545. if (!element.selectChildsByAttribute("name", name).exists())
  546. {
  547. XML::Element* attr = new XML::Element("<value></value>");
  548. attr->setAttribute("name", name);
  549. element.addChild(attr);
  550. }
  551. element.selectChildsByAttribute("name", name)
  552. .addChild(validator->zConstraints()->dublicate());
  553. validator->release();
  554. return this;
  555. }
  556. ObjectValidationBuilder<T>* withDefault(JSON::JSONObject* obj)
  557. {
  558. element.setAttribute("default", obj->toString());
  559. obj->release();
  560. return this;
  561. }
  562. ObjectValidationBuilder<T>* withDefaultNull()
  563. {
  564. element.setAttribute("default", "null");
  565. return this;
  566. }
  567. ObjectValidationBuilder<T>* whichCanBeNull()
  568. {
  569. element.setAttribute("nullable", "true");
  570. return this;
  571. }
  572. ObjectValidationBuilder<T>* whichIsOptional()
  573. {
  574. element.setAttribute("optional", "true");
  575. return this;
  576. }
  577. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  578. Text valueName, Text attributeName)
  579. {
  580. if (!element.selectChildsByAttribute("name", valueName)
  581. .exists())
  582. {
  583. XML::Element* attr = new XML::Element("<value></value>");
  584. attr->setAttribute("name", valueName);
  585. element.addChild(attr);
  586. }
  587. element.selectChildsByAttribute("name", valueName)
  588. .setAttribute("typeSpecifiedBy", attributeName);
  589. return this;
  590. }
  591. ObjectValidationBuilder<T>* removeInvalidEntries()
  592. {
  593. element.setAttribute("removeInvalidEntries", "true");
  594. return this;
  595. }
  596. ObjectValidationBuilder<T>* allowAdditionalAttriutes()
  597. {
  598. element.setAttribute("allowAdditionalAttributes", "true");
  599. return this;
  600. }
  601. T* finishObject()
  602. {
  603. T* result = builder(element);
  604. delete this;
  605. return result;
  606. }
  607. };
  608. template<typename T> class ArrayValidationBuilder
  609. {
  610. private:
  611. XML::Element element;
  612. std::function<T*(XML::Element& element)> builder;
  613. public:
  614. ArrayValidationBuilder(
  615. std::function<T*(XML::Element& element)> builder)
  616. : element("<array></array>"),
  617. builder(builder)
  618. {}
  619. StringValidationBuilder<ArrayValidationBuilder<T>>*
  620. addAcceptedStringInArray()
  621. {
  622. return new StringValidationBuilder<ArrayValidationBuilder<T>>(
  623. [this](XML::Element& e) {
  624. element.addChild(e.dublicate());
  625. return this;
  626. });
  627. }
  628. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  629. addAcceptedNumberInArray()
  630. {
  631. return new NumberValidationBuilder<ArrayValidationBuilder<T>>(
  632. [this](XML::Element& e) {
  633. element.addChild(e.dublicate());
  634. return this;
  635. });
  636. }
  637. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  638. addAcceptedBooleanInArray()
  639. {
  640. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  641. [this](XML::Element& e) {
  642. element.addChild(e.dublicate());
  643. return this;
  644. });
  645. }
  646. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  647. addAcceptedObjectInArray()
  648. {
  649. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>(
  650. [this](XML::Element& e) {
  651. element.addChild(e.dublicate());
  652. return this;
  653. });
  654. }
  655. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  656. addAcceptedArrayInArray()
  657. {
  658. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>(
  659. [this](XML::Element& e) {
  660. element.addChild(e.dublicate());
  661. return this;
  662. });
  663. }
  664. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  665. DataValidator* validator)
  666. {
  667. element.addChild(validator->zConstraints()->dublicate());
  668. validator->release();
  669. return this;
  670. }
  671. ArrayValidationBuilder<T>* acceptNullsInArray()
  672. {
  673. element.setAttribute("nullsEnabled", "true");
  674. return this;
  675. }
  676. ArrayValidationBuilder<T>* withDefault(JSON::JSONArray* array)
  677. {
  678. element.setAttribute("default", array->toString());
  679. array->release();
  680. return this;
  681. }
  682. ArrayValidationBuilder<T>* withDefaultNull()
  683. {
  684. element.setAttribute("default", "null");
  685. return this;
  686. }
  687. ArrayValidationBuilder<T>* whichCanBeNull()
  688. {
  689. element.setAttribute("nullable", "true");
  690. return this;
  691. }
  692. ArrayValidationBuilder<T>* whichIsOptional()
  693. {
  694. element.setAttribute("optional", "true");
  695. return this;
  696. }
  697. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  698. {
  699. element.setAttribute("typeSpecifiedBy", name);
  700. return this;
  701. }
  702. ArrayValidationBuilder<T>* removeInvalidEntries()
  703. {
  704. element.setAttribute("removeInvalidEntries", "true");
  705. return this;
  706. }
  707. T* finishArray()
  708. {
  709. T* result = builder(element);
  710. delete this;
  711. return result;
  712. }
  713. };
  714. template<typename T> class OneOfValidationBuilder
  715. {
  716. private:
  717. XML::Element element;
  718. std::function<T*(XML::Element& element)> builder;
  719. public:
  720. OneOfValidationBuilder(
  721. std::function<T*(XML::Element& element)> builder)
  722. : element("<oneOf></oneOf>"),
  723. builder(builder)
  724. {}
  725. StringValidationBuilder<OneOfValidationBuilder<T>>*
  726. addAcceptedStringInArray()
  727. {
  728. return new StringValidationBuilder<OneOfValidationBuilder<T>>(
  729. [this](XML::Element& e) {
  730. element.addChild(e.dublicate());
  731. return this;
  732. });
  733. }
  734. NumberValidationBuilder<OneOfValidationBuilder<T>>*
  735. addAcceptedNumberInArray()
  736. {
  737. return new NumberValidationBuilder<OneOfValidationBuilder<T>>(
  738. [this](XML::Element& e) {
  739. element.addChild(e.dublicate());
  740. return this;
  741. });
  742. }
  743. BoolValidationBuilder<OneOfValidationBuilder<T>>*
  744. addAcceptedBooleanInArray()
  745. {
  746. return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
  747. [this](XML::Element& e) {
  748. element.addChild(e.dublicate());
  749. return this;
  750. });
  751. }
  752. ObjectValidationBuilder<OneOfValidationBuilder<T>>*
  753. addAcceptedObjectInArray()
  754. {
  755. return new ObjectValidationBuilder<OneOfValidationBuilder<T>>(
  756. [this](XML::Element& e) {
  757. element.addChild(e.dublicate());
  758. return this;
  759. });
  760. }
  761. ArrayValidationBuilder<OneOfValidationBuilder<T>>*
  762. addAcceptedArrayInArray()
  763. {
  764. return new ArrayValidationBuilder<OneOfValidationBuilder<T>>(
  765. [this](XML::Element& e) {
  766. element.addChild(e.dublicate());
  767. return this;
  768. });
  769. }
  770. OneOfValidationBuilder<T>* addAcceptedType(DataValidator* validator)
  771. {
  772. element.addChild(validator->zConstraints()->dublicate());
  773. validator->release();
  774. return this;
  775. }
  776. OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  777. {
  778. element.setAttribute("typeSpecifiedBy", name);
  779. return this;
  780. }
  781. T* finishOneOf()
  782. {
  783. T* result = builder(element);
  784. delete this;
  785. return result;
  786. }
  787. };
  788. } // namespace Validator
  789. } // namespace Framework