DataValidator.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. StringValidationBuilder<ObjectValidationBuilder<T>>*
  501. withRequiredString(Text name)
  502. {
  503. return new StringValidationBuilder<ObjectValidationBuilder<T>>(
  504. [this, name](XML::Element& e) {
  505. if (!element.selectChildsByAttribute("name", name)
  506. .exists())
  507. {
  508. XML::Element* attr
  509. = new XML::Element("<value></value>");
  510. attr->setAttribute("name", name);
  511. element.addChild(attr);
  512. }
  513. element.selectChildsByAttribute("name", name)
  514. .addChild(e.dublicate());
  515. return this;
  516. });
  517. }
  518. BoolValidationBuilder<ObjectValidationBuilder<T>>* withRequiredBool(
  519. Text name)
  520. {
  521. return new BoolValidationBuilder<ObjectValidationBuilder<T>>(
  522. [this, name](XML::Element& e) {
  523. if (!element.selectChildsByAttribute("name", name)
  524. .exists())
  525. {
  526. XML::Element* attr
  527. = new XML::Element("<value></value>");
  528. attr->setAttribute("name", name);
  529. element.addChild(attr);
  530. }
  531. element.selectChildsByAttribute("name", name)
  532. .addChild(e.dublicate());
  533. return this;
  534. });
  535. }
  536. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  537. withRequiredArray(Text name)
  538. {
  539. return new ArrayValidationBuilder<ObjectValidationBuilder<T>>(
  540. [this, name](XML::Element& e) {
  541. if (!element.selectChildsByAttribute("name", name)
  542. .exists())
  543. {
  544. XML::Element* attr
  545. = new XML::Element("<value></value>");
  546. attr->setAttribute("name", name);
  547. element.addChild(attr);
  548. }
  549. element.selectChildsByAttribute("name", name)
  550. .addChild(e.dublicate());
  551. return this;
  552. });
  553. }
  554. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  555. withRequiredObject(Text name)
  556. {
  557. return new ObjectValidationBuilder<ObjectValidationBuilder<T>>(
  558. [this, name](XML::Element& e) {
  559. if (!element.selectChildsByAttribute("name", name)
  560. .exists())
  561. {
  562. XML::Element* attr
  563. = new XML::Element("<value></value>");
  564. attr->setAttribute("name", name);
  565. element.addChild(attr);
  566. }
  567. element.selectChildsByAttribute("name", name)
  568. .addChild(e.dublicate());
  569. return this;
  570. });
  571. }
  572. ObjectValidationBuilder<T>* withRequiredAttribute(
  573. Text name, DataValidator* validator)
  574. {
  575. if (!element.selectChildsByAttribute("name", name).exists())
  576. {
  577. XML::Element* attr = new XML::Element("<value></value>");
  578. attr->setAttribute("name", name);
  579. element.addChild(attr);
  580. }
  581. element.selectChildsByAttribute("name", name)
  582. .addChild(validator->zConstraints()->dublicate());
  583. validator->release();
  584. return this;
  585. }
  586. ObjectValidationBuilder<T>* withRequiredAttribute(Text name,
  587. DataValidator* validator,
  588. bool canBeNull,
  589. bool optional)
  590. {
  591. if (!element.selectChildsByAttribute("name", name).exists())
  592. {
  593. XML::Element* attr = new XML::Element("<value></value>");
  594. attr->setAttribute("name", name);
  595. element.addChild(attr);
  596. }
  597. XML::Element* element = validator->zConstraints()->dublicate();
  598. element->setAttribute("nullable", canBeNull ? "true" : "false");
  599. element->setAttribute("optional", optional ? "true" : "false");
  600. this->element.selectChildsByAttribute("name", name)
  601. .addChild(element);
  602. validator->release();
  603. return this;
  604. }
  605. ObjectValidationBuilder<T>* withDefault(JSON::JSONObject* obj)
  606. {
  607. element.setAttribute("default", obj->toString());
  608. obj->release();
  609. return this;
  610. }
  611. ObjectValidationBuilder<T>* withDefaultNull()
  612. {
  613. element.setAttribute("default", "null");
  614. return this;
  615. }
  616. ObjectValidationBuilder<T>* whichCanBeNull()
  617. {
  618. element.setAttribute("nullable", "true");
  619. return this;
  620. }
  621. ObjectValidationBuilder<T>* whichIsOptional()
  622. {
  623. element.setAttribute("optional", "true");
  624. return this;
  625. }
  626. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  627. Text valueName, Text attributeName)
  628. {
  629. if (!element.selectChildsByAttribute("name", valueName)
  630. .exists())
  631. {
  632. XML::Element* attr = new XML::Element("<value></value>");
  633. attr->setAttribute("name", valueName);
  634. element.addChild(attr);
  635. }
  636. element.selectChildsByAttribute("name", valueName)
  637. .setAttribute("typeSpecifiedBy", attributeName);
  638. return this;
  639. }
  640. ObjectValidationBuilder<T>* removeInvalidEntries()
  641. {
  642. element.setAttribute("removeInvalidEntries", "true");
  643. return this;
  644. }
  645. ObjectValidationBuilder<T>* allowAdditionalAttriutes()
  646. {
  647. element.setAttribute("allowAdditionalAttributes", "true");
  648. return this;
  649. }
  650. T* finishObject()
  651. {
  652. T* result = builder(element);
  653. delete this;
  654. return result;
  655. }
  656. };
  657. template<typename T> class ArrayValidationBuilder
  658. {
  659. private:
  660. XML::Element element;
  661. std::function<T*(XML::Element& element)> builder;
  662. public:
  663. ArrayValidationBuilder(
  664. std::function<T*(XML::Element& element)> builder)
  665. : element("<array></array>"),
  666. builder(builder)
  667. {}
  668. StringValidationBuilder<ArrayValidationBuilder<T>>*
  669. addAcceptedStringInArray()
  670. {
  671. return new StringValidationBuilder<ArrayValidationBuilder<T>>(
  672. [this](XML::Element& e) {
  673. element.addChild(e.dublicate());
  674. return this;
  675. });
  676. }
  677. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  678. addAcceptedNumberInArray()
  679. {
  680. return new NumberValidationBuilder<ArrayValidationBuilder<T>>(
  681. [this](XML::Element& e) {
  682. element.addChild(e.dublicate());
  683. return this;
  684. });
  685. }
  686. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  687. addAcceptedBooleanInArray()
  688. {
  689. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  690. [this](XML::Element& e) {
  691. element.addChild(e.dublicate());
  692. return this;
  693. });
  694. }
  695. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  696. addAcceptedObjectInArray()
  697. {
  698. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>(
  699. [this](XML::Element& e) {
  700. element.addChild(e.dublicate());
  701. return this;
  702. });
  703. }
  704. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  705. addAcceptedArrayInArray()
  706. {
  707. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>(
  708. [this](XML::Element& e) {
  709. element.addChild(e.dublicate());
  710. return this;
  711. });
  712. }
  713. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  714. DataValidator* validator)
  715. {
  716. element.addChild(validator->zConstraints()->dublicate());
  717. validator->release();
  718. return this;
  719. }
  720. ArrayValidationBuilder<T>* acceptNullsInArray()
  721. {
  722. element.setAttribute("nullsEnabled", "true");
  723. return this;
  724. }
  725. ArrayValidationBuilder<T>* withDefault(JSON::JSONArray* array)
  726. {
  727. element.setAttribute("default", array->toString());
  728. array->release();
  729. return this;
  730. }
  731. ArrayValidationBuilder<T>* withDefaultNull()
  732. {
  733. element.setAttribute("default", "null");
  734. return this;
  735. }
  736. ArrayValidationBuilder<T>* whichCanBeNull()
  737. {
  738. element.setAttribute("nullable", "true");
  739. return this;
  740. }
  741. ArrayValidationBuilder<T>* whichIsOptional()
  742. {
  743. element.setAttribute("optional", "true");
  744. return this;
  745. }
  746. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  747. {
  748. element.setAttribute("typeSpecifiedBy", name);
  749. return this;
  750. }
  751. ArrayValidationBuilder<T>* removeInvalidEntries()
  752. {
  753. element.setAttribute("removeInvalidEntries", "true");
  754. return this;
  755. }
  756. T* finishArray()
  757. {
  758. T* result = builder(element);
  759. delete this;
  760. return result;
  761. }
  762. };
  763. template<typename T> class OneOfValidationBuilder
  764. {
  765. private:
  766. XML::Element element;
  767. std::function<T*(XML::Element& element)> builder;
  768. public:
  769. OneOfValidationBuilder(
  770. std::function<T*(XML::Element& element)> builder)
  771. : element("<oneOf></oneOf>"),
  772. builder(builder)
  773. {}
  774. StringValidationBuilder<OneOfValidationBuilder<T>>*
  775. addAcceptedStringInArray()
  776. {
  777. return new StringValidationBuilder<OneOfValidationBuilder<T>>(
  778. [this](XML::Element& e) {
  779. element.addChild(e.dublicate());
  780. return this;
  781. });
  782. }
  783. NumberValidationBuilder<OneOfValidationBuilder<T>>*
  784. addAcceptedNumberInArray()
  785. {
  786. return new NumberValidationBuilder<OneOfValidationBuilder<T>>(
  787. [this](XML::Element& e) {
  788. element.addChild(e.dublicate());
  789. return this;
  790. });
  791. }
  792. BoolValidationBuilder<OneOfValidationBuilder<T>>*
  793. addAcceptedBooleanInArray()
  794. {
  795. return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
  796. [this](XML::Element& e) {
  797. element.addChild(e.dublicate());
  798. return this;
  799. });
  800. }
  801. ObjectValidationBuilder<OneOfValidationBuilder<T>>*
  802. addAcceptedObjectInArray()
  803. {
  804. return new ObjectValidationBuilder<OneOfValidationBuilder<T>>(
  805. [this](XML::Element& e) {
  806. element.addChild(e.dublicate());
  807. return this;
  808. });
  809. }
  810. ArrayValidationBuilder<OneOfValidationBuilder<T>>*
  811. addAcceptedArrayInArray()
  812. {
  813. return new ArrayValidationBuilder<OneOfValidationBuilder<T>>(
  814. [this](XML::Element& e) {
  815. element.addChild(e.dublicate());
  816. return this;
  817. });
  818. }
  819. OneOfValidationBuilder<T>* addAcceptedType(DataValidator* validator)
  820. {
  821. element.addChild(validator->zConstraints()->dublicate());
  822. validator->release();
  823. return this;
  824. }
  825. OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  826. {
  827. element.setAttribute("typeSpecifiedBy", name);
  828. return this;
  829. }
  830. T* finishOneOf()
  831. {
  832. T* result = builder(element);
  833. delete this;
  834. return result;
  835. }
  836. };
  837. } // namespace Validator
  838. } // namespace Framework