DataValidator.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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>* withDefault(JSON::JSONObject* obj)
  587. {
  588. element.setAttribute("default", obj->toString());
  589. obj->release();
  590. return this;
  591. }
  592. ObjectValidationBuilder<T>* withDefaultNull()
  593. {
  594. element.setAttribute("default", "null");
  595. return this;
  596. }
  597. ObjectValidationBuilder<T>* whichCanBeNull()
  598. {
  599. element.setAttribute("nullable", "true");
  600. return this;
  601. }
  602. ObjectValidationBuilder<T>* whichIsOptional()
  603. {
  604. element.setAttribute("optional", "true");
  605. return this;
  606. }
  607. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  608. Text valueName, Text attributeName)
  609. {
  610. if (!element.selectChildsByAttribute("name", valueName)
  611. .exists())
  612. {
  613. XML::Element* attr = new XML::Element("<value></value>");
  614. attr->setAttribute("name", valueName);
  615. element.addChild(attr);
  616. }
  617. element.selectChildsByAttribute("name", valueName)
  618. .setAttribute("typeSpecifiedBy", attributeName);
  619. return this;
  620. }
  621. ObjectValidationBuilder<T>* removeInvalidEntries()
  622. {
  623. element.setAttribute("removeInvalidEntries", "true");
  624. return this;
  625. }
  626. ObjectValidationBuilder<T>* allowAdditionalAttriutes()
  627. {
  628. element.setAttribute("allowAdditionalAttributes", "true");
  629. return this;
  630. }
  631. T* finishObject()
  632. {
  633. T* result = builder(element);
  634. delete this;
  635. return result;
  636. }
  637. };
  638. template<typename T> class ArrayValidationBuilder
  639. {
  640. private:
  641. XML::Element element;
  642. std::function<T*(XML::Element& element)> builder;
  643. public:
  644. ArrayValidationBuilder(
  645. std::function<T*(XML::Element& element)> builder)
  646. : element("<array></array>"),
  647. builder(builder)
  648. {}
  649. StringValidationBuilder<ArrayValidationBuilder<T>>*
  650. addAcceptedStringInArray()
  651. {
  652. return new StringValidationBuilder<ArrayValidationBuilder<T>>(
  653. [this](XML::Element& e) {
  654. element.addChild(e.dublicate());
  655. return this;
  656. });
  657. }
  658. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  659. addAcceptedNumberInArray()
  660. {
  661. return new NumberValidationBuilder<ArrayValidationBuilder<T>>(
  662. [this](XML::Element& e) {
  663. element.addChild(e.dublicate());
  664. return this;
  665. });
  666. }
  667. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  668. addAcceptedBooleanInArray()
  669. {
  670. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  671. [this](XML::Element& e) {
  672. element.addChild(e.dublicate());
  673. return this;
  674. });
  675. }
  676. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  677. addAcceptedObjectInArray()
  678. {
  679. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>(
  680. [this](XML::Element& e) {
  681. element.addChild(e.dublicate());
  682. return this;
  683. });
  684. }
  685. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  686. addAcceptedArrayInArray()
  687. {
  688. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>(
  689. [this](XML::Element& e) {
  690. element.addChild(e.dublicate());
  691. return this;
  692. });
  693. }
  694. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  695. DataValidator* validator)
  696. {
  697. element.addChild(validator->zConstraints()->dublicate());
  698. validator->release();
  699. return this;
  700. }
  701. ArrayValidationBuilder<T>* acceptNullsInArray()
  702. {
  703. element.setAttribute("nullsEnabled", "true");
  704. return this;
  705. }
  706. ArrayValidationBuilder<T>* withDefault(JSON::JSONArray* array)
  707. {
  708. element.setAttribute("default", array->toString());
  709. array->release();
  710. return this;
  711. }
  712. ArrayValidationBuilder<T>* withDefaultNull()
  713. {
  714. element.setAttribute("default", "null");
  715. return this;
  716. }
  717. ArrayValidationBuilder<T>* whichCanBeNull()
  718. {
  719. element.setAttribute("nullable", "true");
  720. return this;
  721. }
  722. ArrayValidationBuilder<T>* whichIsOptional()
  723. {
  724. element.setAttribute("optional", "true");
  725. return this;
  726. }
  727. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  728. {
  729. element.setAttribute("typeSpecifiedBy", name);
  730. return this;
  731. }
  732. ArrayValidationBuilder<T>* removeInvalidEntries()
  733. {
  734. element.setAttribute("removeInvalidEntries", "true");
  735. return this;
  736. }
  737. T* finishArray()
  738. {
  739. T* result = builder(element);
  740. delete this;
  741. return result;
  742. }
  743. };
  744. template<typename T> class OneOfValidationBuilder
  745. {
  746. private:
  747. XML::Element element;
  748. std::function<T*(XML::Element& element)> builder;
  749. public:
  750. OneOfValidationBuilder(
  751. std::function<T*(XML::Element& element)> builder)
  752. : element("<oneOf></oneOf>"),
  753. builder(builder)
  754. {}
  755. StringValidationBuilder<OneOfValidationBuilder<T>>*
  756. addAcceptedStringInArray()
  757. {
  758. return new StringValidationBuilder<OneOfValidationBuilder<T>>(
  759. [this](XML::Element& e) {
  760. element.addChild(e.dublicate());
  761. return this;
  762. });
  763. }
  764. NumberValidationBuilder<OneOfValidationBuilder<T>>*
  765. addAcceptedNumberInArray()
  766. {
  767. return new NumberValidationBuilder<OneOfValidationBuilder<T>>(
  768. [this](XML::Element& e) {
  769. element.addChild(e.dublicate());
  770. return this;
  771. });
  772. }
  773. BoolValidationBuilder<OneOfValidationBuilder<T>>*
  774. addAcceptedBooleanInArray()
  775. {
  776. return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
  777. [this](XML::Element& e) {
  778. element.addChild(e.dublicate());
  779. return this;
  780. });
  781. }
  782. ObjectValidationBuilder<OneOfValidationBuilder<T>>*
  783. addAcceptedObjectInArray()
  784. {
  785. return new ObjectValidationBuilder<OneOfValidationBuilder<T>>(
  786. [this](XML::Element& e) {
  787. element.addChild(e.dublicate());
  788. return this;
  789. });
  790. }
  791. ArrayValidationBuilder<OneOfValidationBuilder<T>>*
  792. addAcceptedArrayInArray()
  793. {
  794. return new ArrayValidationBuilder<OneOfValidationBuilder<T>>(
  795. [this](XML::Element& e) {
  796. element.addChild(e.dublicate());
  797. return this;
  798. });
  799. }
  800. OneOfValidationBuilder<T>* addAcceptedType(DataValidator* validator)
  801. {
  802. element.addChild(validator->zConstraints()->dublicate());
  803. validator->release();
  804. return this;
  805. }
  806. OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  807. {
  808. element.setAttribute("typeSpecifiedBy", name);
  809. return this;
  810. }
  811. T* finishOneOf()
  812. {
  813. T* result = builder(element);
  814. delete this;
  815. return result;
  816. }
  817. };
  818. } // namespace Validator
  819. } // namespace Framework