JsonDetail.cpp 30 KB

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