JsonDetail.cpp 29 KB

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