JsonDetail.cpp 29 KB

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