mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Converted json validator into a class
This commit is contained in:
		| @@ -11,7 +11,7 @@ | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class JsonFormatException : public std::runtime_error | ||||
| class DLL_LINKAGE JsonFormatException : public std::runtime_error | ||||
| { | ||||
| public: | ||||
| 	using runtime_error::runtime_error; | ||||
|   | ||||
| @@ -17,7 +17,7 @@ class JsonNode; | ||||
| using JsonMap = std::map<std::string, JsonNode>; | ||||
| using JsonVector = std::vector<JsonNode>; | ||||
|  | ||||
| struct JsonParsingSettings | ||||
| struct DLL_LINKAGE JsonParsingSettings | ||||
| { | ||||
| 	enum class JsonFormatMode | ||||
| 	{ | ||||
|   | ||||
| @@ -105,7 +105,8 @@ void JsonUtils::maximize(JsonNode & node, const std::string & schemaName) | ||||
|  | ||||
| bool JsonUtils::validate(const JsonNode & node, const std::string & schemaName, const std::string & dataName) | ||||
| { | ||||
| 	std::string log = Validation::check(schemaName, node); | ||||
| 	JsonValidator validator; | ||||
| 	std::string log = validator.check(schemaName, node); | ||||
| 	if (!log.empty()) | ||||
| 	{ | ||||
| 		logMod->warn("Data in %s is invalid!", dataName); | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -13,28 +13,23 @@ | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
|  | ||||
| //Internal class for Json validation. Mostly compilant with json-schema v6 draft | ||||
| namespace Validation | ||||
| /// Class for Json validation. Mostly compilant with json-schema v6 draf | ||||
| struct JsonValidator | ||||
| { | ||||
| 	/// struct used to pass data around during validation | ||||
| 	struct ValidationData | ||||
| 	{ | ||||
| 		/// path from root node to current one. | ||||
| 		/// JsonNode is used as variant - either string (name of node) or as float (index in list) | ||||
| 		std::vector<JsonNode> currentPath; | ||||
| 	/// path from root node to current one. | ||||
| 	/// JsonNode is used as variant - either string (name of node) or as float (index in list) | ||||
| 	std::vector<JsonNode> currentPath; | ||||
|  | ||||
| 		/// Stack of used schemas. Last schema is the one used currently. | ||||
| 		/// May contain multiple items in case if remote references were found | ||||
| 		std::vector<std::string> usedSchemas; | ||||
| 	/// Stack of used schemas. Last schema is the one used currently. | ||||
| 	/// May contain multiple items in case if remote references were found | ||||
| 	std::vector<std::string> usedSchemas; | ||||
|  | ||||
| 		/// generates error message | ||||
| 		std::string makeErrorMessage(const std::string &message); | ||||
| 	}; | ||||
| 	/// generates error message | ||||
| 	std::string makeErrorMessage(const std::string &message); | ||||
|  | ||||
| 	using TFormatValidator = std::function<std::string(const JsonNode &)>; | ||||
| 	using TFormatMap = std::unordered_map<std::string, TFormatValidator>; | ||||
| 	using TFieldValidator = std::function<std::string(ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)>; | ||||
| 	using TFieldValidator = std::function<std::string(JsonValidator &, const JsonNode &, const JsonNode &, const JsonNode &)>; | ||||
| 	using TValidatorMap = std::unordered_map<std::string, TFieldValidator>; | ||||
|  | ||||
| 	/// map of known fields in schema | ||||
| @@ -42,8 +37,7 @@ namespace Validation | ||||
| 	const TFormatMap & getKnownFormats(); | ||||
|  | ||||
| 	std::string check(const std::string & schemaName, const JsonNode & data); | ||||
| 	std::string check(const std::string & schemaName, const JsonNode & data, ValidationData & validator); | ||||
| 	std::string check(const JsonNode & schema, const JsonNode & data, ValidationData & validator); | ||||
| } | ||||
| 	std::string check(const JsonNode & schema, const JsonNode & data); | ||||
| }; | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
|   | ||||
		Reference in New Issue
	
	Block a user