JsonDetail.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * JsonDetail.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonDetail.h"
  12. #include "CGeneralTextHandler.h"
  13. #include "filesystem/Filesystem.h"
  14. #include "ScopeGuard.h"
  15. static const JsonNode nullNode;
  16. template<typename Iterator>
  17. void JsonWriter::writeContainer(Iterator begin, Iterator end)
  18. {
  19. if (begin == end)
  20. return;
  21. prefix += '\t';
  22. writeEntry(begin++);
  23. while (begin != end)
  24. {
  25. out<<",\n";
  26. writeEntry(begin++);
  27. }
  28. out<<"\n";
  29. prefix.resize(prefix.size()-1);
  30. }
  31. void JsonWriter::writeEntry(JsonMap::const_iterator entry)
  32. {
  33. out << prefix;
  34. writeString(entry->first);
  35. out << " : ";
  36. writeNode(entry->second);
  37. }
  38. void JsonWriter::writeEntry(JsonVector::const_iterator entry)
  39. {
  40. out << prefix;
  41. writeNode(*entry);
  42. }
  43. void JsonWriter::writeString(const std::string &string)
  44. {
  45. static const std::string escaped = "\"\\\b\f\n\r\t";
  46. out <<'\"';
  47. size_t pos=0, start=0;
  48. for (; pos<string.size(); pos++)
  49. {
  50. size_t escapedChar = escaped.find(string[pos]);
  51. if (escapedChar != std::string::npos)
  52. {
  53. out.write(string.data()+start, pos - start);
  54. out << '\\' << escaped[escapedChar];
  55. start = pos;
  56. }
  57. }
  58. out.write(string.data()+start, pos - start);
  59. out <<'\"';
  60. }
  61. void JsonWriter::writeNode(const JsonNode &node)
  62. {
  63. switch(node.getType())
  64. {
  65. break; case JsonNode::DATA_NULL:
  66. out << "null";
  67. break; case JsonNode::DATA_BOOL:
  68. if (node.Bool())
  69. out << "true";
  70. else
  71. out << "false";
  72. break; case JsonNode::DATA_FLOAT:
  73. out << node.Float();
  74. break; case JsonNode::DATA_STRING:
  75. writeString(node.String());
  76. break; case JsonNode::DATA_VECTOR:
  77. out << "[" << "\n";
  78. writeContainer(node.Vector().begin(), node.Vector().end());
  79. out << prefix << "]";
  80. break; case JsonNode::DATA_STRUCT:
  81. out << "{" << "\n";
  82. writeContainer(node.Struct().begin(), node.Struct().end());
  83. out << prefix << "}";
  84. }
  85. if (!node.meta.empty()) // write metainf as comment
  86. out << " //" << node.meta;
  87. }
  88. JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
  89. out(output)
  90. {
  91. writeNode(node);
  92. }
  93. std::ostream & operator<<(std::ostream &out, const JsonNode &node)
  94. {
  95. JsonWriter(out, node);
  96. return out << "\n";
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. JsonParser::JsonParser(const char * inputString, size_t stringSize):
  100. input(inputString, stringSize),
  101. lineCount(1),
  102. lineStart(0),
  103. pos(0)
  104. {
  105. }
  106. JsonNode JsonParser::parse(std::string fileName)
  107. {
  108. JsonNode root;
  109. if (!Unicode::isValidString(&input[0], input.size()))
  110. error("Not a valid UTF-8 file", false);
  111. extractValue(root);
  112. extractWhitespace(false);
  113. //Warn if there are any non-whitespace symbols left
  114. if (pos < input.size())
  115. error("Not all file was parsed!", true);
  116. if (!errors.empty())
  117. {
  118. logGlobal->warnStream()<<"File " << fileName << " is not a valid JSON file!";
  119. logGlobal->warnStream()<<errors;
  120. }
  121. return root;
  122. }
  123. bool JsonParser::extractSeparator()
  124. {
  125. if (!extractWhitespace())
  126. return false;
  127. if ( input[pos] !=':')
  128. return error("Separator expected");
  129. pos++;
  130. return true;
  131. }
  132. bool JsonParser::extractValue(JsonNode &node)
  133. {
  134. if (!extractWhitespace())
  135. return false;
  136. switch (input[pos])
  137. {
  138. case '\"': return extractString(node);
  139. case 'n' : return extractNull(node);
  140. case 't' : return extractTrue(node);
  141. case 'f' : return extractFalse(node);
  142. case '{' : return extractStruct(node);
  143. case '[' : return extractArray(node);
  144. case '-' : return extractFloat(node);
  145. default:
  146. {
  147. if (input[pos] >= '0' && input[pos] <= '9')
  148. return extractFloat(node);
  149. return error("Value expected!");
  150. }
  151. }
  152. }
  153. bool JsonParser::extractWhitespace(bool verbose)
  154. {
  155. while (true)
  156. {
  157. while (pos < input.size() && (ui8)input[pos] <= ' ')
  158. {
  159. if (input[pos] == '\n')
  160. {
  161. lineCount++;
  162. lineStart = pos+1;
  163. }
  164. pos++;
  165. }
  166. if (pos >= input.size() || input[pos] != '/')
  167. break;
  168. pos++;
  169. if (pos == input.size())
  170. break;
  171. if (input[pos] == '/')
  172. pos++;
  173. else
  174. error("Comments must consist from two slashes!", true);
  175. while (pos < input.size() && input[pos] != '\n')
  176. pos++;
  177. }
  178. if (pos >= input.size() && verbose)
  179. return error("Unexpected end of file!");
  180. return true;
  181. }
  182. bool JsonParser::extractEscaping(std::string &str)
  183. {
  184. switch(input[pos])
  185. {
  186. break; case '\"': str += '\"';
  187. break; case '\\': str += '\\';
  188. break; case 'b': str += '\b';
  189. break; case 'f': str += '\f';
  190. break; case 'n': str += '\n';
  191. break; case 'r': str += '\r';
  192. break; case 't': str += '\t';
  193. break; default: return error("Unknown escape sequence!", true);
  194. };
  195. return true;
  196. }
  197. bool JsonParser::extractString(std::string &str)
  198. {
  199. if (input[pos] != '\"')
  200. return error("String expected!");
  201. pos++;
  202. size_t first = pos;
  203. while (pos != input.size())
  204. {
  205. if (input[pos] == '\"') // Correct end of string
  206. {
  207. str.append( &input[first], pos-first);
  208. pos++;
  209. return true;
  210. }
  211. if (input[pos] == '\\') // Escaping
  212. {
  213. str.append( &input[first], pos-first);
  214. pos++;
  215. if (pos == input.size())
  216. break;
  217. extractEscaping(str);
  218. first = pos + 1;
  219. }
  220. if (input[pos] == '\n') // end-of-line
  221. {
  222. str.append( &input[first], pos-first);
  223. return error("Closing quote not found!", true);
  224. }
  225. if ((unsigned char)(input[pos]) < ' ') // control character
  226. {
  227. str.append( &input[first], pos-first);
  228. first = pos+1;
  229. error("Illegal character in the string!", true);
  230. }
  231. pos++;
  232. }
  233. return error("Unterminated string!");
  234. }
  235. bool JsonParser::extractString(JsonNode &node)
  236. {
  237. std::string str;
  238. if (!extractString(str))
  239. return false;
  240. node.setType(JsonNode::DATA_STRING);
  241. node.String() = str;
  242. return true;
  243. }
  244. bool JsonParser::extractLiteral(const std::string &literal)
  245. {
  246. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  247. {
  248. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  249. || (input[pos]>'A' && input[pos]<'Z')))
  250. pos++;
  251. return error("Unknown literal found", true);
  252. }
  253. pos += literal.size();
  254. return true;
  255. }
  256. bool JsonParser::extractNull(JsonNode &node)
  257. {
  258. if (!extractLiteral("null"))
  259. return false;
  260. node.clear();
  261. return true;
  262. }
  263. bool JsonParser::extractTrue(JsonNode &node)
  264. {
  265. if (!extractLiteral("true"))
  266. return false;
  267. node.Bool() = true;
  268. return true;
  269. }
  270. bool JsonParser::extractFalse(JsonNode &node)
  271. {
  272. if (!extractLiteral("false"))
  273. return false;
  274. node.Bool() = false;
  275. return true;
  276. }
  277. bool JsonParser::extractStruct(JsonNode &node)
  278. {
  279. node.setType(JsonNode::DATA_STRUCT);
  280. pos++;
  281. if (!extractWhitespace())
  282. return false;
  283. //Empty struct found
  284. if (input[pos] == '}')
  285. {
  286. pos++;
  287. return true;
  288. }
  289. while (true)
  290. {
  291. if (!extractWhitespace())
  292. return false;
  293. std::string key;
  294. if (!extractString(key))
  295. return false;
  296. if (node.Struct().find(key) != node.Struct().end())
  297. error("Dublicated element encountered!", true);
  298. if (!extractSeparator())
  299. return false;
  300. if (!extractElement(node.Struct()[key], '}'))
  301. return false;
  302. if (input[pos] == '}')
  303. {
  304. pos++;
  305. return true;
  306. }
  307. }
  308. }
  309. bool JsonParser::extractArray(JsonNode &node)
  310. {
  311. pos++;
  312. node.setType(JsonNode::DATA_VECTOR);
  313. if (!extractWhitespace())
  314. return false;
  315. //Empty array found
  316. if (input[pos] == ']')
  317. {
  318. pos++;
  319. return true;
  320. }
  321. while (true)
  322. {
  323. //NOTE: currently 50% of time is this vector resizing.
  324. //May be useful to use list during parsing and then swap() all items to vector
  325. node.Vector().resize(node.Vector().size()+1);
  326. if (!extractElement(node.Vector().back(), ']'))
  327. return false;
  328. if (input[pos] == ']')
  329. {
  330. pos++;
  331. return true;
  332. }
  333. }
  334. }
  335. bool JsonParser::extractElement(JsonNode &node, char terminator)
  336. {
  337. if (!extractValue(node))
  338. return false;
  339. if (!extractWhitespace())
  340. return false;
  341. bool comma = (input[pos] == ',');
  342. if (comma )
  343. {
  344. pos++;
  345. if (!extractWhitespace())
  346. return false;
  347. }
  348. if (input[pos] == terminator)
  349. {
  350. //FIXME: MOD COMPATIBILITY: Too many of these right now, re-enable later
  351. //if (comma)
  352. //error("Extra comma found!", true);
  353. return true;
  354. }
  355. if (!comma)
  356. error("Comma expected!", true);
  357. return true;
  358. }
  359. bool JsonParser::extractFloat(JsonNode &node)
  360. {
  361. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  362. bool negative=false;
  363. double result=0;
  364. if (input[pos] == '-')
  365. {
  366. pos++;
  367. negative = true;
  368. }
  369. if (input[pos] < '0' || input[pos] > '9')
  370. return error("Number expected!");
  371. //Extract integer part
  372. while (input[pos] >= '0' && input[pos] <= '9')
  373. {
  374. result = result*10+(input[pos]-'0');
  375. pos++;
  376. }
  377. if (input[pos] == '.')
  378. {
  379. //extract fractional part
  380. pos++;
  381. double fractMult = 0.1;
  382. if (input[pos] < '0' || input[pos] > '9')
  383. return error("Decimal part expected!");
  384. while (input[pos] >= '0' && input[pos] <= '9')
  385. {
  386. result = result + fractMult*(input[pos]-'0');
  387. fractMult /= 10;
  388. pos++;
  389. }
  390. }
  391. //TODO: exponential part
  392. if (negative)
  393. result = -result;
  394. node.setType(JsonNode::DATA_FLOAT);
  395. node.Float() = result;
  396. return true;
  397. }
  398. bool JsonParser::error(const std::string &message, bool warning)
  399. {
  400. std::ostringstream stream;
  401. std::string type(warning?" warning: ":" error: ");
  402. stream << "At line " << lineCount << ", position "<<pos-lineStart
  403. << type << message <<"\n";
  404. errors += stream.str();
  405. return warning;
  406. }
  407. ///////////////////////////////////////////////////////////////////////////////
  408. static const std::unordered_map<std::string, JsonNode::JsonType> stringToType =
  409. boost::assign::map_list_of
  410. ("null", JsonNode::DATA_NULL) ("boolean", JsonNode::DATA_BOOL)
  411. ("number", JsonNode::DATA_FLOAT) ("string", JsonNode::DATA_STRING)
  412. ("array", JsonNode::DATA_VECTOR) ("object", JsonNode::DATA_STRUCT);
  413. namespace
  414. {
  415. namespace Common
  416. {
  417. std::string emptyCheck(Validation::ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)
  418. {
  419. // check is not needed - e.g. incorporated into another check
  420. return "";
  421. }
  422. std::string notImplementedCheck(Validation::ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)
  423. {
  424. return "Not implemented entry in schema";
  425. }
  426. std::string schemaListCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data,
  427. std::string errorMsg, std::function<bool(size_t)> isValid)
  428. {
  429. std::string errors = "<tested schemas>\n";
  430. size_t result = 0;
  431. for(auto & schemaEntry : schema.Vector())
  432. {
  433. std::string error = check(schemaEntry, data, validator);
  434. if (error.empty())
  435. {
  436. result++;
  437. }
  438. else
  439. {
  440. errors += error;
  441. errors += "<end of schema>\n";
  442. }
  443. }
  444. if (isValid(result))
  445. return "";
  446. else
  447. return validator.makeErrorMessage(errorMsg) + errors;
  448. }
  449. std::string allOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  450. {
  451. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass all schemas", [&](size_t count)
  452. {
  453. return count == schema.Vector().size();
  454. });
  455. }
  456. std::string anyOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  457. {
  458. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass any schema", [&](size_t count)
  459. {
  460. return count > 0;
  461. });
  462. }
  463. std::string oneOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  464. {
  465. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass exactly one schema", [&](size_t count)
  466. {
  467. return count == 1;
  468. });
  469. }
  470. std::string notCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  471. {
  472. if (check(schema, data, validator).empty())
  473. return validator.makeErrorMessage("Successful validation against negative check");
  474. return "";
  475. }
  476. std::string enumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  477. {
  478. for(auto & enumEntry : schema.Vector())
  479. {
  480. if (data == enumEntry)
  481. return "";
  482. }
  483. return validator.makeErrorMessage("Key must have one of predefined values");
  484. }
  485. std::string typeCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  486. {
  487. JsonNode::JsonType type = stringToType.find(schema.String())->second;
  488. if(type != data.getType() && data.getType() != JsonNode::DATA_NULL)
  489. return validator.makeErrorMessage("Type mismatch!");
  490. return "";
  491. }
  492. std::string refCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  493. {
  494. std::string URI = schema.String();
  495. //node must be validated using schema pointed by this reference and not by data here
  496. //Local reference. Turn it into more easy to handle remote ref
  497. if (boost::algorithm::starts_with(URI, "#"))
  498. URI = validator.usedSchemas.back() + URI;
  499. return check(JsonUtils::getSchema(URI), data, validator);
  500. }
  501. std::string formatCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  502. {
  503. auto formats = Validation::getKnownFormats();
  504. std::string errors;
  505. auto checker = formats.find(schema.String());
  506. if (checker != formats.end())
  507. {
  508. std::string result = checker->second(data);
  509. if (!result.empty())
  510. errors += validator.makeErrorMessage(result);
  511. }
  512. else
  513. errors += validator.makeErrorMessage("Unknown format: " + schema.String());
  514. return errors;
  515. }
  516. }
  517. namespace String
  518. {
  519. std::string maxLengthCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  520. {
  521. if (data.String().size() > schema.Float())
  522. return validator.makeErrorMessage("String too long");
  523. return "";
  524. }
  525. std::string minLengthCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  526. {
  527. if (data.String().size() < schema.Float())
  528. return validator.makeErrorMessage("String too short");
  529. return "";
  530. }
  531. }
  532. namespace Number
  533. {
  534. std::string maximumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  535. {
  536. if (baseSchema["exclusiveMaximum"].Bool())
  537. {
  538. if (data.Float() >= schema.Float())
  539. return validator.makeErrorMessage("Value is too large");
  540. }
  541. else
  542. {
  543. if (data.Float() > schema.Float())
  544. return validator.makeErrorMessage("Value is too large");
  545. }
  546. return "";
  547. }
  548. std::string minimumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  549. {
  550. if (baseSchema["exclusiveMinimum"].Bool())
  551. {
  552. if (data.Float() <= schema.Float())
  553. return validator.makeErrorMessage("Value is too small");
  554. }
  555. else
  556. {
  557. if (data.Float() < schema.Float())
  558. return validator.makeErrorMessage("Value is too small");
  559. }
  560. return "";
  561. }
  562. std::string multipleOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  563. {
  564. double result = data.Float() / schema.Float();
  565. if (floor(result) != result)
  566. return validator.makeErrorMessage("Value is not divisible");
  567. return "";
  568. }
  569. }
  570. namespace Vector
  571. {
  572. std::string itemEntryCheck(Validation::ValidationData & validator, const JsonVector items, const JsonNode & schema, size_t index)
  573. {
  574. validator.currentPath.push_back(JsonNode());
  575. validator.currentPath.back().Float() = index;
  576. auto onExit = vstd::makeScopeGuard([&]
  577. {
  578. validator.currentPath.pop_back();
  579. });
  580. if (!schema.isNull())
  581. return check(schema, items[index], validator);
  582. return "";
  583. }
  584. std::string itemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  585. {
  586. std::string errors;
  587. for (size_t i=0; i<data.Vector().size(); i++)
  588. {
  589. if (schema.getType() == JsonNode::DATA_VECTOR)
  590. {
  591. if (schema.Vector().size() > i)
  592. errors += itemEntryCheck(validator, data.Vector(), schema.Vector()[i], i);
  593. }
  594. else
  595. {
  596. errors += itemEntryCheck(validator, data.Vector(), schema, i);
  597. }
  598. }
  599. return errors;
  600. }
  601. std::string additionalItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  602. {
  603. std::string errors;
  604. // "items" is struct or empty (defaults to empty struct) - validation always successfull
  605. const JsonNode & items = baseSchema["items"];
  606. if (items.getType() != JsonNode::DATA_VECTOR)
  607. return "";
  608. for (size_t i=items.Vector().size(); i<data.Vector().size(); i++)
  609. {
  610. if (schema.getType() == JsonNode::DATA_STRUCT)
  611. errors += itemEntryCheck(validator, data.Vector(), schema, i);
  612. else if (!schema.isNull() && schema.Bool() == false)
  613. errors += validator.makeErrorMessage("Unknown entry found");
  614. }
  615. return errors;
  616. }
  617. std::string minItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  618. {
  619. if (data.Vector().size() < schema.Float())
  620. return validator.makeErrorMessage("Too few items in the list");
  621. return "";
  622. }
  623. std::string maxItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  624. {
  625. if (data.Vector().size() > schema.Float())
  626. return validator.makeErrorMessage("Too many items in the list!");
  627. return "";
  628. }
  629. std::string uniqueItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  630. {
  631. if (schema.Bool())
  632. {
  633. for (auto itA = schema.Vector().begin(); itA != schema.Vector().end(); itA++)
  634. {
  635. auto itB = itA;
  636. while (++itB != schema.Vector().end())
  637. {
  638. if (*itA == *itB)
  639. return validator.makeErrorMessage("List must consist from unique items");
  640. }
  641. }
  642. }
  643. return "";
  644. }
  645. }
  646. namespace Struct
  647. {
  648. std::string maxPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  649. {
  650. if (data.Struct().size() > schema.Float())
  651. return validator.makeErrorMessage("Too many items in the list!");
  652. return "";
  653. }
  654. std::string minPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  655. {
  656. if (data.Struct().size() < schema.Float())
  657. return validator.makeErrorMessage("Too few items in the list");
  658. return "";
  659. }
  660. std::string uniquePropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  661. {
  662. for (auto itA = data.Struct().begin(); itA != data.Struct().end(); itA++)
  663. {
  664. auto itB = itA;
  665. while (++itB != data.Struct().end())
  666. {
  667. if (itA->second == itB->second)
  668. return validator.makeErrorMessage("List must consist from unique items");
  669. }
  670. }
  671. return "";
  672. }
  673. std::string requiredCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  674. {
  675. std::string errors;
  676. for(auto & required : schema.Vector())
  677. {
  678. if (data[required.String()].isNull())
  679. errors += validator.makeErrorMessage("Required entry " + required.String() + " is missing");
  680. }
  681. return errors;
  682. }
  683. std::string dependenciesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  684. {
  685. std::string errors;
  686. for(auto & deps : schema.Struct())
  687. {
  688. if (!data[deps.first].isNull())
  689. {
  690. if (deps.second.getType() == JsonNode::DATA_VECTOR)
  691. {
  692. JsonVector depList = deps.second.Vector();
  693. for(auto & depEntry : depList)
  694. {
  695. if (data[depEntry.String()].isNull())
  696. errors += validator.makeErrorMessage("Property " + depEntry.String() + " required for " + deps.first + " is missing");
  697. }
  698. }
  699. else
  700. {
  701. if (!check(deps.second, data, validator).empty())
  702. errors += validator.makeErrorMessage("Requirements for " + deps.first + " are not fulfilled");
  703. }
  704. }
  705. }
  706. return errors;
  707. }
  708. std::string propertyEntryCheck(Validation::ValidationData & validator, const JsonNode &node, const JsonNode & schema, std::string nodeName)
  709. {
  710. validator.currentPath.push_back(JsonNode());
  711. validator.currentPath.back().String() = nodeName;
  712. auto onExit = vstd::makeScopeGuard([&]
  713. {
  714. validator.currentPath.pop_back();
  715. });
  716. // there is schema specifically for this item
  717. if (!schema.isNull())
  718. return check(schema, node, validator);
  719. return "";
  720. }
  721. std::string propertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  722. {
  723. std::string errors;
  724. for(auto & entry : data.Struct())
  725. errors += propertyEntryCheck(validator, entry.second, schema[entry.first], entry.first);
  726. return errors;
  727. }
  728. std::string additionalPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  729. {
  730. std::string errors;
  731. for(auto & entry : data.Struct())
  732. {
  733. if (baseSchema["properties"].Struct().count(entry.first) == 0)
  734. {
  735. // try generic additionalItems schema
  736. if (schema.getType() == JsonNode::DATA_STRUCT)
  737. return propertyEntryCheck(validator, entry.second, schema, entry.first);
  738. // or, additionalItems field can be bool which indicates if such items are allowed
  739. if (!schema.isNull() && schema.Bool() == false) // present and set to false - error
  740. return validator.makeErrorMessage("Unknown entry found: " + entry.first);
  741. }
  742. }
  743. return errors;
  744. }
  745. }
  746. namespace Formats
  747. {
  748. #define TEST_FILE(prefix, file, type) \
  749. if (CResourceHandler::get()->existsResource(ResourceID(prefix + file, type))) \
  750. return ""
  751. std::string testAnimation(std::string path)
  752. {
  753. TEST_FILE("Sprites/", path, EResType::ANIMATION);
  754. TEST_FILE("Sprites/", path, EResType::TEXT);
  755. return "Animation file \"" + path + "\" was not found";
  756. }
  757. std::string textFile(const JsonNode & node)
  758. {
  759. TEST_FILE("", node.String(), EResType::TEXT);
  760. return "Text file \"" + node.String() + "\" was not found";
  761. }
  762. std::string musicFile(const JsonNode & node)
  763. {
  764. TEST_FILE("", node.String(), EResType::MUSIC);
  765. return "Music file \"" + node.String() + "\" was not found";
  766. }
  767. std::string soundFile(const JsonNode & node)
  768. {
  769. TEST_FILE("Sounds/", node.String(), EResType::SOUND);
  770. return "Sound file \"" + node.String() + "\" was not found";
  771. }
  772. std::string defFile(const JsonNode & node)
  773. {
  774. TEST_FILE("Sprites/", node.String(), EResType::ANIMATION);
  775. return "Def file \"" + node.String() + "\" was not found";
  776. }
  777. std::string animationFile(const JsonNode & node)
  778. {
  779. return testAnimation(node.String());
  780. }
  781. std::string imageFile(const JsonNode & node)
  782. {
  783. TEST_FILE("Data/", node.String(), EResType::IMAGE);
  784. TEST_FILE("Sprites/", node.String(), EResType::IMAGE);
  785. if (node.String().find(':') != std::string::npos)
  786. return testAnimation(node.String().substr(0, node.String().find(':')));
  787. return "Image file not found";
  788. }
  789. #undef TEST_FILE
  790. }
  791. Validation::TValidatorMap createCommonFields()
  792. {
  793. Validation::TValidatorMap ret;
  794. ret["format"] = Common::formatCheck;
  795. ret["allOf"] = Common::allOfCheck;
  796. ret["anyOf"] = Common::anyOfCheck;
  797. ret["oneOf"] = Common::oneOfCheck;
  798. ret["enum"] = Common::enumCheck;
  799. ret["type"] = Common::typeCheck;
  800. ret["not"] = Common::notCheck;
  801. ret["$ref"] = Common::refCheck;
  802. // fields that don't need implementation
  803. ret["title"] = Common::emptyCheck;
  804. ret["$schema"] = Common::emptyCheck;
  805. ret["default"] = Common::emptyCheck;
  806. ret["description"] = Common::emptyCheck;
  807. ret["definitions"] = Common::emptyCheck;
  808. return ret;
  809. }
  810. Validation::TValidatorMap createStringFields()
  811. {
  812. Validation::TValidatorMap ret = createCommonFields();
  813. ret["maxLength"] = String::maxLengthCheck;
  814. ret["minLength"] = String::minLengthCheck;
  815. ret["pattern"] = Common::notImplementedCheck;
  816. return ret;
  817. }
  818. Validation::TValidatorMap createNumberFields()
  819. {
  820. Validation::TValidatorMap ret = createCommonFields();
  821. ret["maximum"] = Number::maximumCheck;
  822. ret["minimum"] = Number::minimumCheck;
  823. ret["multipleOf"] = Number::multipleOfCheck;
  824. ret["exclusiveMaximum"] = Common::emptyCheck;
  825. ret["exclusiveMinimum"] = Common::emptyCheck;
  826. return ret;
  827. }
  828. Validation::TValidatorMap createVectorFields()
  829. {
  830. Validation::TValidatorMap ret = createCommonFields();
  831. ret["items"] = Vector::itemsCheck;
  832. ret["minItems"] = Vector::minItemsCheck;
  833. ret["maxItems"] = Vector::maxItemsCheck;
  834. ret["uniqueItems"] = Vector::uniqueItemsCheck;
  835. ret["additionalItems"] = Vector::additionalItemsCheck;
  836. return ret;
  837. }
  838. Validation::TValidatorMap createStructFields()
  839. {
  840. Validation::TValidatorMap ret = createCommonFields();
  841. ret["additionalProperties"] = Struct::additionalPropertiesCheck;
  842. ret["uniqueProperties"] = Struct::uniquePropertiesCheck;
  843. ret["maxProperties"] = Struct::maxPropertiesCheck;
  844. ret["minProperties"] = Struct::minPropertiesCheck;
  845. ret["dependencies"] = Struct::dependenciesCheck;
  846. ret["properties"] = Struct::propertiesCheck;
  847. ret["required"] = Struct::requiredCheck;
  848. ret["patternProperties"] = Common::notImplementedCheck;
  849. return ret;
  850. }
  851. Validation::TFormatMap createFormatMap()
  852. {
  853. Validation::TFormatMap ret;
  854. ret["textFile"] = Formats::textFile;
  855. ret["musicFile"] = Formats::musicFile;
  856. ret["soundFile"] = Formats::soundFile;
  857. ret["defFile"] = Formats::defFile;
  858. ret["animationFile"] = Formats::animationFile;
  859. ret["imageFile"] = Formats::imageFile;
  860. return ret;
  861. }
  862. }
  863. namespace Validation
  864. {
  865. std::string ValidationData::makeErrorMessage(const std::string &message)
  866. {
  867. std::string errors;
  868. errors += "At ";
  869. if (!currentPath.empty())
  870. {
  871. for(const JsonNode &path : currentPath)
  872. {
  873. errors += "/";
  874. if (path.getType() == JsonNode::DATA_STRING)
  875. errors += path.String();
  876. else
  877. errors += boost::lexical_cast<std::string>(static_cast<unsigned>(path.Float()));
  878. }
  879. }
  880. else
  881. errors += "<root>";
  882. errors += "\n\t Error: " + message + "\n";
  883. return errors;
  884. }
  885. std::string check(std::string schemaName, const JsonNode & data)
  886. {
  887. ValidationData validator;
  888. return check(schemaName, data, validator);
  889. }
  890. std::string check(std::string schemaName, const JsonNode & data, ValidationData & validator)
  891. {
  892. validator.usedSchemas.push_back(schemaName);
  893. auto onscopeExit = vstd::makeScopeGuard([&]()
  894. {
  895. validator.usedSchemas.pop_back();
  896. });
  897. return check(JsonUtils::getSchema(schemaName), data, validator);
  898. }
  899. std::string check(const JsonNode & schema, const JsonNode & data, ValidationData & validator)
  900. {
  901. const TValidatorMap & knownFields = getKnownFieldsFor(data.getType());
  902. std::string errors;
  903. for(auto & entry : schema.Struct())
  904. {
  905. auto checker = knownFields.find(entry.first);
  906. if (checker != knownFields.end())
  907. errors += checker->second(validator, schema, entry.second, data);
  908. //else
  909. // errors += validator.makeErrorMessage("Unknown entry in schema " + entry.first);
  910. }
  911. return errors;
  912. }
  913. const TValidatorMap & getKnownFieldsFor(JsonNode::JsonType type)
  914. {
  915. static const TValidatorMap commonFields = createCommonFields();
  916. static const TValidatorMap numberFields = createNumberFields();
  917. static const TValidatorMap stringFields = createStringFields();
  918. static const TValidatorMap vectorFields = createVectorFields();
  919. static const TValidatorMap structFields = createStructFields();
  920. switch (type)
  921. {
  922. case JsonNode::DATA_FLOAT: return numberFields;
  923. case JsonNode::DATA_STRING: return stringFields;
  924. case JsonNode::DATA_VECTOR: return vectorFields;
  925. case JsonNode::DATA_STRUCT: return structFields;
  926. default: return commonFields;
  927. }
  928. }
  929. const TFormatMap & getKnownFormats()
  930. {
  931. static TFormatMap knownFormats = createFormatMap();
  932. return knownFormats;
  933. }
  934. } // Validation namespace