schema.h 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. // Tencent is pleased to support the open source community by making RapidJSON available->
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip-> All rights reserved->
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License-> You may obtain a copy of the License at
  7. //
  8. // http://opensource->org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied-> See the License for the
  13. // specific language governing permissions and limitations under the License->
  14. #ifndef RAPIDJSON_SCHEMA_H_
  15. #define RAPIDJSON_SCHEMA_H_
  16. #include "document.h"
  17. #include "pointer.h"
  18. #include <cmath> // abs, floor
  19. #if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX)
  20. #define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1
  21. #else
  22. #define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0
  23. #endif
  24. #if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800))
  25. #define RAPIDJSON_SCHEMA_USE_STDREGEX 1
  26. #else
  27. #define RAPIDJSON_SCHEMA_USE_STDREGEX 0
  28. #endif
  29. #if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
  30. #include "internal/regex.h"
  31. #elif RAPIDJSON_SCHEMA_USE_STDREGEX
  32. #include <regex>
  33. #endif
  34. #if RAPIDJSON_SCHEMA_USE_INTERNALREGEX || RAPIDJSON_SCHEMA_USE_STDREGEX
  35. #define RAPIDJSON_SCHEMA_HAS_REGEX 1
  36. #else
  37. #define RAPIDJSON_SCHEMA_HAS_REGEX 0
  38. #endif
  39. #ifndef RAPIDJSON_SCHEMA_VERBOSE
  40. #define RAPIDJSON_SCHEMA_VERBOSE 0
  41. #endif
  42. #if RAPIDJSON_SCHEMA_VERBOSE
  43. #include "stringbuffer.h"
  44. #endif
  45. RAPIDJSON_DIAG_PUSH
  46. #if defined(__GNUC__)
  47. RAPIDJSON_DIAG_OFF(effc++)
  48. #endif
  49. #ifdef __clang__
  50. RAPIDJSON_DIAG_OFF(weak-vtables)
  51. RAPIDJSON_DIAG_OFF(exit-time-destructors)
  52. RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
  53. RAPIDJSON_DIAG_OFF(variadic-macros)
  54. #endif
  55. #ifdef _MSC_VER
  56. RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
  57. #endif
  58. RAPIDJSON_NAMESPACE_BEGIN
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // Verbose Utilities
  61. #if RAPIDJSON_SCHEMA_VERBOSE
  62. namespace internal {
  63. inline void PrintInvalidKeyword(const char* keyword) {
  64. printf("Fail keyword: %s\n", keyword);
  65. }
  66. inline void PrintInvalidKeyword(const wchar_t* keyword) {
  67. wprintf(L"Fail keyword: %ls\n", keyword);
  68. }
  69. inline void PrintInvalidDocument(const char* document) {
  70. printf("Fail document: %s\n\n", document);
  71. }
  72. inline void PrintInvalidDocument(const wchar_t* document) {
  73. wprintf(L"Fail document: %ls\n\n", document);
  74. }
  75. inline void PrintValidatorPointers(unsigned depth, const char* s, const char* d) {
  76. printf("S: %*s%s\nD: %*s%s\n\n", depth * 4, " ", s, depth * 4, " ", d);
  77. }
  78. inline void PrintValidatorPointers(unsigned depth, const wchar_t* s, const wchar_t* d) {
  79. wprintf(L"S: %*ls%ls\nD: %*ls%ls\n\n", depth * 4, L" ", s, depth * 4, L" ", d);
  80. }
  81. } // namespace internal
  82. #endif // RAPIDJSON_SCHEMA_VERBOSE
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // RAPIDJSON_INVALID_KEYWORD_RETURN
  85. #if RAPIDJSON_SCHEMA_VERBOSE
  86. #define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) internal::PrintInvalidKeyword(keyword)
  87. #else
  88. #define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword)
  89. #endif
  90. #define RAPIDJSON_INVALID_KEYWORD_RETURN(keyword)\
  91. RAPIDJSON_MULTILINEMACRO_BEGIN\
  92. context.invalidKeyword = keyword.GetString();\
  93. RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword.GetString());\
  94. return false;\
  95. RAPIDJSON_MULTILINEMACRO_END
  96. ///////////////////////////////////////////////////////////////////////////////
  97. // Forward declarations
  98. template <typename ValueType, typename Allocator>
  99. class GenericSchemaDocument;
  100. namespace internal {
  101. template <typename SchemaDocumentType>
  102. class Schema;
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // ISchemaValidator
  105. class ISchemaValidator {
  106. public:
  107. virtual ~ISchemaValidator() {}
  108. virtual bool IsValid() const = 0;
  109. };
  110. ///////////////////////////////////////////////////////////////////////////////
  111. // ISchemaStateFactory
  112. template <typename SchemaType>
  113. class ISchemaStateFactory {
  114. public:
  115. virtual ~ISchemaStateFactory() {}
  116. virtual ISchemaValidator* CreateSchemaValidator(const SchemaType&) = 0;
  117. virtual void DestroySchemaValidator(ISchemaValidator* validator) = 0;
  118. virtual void* CreateHasher() = 0;
  119. virtual uint64_t GetHashCode(void* hasher) = 0;
  120. virtual void DestroryHasher(void* hasher) = 0;
  121. virtual void* MallocState(size_t size) = 0;
  122. virtual void FreeState(void* p) = 0;
  123. };
  124. ///////////////////////////////////////////////////////////////////////////////
  125. // Hasher
  126. // For comparison of compound value
  127. template<typename Encoding, typename Allocator>
  128. class Hasher {
  129. public:
  130. typedef typename Encoding::Ch Ch;
  131. Hasher(Allocator* allocator = 0, size_t stackCapacity = kDefaultSize) : stack_(allocator, stackCapacity) {}
  132. bool Null() { return WriteType(kNullType); }
  133. bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); }
  134. bool Int(int i) { Number n; n.u.i = i; n.d = static_cast<double>(i); return WriteNumber(n); }
  135. bool Uint(unsigned u) { Number n; n.u.u = u; n.d = static_cast<double>(u); return WriteNumber(n); }
  136. bool Int64(int64_t i) { Number n; n.u.i = i; n.d = static_cast<double>(i); return WriteNumber(n); }
  137. bool Uint64(uint64_t u) { Number n; n.u.u = u; n.d = static_cast<double>(u); return WriteNumber(n); }
  138. bool Double(double d) {
  139. Number n;
  140. if (d < 0) n.u.i = static_cast<int64_t>(d);
  141. else n.u.u = static_cast<uint64_t>(d);
  142. n.d = d;
  143. return WriteNumber(n);
  144. }
  145. bool RawNumber(const Ch* str, SizeType len, bool) {
  146. WriteBuffer(kNumberType, str, len * sizeof(Ch));
  147. return true;
  148. }
  149. bool String(const Ch* str, SizeType len, bool) {
  150. WriteBuffer(kStringType, str, len * sizeof(Ch));
  151. return true;
  152. }
  153. bool StartObject() { return true; }
  154. bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); }
  155. bool EndObject(SizeType memberCount) {
  156. uint64_t h = Hash(0, kObjectType);
  157. uint64_t* kv = stack_.template Pop<uint64_t>(memberCount * 2);
  158. for (SizeType i = 0; i < memberCount; i++)
  159. h ^= Hash(kv[i * 2], kv[i * 2 + 1]); // Use xor to achieve member order insensitive
  160. *stack_.template Push<uint64_t>() = h;
  161. return true;
  162. }
  163. bool StartArray() { return true; }
  164. bool EndArray(SizeType elementCount) {
  165. uint64_t h = Hash(0, kArrayType);
  166. uint64_t* e = stack_.template Pop<uint64_t>(elementCount);
  167. for (SizeType i = 0; i < elementCount; i++)
  168. h = Hash(h, e[i]); // Use hash to achieve element order sensitive
  169. *stack_.template Push<uint64_t>() = h;
  170. return true;
  171. }
  172. bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); }
  173. uint64_t GetHashCode() const {
  174. RAPIDJSON_ASSERT(IsValid());
  175. return *stack_.template Top<uint64_t>();
  176. }
  177. private:
  178. static const size_t kDefaultSize = 256;
  179. struct Number {
  180. union U {
  181. uint64_t u;
  182. int64_t i;
  183. }u;
  184. double d;
  185. };
  186. bool WriteType(Type type) { return WriteBuffer(type, 0, 0); }
  187. bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); }
  188. bool WriteBuffer(Type type, const void* data, size_t len) {
  189. // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/
  190. uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type);
  191. const unsigned char* d = static_cast<const unsigned char*>(data);
  192. for (size_t i = 0; i < len; i++)
  193. h = Hash(h, d[i]);
  194. *stack_.template Push<uint64_t>() = h;
  195. return true;
  196. }
  197. static uint64_t Hash(uint64_t h, uint64_t d) {
  198. static const uint64_t kPrime = RAPIDJSON_UINT64_C2(0x00000100, 0x000001b3);
  199. h ^= d;
  200. h *= kPrime;
  201. return h;
  202. }
  203. Stack<Allocator> stack_;
  204. };
  205. ///////////////////////////////////////////////////////////////////////////////
  206. // SchemaValidationContext
  207. template <typename SchemaDocumentType>
  208. struct SchemaValidationContext {
  209. typedef Schema<SchemaDocumentType> SchemaType;
  210. typedef ISchemaStateFactory<SchemaType> SchemaValidatorFactoryType;
  211. typedef typename SchemaType::ValueType ValueType;
  212. typedef typename ValueType::Ch Ch;
  213. enum PatternValidatorType {
  214. kPatternValidatorOnly,
  215. kPatternValidatorWithProperty,
  216. kPatternValidatorWithAdditionalProperty
  217. };
  218. SchemaValidationContext(SchemaValidatorFactoryType& f, const SchemaType* s) :
  219. factory(f),
  220. schema(s),
  221. valueSchema(),
  222. invalidKeyword(),
  223. hasher(),
  224. arrayElementHashCodes(),
  225. validators(),
  226. validatorCount(),
  227. patternPropertiesValidators(),
  228. patternPropertiesValidatorCount(),
  229. patternPropertiesSchemas(),
  230. patternPropertiesSchemaCount(),
  231. valuePatternValidatorType(kPatternValidatorOnly),
  232. propertyExist(),
  233. inArray(false),
  234. valueUniqueness(false),
  235. arrayUniqueness(false)
  236. {
  237. }
  238. ~SchemaValidationContext() {
  239. if (hasher)
  240. factory.DestroryHasher(hasher);
  241. if (validators) {
  242. for (SizeType i = 0; i < validatorCount; i++)
  243. factory.DestroySchemaValidator(validators[i]);
  244. factory.FreeState(validators);
  245. }
  246. if (patternPropertiesValidators) {
  247. for (SizeType i = 0; i < patternPropertiesValidatorCount; i++)
  248. factory.DestroySchemaValidator(patternPropertiesValidators[i]);
  249. factory.FreeState(patternPropertiesValidators);
  250. }
  251. if (patternPropertiesSchemas)
  252. factory.FreeState(patternPropertiesSchemas);
  253. if (propertyExist)
  254. factory.FreeState(propertyExist);
  255. }
  256. SchemaValidatorFactoryType& factory;
  257. const SchemaType* schema;
  258. const SchemaType* valueSchema;
  259. const Ch* invalidKeyword;
  260. void* hasher; // Only validator access
  261. void* arrayElementHashCodes; // Only validator access this
  262. ISchemaValidator** validators;
  263. SizeType validatorCount;
  264. ISchemaValidator** patternPropertiesValidators;
  265. SizeType patternPropertiesValidatorCount;
  266. const SchemaType** patternPropertiesSchemas;
  267. SizeType patternPropertiesSchemaCount;
  268. PatternValidatorType valuePatternValidatorType;
  269. PatternValidatorType objectPatternValidatorType;
  270. SizeType arrayElementIndex;
  271. bool* propertyExist;
  272. bool inArray;
  273. bool valueUniqueness;
  274. bool arrayUniqueness;
  275. };
  276. ///////////////////////////////////////////////////////////////////////////////
  277. // Schema
  278. template <typename SchemaDocumentType>
  279. class Schema {
  280. public:
  281. typedef typename SchemaDocumentType::ValueType ValueType;
  282. typedef typename SchemaDocumentType::AllocatorType AllocatorType;
  283. typedef typename SchemaDocumentType::PointerType PointerType;
  284. typedef typename ValueType::EncodingType EncodingType;
  285. typedef typename EncodingType::Ch Ch;
  286. typedef SchemaValidationContext<SchemaDocumentType> Context;
  287. typedef Schema<SchemaDocumentType> SchemaType;
  288. typedef GenericValue<EncodingType, AllocatorType> SValue;
  289. friend class GenericSchemaDocument<ValueType, AllocatorType>;
  290. Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) :
  291. allocator_(allocator),
  292. enum_(),
  293. enumCount_(),
  294. not_(),
  295. type_((1 << kTotalSchemaType) - 1), // typeless
  296. validatorCount_(),
  297. properties_(),
  298. additionalPropertiesSchema_(),
  299. patternProperties_(),
  300. patternPropertyCount_(),
  301. propertyCount_(),
  302. minProperties_(),
  303. maxProperties_(SizeType(~0)),
  304. additionalProperties_(true),
  305. hasDependencies_(),
  306. hasRequired_(),
  307. hasSchemaDependencies_(),
  308. additionalItemsSchema_(),
  309. itemsList_(),
  310. itemsTuple_(),
  311. itemsTupleCount_(),
  312. minItems_(),
  313. maxItems_(SizeType(~0)),
  314. additionalItems_(true),
  315. uniqueItems_(false),
  316. pattern_(),
  317. minLength_(0),
  318. maxLength_(~SizeType(0)),
  319. exclusiveMinimum_(false),
  320. exclusiveMaximum_(false)
  321. {
  322. typedef typename SchemaDocumentType::ValueType ValueType;
  323. typedef typename ValueType::ConstValueIterator ConstValueIterator;
  324. typedef typename ValueType::ConstMemberIterator ConstMemberIterator;
  325. if (!value.IsObject())
  326. return;
  327. if (const ValueType* v = GetMember(value, GetTypeString())) {
  328. type_ = 0;
  329. if (v->IsString())
  330. AddType(*v);
  331. else if (v->IsArray())
  332. for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr)
  333. AddType(*itr);
  334. }
  335. if (const ValueType* v = GetMember(value, GetEnumString()))
  336. if (v->IsArray() && v->Size() > 0) {
  337. enum_ = static_cast<uint64_t*>(allocator_->Malloc(sizeof(uint64_t) * v->Size()));
  338. for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) {
  339. typedef Hasher<EncodingType, MemoryPoolAllocator<> > EnumHasherType;
  340. char buffer[256 + 24];
  341. MemoryPoolAllocator<> hasherAllocator(buffer, sizeof(buffer));
  342. EnumHasherType h(&hasherAllocator, 256);
  343. itr->Accept(h);
  344. enum_[enumCount_++] = h.GetHashCode();
  345. }
  346. }
  347. if (schemaDocument) {
  348. AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document);
  349. AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document);
  350. AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document);
  351. }
  352. if (const ValueType* v = GetMember(value, GetNotString())) {
  353. schemaDocument->CreateSchema(&not_, p.Append(GetNotString(), allocator_), *v, document);
  354. notValidatorIndex_ = validatorCount_;
  355. validatorCount_++;
  356. }
  357. // Object
  358. const ValueType* properties = GetMember(value, GetPropertiesString());
  359. const ValueType* required = GetMember(value, GetRequiredString());
  360. const ValueType* dependencies = GetMember(value, GetDependenciesString());
  361. {
  362. // Gather properties from properties/required/dependencies
  363. SValue allProperties(kArrayType);
  364. if (properties && properties->IsObject())
  365. for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr)
  366. AddUniqueElement(allProperties, itr->name);
  367. if (required && required->IsArray())
  368. for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr)
  369. if (itr->IsString())
  370. AddUniqueElement(allProperties, *itr);
  371. if (dependencies && dependencies->IsObject())
  372. for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) {
  373. AddUniqueElement(allProperties, itr->name);
  374. if (itr->value.IsArray())
  375. for (ConstValueIterator i = itr->value.Begin(); i != itr->value.End(); ++i)
  376. if (i->IsString())
  377. AddUniqueElement(allProperties, *i);
  378. }
  379. if (allProperties.Size() > 0) {
  380. propertyCount_ = allProperties.Size();
  381. properties_ = static_cast<Property*>(allocator_->Malloc(sizeof(Property) * propertyCount_));
  382. for (SizeType i = 0; i < propertyCount_; i++) {
  383. new (&properties_[i]) Property();
  384. properties_[i].name = allProperties[i];
  385. properties_[i].schema = GetTypeless();
  386. }
  387. }
  388. }
  389. if (properties && properties->IsObject()) {
  390. PointerType q = p.Append(GetPropertiesString(), allocator_);
  391. for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) {
  392. SizeType index;
  393. if (FindPropertyIndex(itr->name, &index))
  394. schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document);
  395. }
  396. }
  397. if (const ValueType* v = GetMember(value, GetPatternPropertiesString())) {
  398. PointerType q = p.Append(GetPatternPropertiesString(), allocator_);
  399. patternProperties_ = static_cast<PatternProperty*>(allocator_->Malloc(sizeof(PatternProperty) * v->MemberCount()));
  400. patternPropertyCount_ = 0;
  401. for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) {
  402. new (&patternProperties_[patternPropertyCount_]) PatternProperty();
  403. patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name);
  404. schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value, document);
  405. patternPropertyCount_++;
  406. }
  407. }
  408. if (required && required->IsArray())
  409. for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr)
  410. if (itr->IsString()) {
  411. SizeType index;
  412. if (FindPropertyIndex(*itr, &index)) {
  413. properties_[index].required = true;
  414. hasRequired_ = true;
  415. }
  416. }
  417. if (dependencies && dependencies->IsObject()) {
  418. PointerType q = p.Append(GetDependenciesString(), allocator_);
  419. hasDependencies_ = true;
  420. for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) {
  421. SizeType sourceIndex;
  422. if (FindPropertyIndex(itr->name, &sourceIndex)) {
  423. if (itr->value.IsArray()) {
  424. properties_[sourceIndex].dependencies = static_cast<bool*>(allocator_->Malloc(sizeof(bool) * propertyCount_));
  425. std::memset(properties_[sourceIndex].dependencies, 0, sizeof(bool)* propertyCount_);
  426. for (ConstValueIterator targetItr = itr->value.Begin(); targetItr != itr->value.End(); ++targetItr) {
  427. SizeType targetIndex;
  428. if (FindPropertyIndex(*targetItr, &targetIndex))
  429. properties_[sourceIndex].dependencies[targetIndex] = true;
  430. }
  431. }
  432. else if (itr->value.IsObject()) {
  433. hasSchemaDependencies_ = true;
  434. schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document);
  435. properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_;
  436. validatorCount_++;
  437. }
  438. }
  439. }
  440. }
  441. if (const ValueType* v = GetMember(value, GetAdditionalPropertiesString())) {
  442. if (v->IsBool())
  443. additionalProperties_ = v->GetBool();
  444. else if (v->IsObject())
  445. schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document);
  446. }
  447. AssignIfExist(minProperties_, value, GetMinPropertiesString());
  448. AssignIfExist(maxProperties_, value, GetMaxPropertiesString());
  449. // Array
  450. if (const ValueType* v = GetMember(value, GetItemsString())) {
  451. PointerType q = p.Append(GetItemsString(), allocator_);
  452. if (v->IsObject()) // List validation
  453. schemaDocument->CreateSchema(&itemsList_, q, *v, document);
  454. else if (v->IsArray()) { // Tuple validation
  455. itemsTuple_ = static_cast<const Schema**>(allocator_->Malloc(sizeof(const Schema*) * v->Size()));
  456. SizeType index = 0;
  457. for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++)
  458. schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document);
  459. }
  460. }
  461. AssignIfExist(minItems_, value, GetMinItemsString());
  462. AssignIfExist(maxItems_, value, GetMaxItemsString());
  463. if (const ValueType* v = GetMember(value, GetAdditionalItemsString())) {
  464. if (v->IsBool())
  465. additionalItems_ = v->GetBool();
  466. else if (v->IsObject())
  467. schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document);
  468. }
  469. AssignIfExist(uniqueItems_, value, GetUniqueItemsString());
  470. // String
  471. AssignIfExist(minLength_, value, GetMinLengthString());
  472. AssignIfExist(maxLength_, value, GetMaxLengthString());
  473. if (const ValueType* v = GetMember(value, GetPatternString()))
  474. pattern_ = CreatePattern(*v);
  475. // Number
  476. if (const ValueType* v = GetMember(value, GetMinimumString()))
  477. if (v->IsNumber())
  478. minimum_.CopyFrom(*v, *allocator_);
  479. if (const ValueType* v = GetMember(value, GetMaximumString()))
  480. if (v->IsNumber())
  481. maximum_.CopyFrom(*v, *allocator_);
  482. AssignIfExist(exclusiveMinimum_, value, GetExclusiveMinimumString());
  483. AssignIfExist(exclusiveMaximum_, value, GetExclusiveMaximumString());
  484. if (const ValueType* v = GetMember(value, GetMultipleOfString()))
  485. if (v->IsNumber() && v->GetDouble() > 0.0)
  486. multipleOf_.CopyFrom(*v, *allocator_);
  487. }
  488. ~Schema() {
  489. if (allocator_) {
  490. allocator_->Free(enum_);
  491. }
  492. if (properties_) {
  493. for (SizeType i = 0; i < propertyCount_; i++)
  494. properties_[i].~Property();
  495. AllocatorType::Free(properties_);
  496. }
  497. if (patternProperties_) {
  498. for (SizeType i = 0; i < patternPropertyCount_; i++)
  499. patternProperties_[i].~PatternProperty();
  500. AllocatorType::Free(patternProperties_);
  501. }
  502. AllocatorType::Free(itemsTuple_);
  503. #if RAPIDJSON_SCHEMA_HAS_REGEX
  504. if (pattern_) {
  505. pattern_->~RegexType();
  506. allocator_->Free(pattern_);
  507. }
  508. #endif
  509. }
  510. bool BeginValue(Context& context) const {
  511. if (context.inArray) {
  512. if (uniqueItems_)
  513. context.valueUniqueness = true;
  514. if (itemsList_)
  515. context.valueSchema = itemsList_;
  516. else if (itemsTuple_) {
  517. if (context.arrayElementIndex < itemsTupleCount_)
  518. context.valueSchema = itemsTuple_[context.arrayElementIndex];
  519. else if (additionalItemsSchema_)
  520. context.valueSchema = additionalItemsSchema_;
  521. else if (additionalItems_)
  522. context.valueSchema = GetTypeless();
  523. else
  524. RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString());
  525. }
  526. else
  527. context.valueSchema = GetTypeless();
  528. context.arrayElementIndex++;
  529. }
  530. return true;
  531. }
  532. RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const {
  533. if (context.patternPropertiesValidatorCount > 0) {
  534. bool otherValid = false;
  535. SizeType count = context.patternPropertiesValidatorCount;
  536. if (context.objectPatternValidatorType != Context::kPatternValidatorOnly)
  537. otherValid = context.patternPropertiesValidators[--count]->IsValid();
  538. bool patternValid = true;
  539. for (SizeType i = 0; i < count; i++)
  540. if (!context.patternPropertiesValidators[i]->IsValid()) {
  541. patternValid = false;
  542. break;
  543. }
  544. if (context.objectPatternValidatorType == Context::kPatternValidatorOnly) {
  545. if (!patternValid)
  546. RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString());
  547. }
  548. else if (context.objectPatternValidatorType == Context::kPatternValidatorWithProperty) {
  549. if (!patternValid || !otherValid)
  550. RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString());
  551. }
  552. else if (!patternValid && !otherValid) // kPatternValidatorWithAdditionalProperty)
  553. RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString());
  554. }
  555. if (enum_) {
  556. const uint64_t h = context.factory.GetHashCode(context.hasher);
  557. for (SizeType i = 0; i < enumCount_; i++)
  558. if (enum_[i] == h)
  559. goto foundEnum;
  560. RAPIDJSON_INVALID_KEYWORD_RETURN(GetEnumString());
  561. foundEnum:;
  562. }
  563. if (allOf_.schemas)
  564. for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++)
  565. if (!context.validators[i]->IsValid())
  566. RAPIDJSON_INVALID_KEYWORD_RETURN(GetAllOfString());
  567. if (anyOf_.schemas) {
  568. for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++)
  569. if (context.validators[i]->IsValid())
  570. goto foundAny;
  571. RAPIDJSON_INVALID_KEYWORD_RETURN(GetAnyOfString());
  572. foundAny:;
  573. }
  574. if (oneOf_.schemas) {
  575. bool oneValid = false;
  576. for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++)
  577. if (context.validators[i]->IsValid()) {
  578. if (oneValid)
  579. RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString());
  580. else
  581. oneValid = true;
  582. }
  583. if (!oneValid)
  584. RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString());
  585. }
  586. if (not_ && context.validators[notValidatorIndex_]->IsValid())
  587. RAPIDJSON_INVALID_KEYWORD_RETURN(GetNotString());
  588. return true;
  589. }
  590. bool Null(Context& context) const {
  591. if (!(type_ & (1 << kNullSchemaType)))
  592. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  593. return CreateParallelValidator(context);
  594. }
  595. bool Bool(Context& context, bool) const {
  596. if (!(type_ & (1 << kBooleanSchemaType)))
  597. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  598. return CreateParallelValidator(context);
  599. }
  600. bool Int(Context& context, int i) const {
  601. if (!CheckInt(context, i))
  602. return false;
  603. return CreateParallelValidator(context);
  604. }
  605. bool Uint(Context& context, unsigned u) const {
  606. if (!CheckUint(context, u))
  607. return false;
  608. return CreateParallelValidator(context);
  609. }
  610. bool Int64(Context& context, int64_t i) const {
  611. if (!CheckInt(context, i))
  612. return false;
  613. return CreateParallelValidator(context);
  614. }
  615. bool Uint64(Context& context, uint64_t u) const {
  616. if (!CheckUint(context, u))
  617. return false;
  618. return CreateParallelValidator(context);
  619. }
  620. bool Double(Context& context, double d) const {
  621. if (!(type_ & (1 << kNumberSchemaType)))
  622. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  623. if (!minimum_.IsNull() && !CheckDoubleMinimum(context, d))
  624. return false;
  625. if (!maximum_.IsNull() && !CheckDoubleMaximum(context, d))
  626. return false;
  627. if (!multipleOf_.IsNull() && !CheckDoubleMultipleOf(context, d))
  628. return false;
  629. return CreateParallelValidator(context);
  630. }
  631. bool String(Context& context, const Ch* str, SizeType length, bool) const {
  632. if (!(type_ & (1 << kStringSchemaType)))
  633. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  634. if (minLength_ != 0 || maxLength_ != SizeType(~0)) {
  635. SizeType count;
  636. if (internal::CountStringCodePoint<EncodingType>(str, length, &count)) {
  637. if (count < minLength_)
  638. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinLengthString());
  639. if (count > maxLength_)
  640. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxLengthString());
  641. }
  642. }
  643. if (pattern_ && !IsPatternMatch(pattern_, str, length))
  644. RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternString());
  645. return CreateParallelValidator(context);
  646. }
  647. bool StartObject(Context& context) const {
  648. if (!(type_ & (1 << kObjectSchemaType)))
  649. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  650. if (hasDependencies_ || hasRequired_) {
  651. context.propertyExist = static_cast<bool*>(context.factory.MallocState(sizeof(bool) * propertyCount_));
  652. std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_);
  653. }
  654. if (patternProperties_) { // pre-allocate schema array
  655. SizeType count = patternPropertyCount_ + 1; // extra for valuePatternValidatorType
  656. context.patternPropertiesSchemas = static_cast<const SchemaType**>(context.factory.MallocState(sizeof(const SchemaType*) * count));
  657. context.patternPropertiesSchemaCount = 0;
  658. std::memset(context.patternPropertiesSchemas, 0, sizeof(SchemaType*) * count);
  659. }
  660. return CreateParallelValidator(context);
  661. }
  662. bool Key(Context& context, const Ch* str, SizeType len, bool) const {
  663. if (patternProperties_) {
  664. context.patternPropertiesSchemaCount = 0;
  665. for (SizeType i = 0; i < patternPropertyCount_; i++)
  666. if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len))
  667. context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema;
  668. }
  669. SizeType index;
  670. if (FindPropertyIndex(ValueType(str, len).Move(), &index)) {
  671. if (context.patternPropertiesSchemaCount > 0) {
  672. context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema;
  673. context.valueSchema = GetTypeless();
  674. context.valuePatternValidatorType = Context::kPatternValidatorWithProperty;
  675. }
  676. else
  677. context.valueSchema = properties_[index].schema;
  678. if (context.propertyExist)
  679. context.propertyExist[index] = true;
  680. return true;
  681. }
  682. if (additionalPropertiesSchema_) {
  683. if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) {
  684. context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_;
  685. context.valueSchema = GetTypeless();
  686. context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty;
  687. }
  688. else
  689. context.valueSchema = additionalPropertiesSchema_;
  690. return true;
  691. }
  692. else if (additionalProperties_) {
  693. context.valueSchema = GetTypeless();
  694. return true;
  695. }
  696. if (context.patternPropertiesSchemaCount == 0) // patternProperties are not additional properties
  697. RAPIDJSON_INVALID_KEYWORD_RETURN(GetAdditionalPropertiesString());
  698. return true;
  699. }
  700. bool EndObject(Context& context, SizeType memberCount) const {
  701. if (hasRequired_)
  702. for (SizeType index = 0; index < propertyCount_; index++)
  703. if (properties_[index].required)
  704. if (!context.propertyExist[index])
  705. RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString());
  706. if (memberCount < minProperties_)
  707. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString());
  708. if (memberCount > maxProperties_)
  709. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxPropertiesString());
  710. if (hasDependencies_) {
  711. for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++)
  712. if (context.propertyExist[sourceIndex]) {
  713. if (properties_[sourceIndex].dependencies) {
  714. for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++)
  715. if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex])
  716. RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString());
  717. }
  718. else if (properties_[sourceIndex].dependenciesSchema)
  719. if (!context.validators[properties_[sourceIndex].dependenciesValidatorIndex]->IsValid())
  720. RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString());
  721. }
  722. }
  723. return true;
  724. }
  725. bool StartArray(Context& context) const {
  726. if (!(type_ & (1 << kArraySchemaType)))
  727. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  728. context.arrayElementIndex = 0;
  729. context.inArray = true;
  730. return CreateParallelValidator(context);
  731. }
  732. bool EndArray(Context& context, SizeType elementCount) const {
  733. context.inArray = false;
  734. if (elementCount < minItems_)
  735. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinItemsString());
  736. if (elementCount > maxItems_)
  737. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxItemsString());
  738. return true;
  739. }
  740. // Generate functions for string literal according to Ch
  741. #define RAPIDJSON_STRING_(name, ...) \
  742. static const ValueType& Get##name##String() {\
  743. static const Ch s[] = { __VA_ARGS__, '\0' };\
  744. static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\
  745. return v;\
  746. }
  747. RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l')
  748. RAPIDJSON_STRING_(Boolean, 'b', 'o', 'o', 'l', 'e', 'a', 'n')
  749. RAPIDJSON_STRING_(Object, 'o', 'b', 'j', 'e', 'c', 't')
  750. RAPIDJSON_STRING_(Array, 'a', 'r', 'r', 'a', 'y')
  751. RAPIDJSON_STRING_(String, 's', 't', 'r', 'i', 'n', 'g')
  752. RAPIDJSON_STRING_(Number, 'n', 'u', 'm', 'b', 'e', 'r')
  753. RAPIDJSON_STRING_(Integer, 'i', 'n', 't', 'e', 'g', 'e', 'r')
  754. RAPIDJSON_STRING_(Type, 't', 'y', 'p', 'e')
  755. RAPIDJSON_STRING_(Enum, 'e', 'n', 'u', 'm')
  756. RAPIDJSON_STRING_(AllOf, 'a', 'l', 'l', 'O', 'f')
  757. RAPIDJSON_STRING_(AnyOf, 'a', 'n', 'y', 'O', 'f')
  758. RAPIDJSON_STRING_(OneOf, 'o', 'n', 'e', 'O', 'f')
  759. RAPIDJSON_STRING_(Not, 'n', 'o', 't')
  760. RAPIDJSON_STRING_(Properties, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's')
  761. RAPIDJSON_STRING_(Required, 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd')
  762. RAPIDJSON_STRING_(Dependencies, 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'i', 'e', 's')
  763. RAPIDJSON_STRING_(PatternProperties, 'p', 'a', 't', 't', 'e', 'r', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's')
  764. RAPIDJSON_STRING_(AdditionalProperties, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's')
  765. RAPIDJSON_STRING_(MinProperties, 'm', 'i', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's')
  766. RAPIDJSON_STRING_(MaxProperties, 'm', 'a', 'x', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's')
  767. RAPIDJSON_STRING_(Items, 'i', 't', 'e', 'm', 's')
  768. RAPIDJSON_STRING_(MinItems, 'm', 'i', 'n', 'I', 't', 'e', 'm', 's')
  769. RAPIDJSON_STRING_(MaxItems, 'm', 'a', 'x', 'I', 't', 'e', 'm', 's')
  770. RAPIDJSON_STRING_(AdditionalItems, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'I', 't', 'e', 'm', 's')
  771. RAPIDJSON_STRING_(UniqueItems, 'u', 'n', 'i', 'q', 'u', 'e', 'I', 't', 'e', 'm', 's')
  772. RAPIDJSON_STRING_(MinLength, 'm', 'i', 'n', 'L', 'e', 'n', 'g', 't', 'h')
  773. RAPIDJSON_STRING_(MaxLength, 'm', 'a', 'x', 'L', 'e', 'n', 'g', 't', 'h')
  774. RAPIDJSON_STRING_(Pattern, 'p', 'a', 't', 't', 'e', 'r', 'n')
  775. RAPIDJSON_STRING_(Minimum, 'm', 'i', 'n', 'i', 'm', 'u', 'm')
  776. RAPIDJSON_STRING_(Maximum, 'm', 'a', 'x', 'i', 'm', 'u', 'm')
  777. RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm')
  778. RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm')
  779. RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f')
  780. #undef RAPIDJSON_STRING_
  781. private:
  782. enum SchemaValueType {
  783. kNullSchemaType,
  784. kBooleanSchemaType,
  785. kObjectSchemaType,
  786. kArraySchemaType,
  787. kStringSchemaType,
  788. kNumberSchemaType,
  789. kIntegerSchemaType,
  790. kTotalSchemaType
  791. };
  792. #if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
  793. typedef internal::GenericRegex<EncodingType> RegexType;
  794. #elif RAPIDJSON_SCHEMA_USE_STDREGEX
  795. typedef std::basic_regex<Ch> RegexType;
  796. #else
  797. typedef char RegexType;
  798. #endif
  799. struct SchemaArray {
  800. SchemaArray() : schemas(), count() {}
  801. ~SchemaArray() { AllocatorType::Free(schemas); }
  802. const SchemaType** schemas;
  803. SizeType begin; // begin index of context.validators
  804. SizeType count;
  805. };
  806. static const SchemaType* GetTypeless() {
  807. static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0);
  808. return &typeless;
  809. }
  810. template <typename V1, typename V2>
  811. void AddUniqueElement(V1& a, const V2& v) {
  812. for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
  813. if (*itr == v)
  814. return;
  815. V1 c(v, *allocator_);
  816. a.PushBack(c, *allocator_);
  817. }
  818. static const ValueType* GetMember(const ValueType& value, const ValueType& name) {
  819. typename ValueType::ConstMemberIterator itr = value.FindMember(name);
  820. return itr != value.MemberEnd() ? &(itr->value) : 0;
  821. }
  822. static void AssignIfExist(bool& out, const ValueType& value, const ValueType& name) {
  823. if (const ValueType* v = GetMember(value, name))
  824. if (v->IsBool())
  825. out = v->GetBool();
  826. }
  827. static void AssignIfExist(SizeType& out, const ValueType& value, const ValueType& name) {
  828. if (const ValueType* v = GetMember(value, name))
  829. if (v->IsUint64() && v->GetUint64() <= SizeType(~0))
  830. out = static_cast<SizeType>(v->GetUint64());
  831. }
  832. void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) {
  833. if (const ValueType* v = GetMember(value, name)) {
  834. if (v->IsArray() && v->Size() > 0) {
  835. PointerType q = p.Append(name, allocator_);
  836. out.count = v->Size();
  837. out.schemas = static_cast<const Schema**>(allocator_->Malloc(out.count * sizeof(const Schema*)));
  838. memset(out.schemas, 0, sizeof(Schema*)* out.count);
  839. for (SizeType i = 0; i < out.count; i++)
  840. schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document);
  841. out.begin = validatorCount_;
  842. validatorCount_ += out.count;
  843. }
  844. }
  845. }
  846. #if RAPIDJSON_SCHEMA_USE_INTERNALREGEX
  847. template <typename ValueType>
  848. RegexType* CreatePattern(const ValueType& value) {
  849. if (value.IsString()) {
  850. RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString());
  851. if (!r->IsValid()) {
  852. r->~RegexType();
  853. AllocatorType::Free(r);
  854. r = 0;
  855. }
  856. return r;
  857. }
  858. return 0;
  859. }
  860. static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) {
  861. return pattern->Search(str);
  862. }
  863. #elif RAPIDJSON_SCHEMA_USE_STDREGEX
  864. template <typename ValueType>
  865. RegexType* CreatePattern(const ValueType& value) {
  866. if (value.IsString())
  867. try {
  868. return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript);
  869. }
  870. catch (const std::regex_error&) {
  871. }
  872. return 0;
  873. }
  874. static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) {
  875. std::match_results<const Ch*> r;
  876. return std::regex_search(str, str + length, r, *pattern);
  877. }
  878. #else
  879. template <typename ValueType>
  880. RegexType* CreatePattern(const ValueType&) { return 0; }
  881. static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; }
  882. #endif // RAPIDJSON_SCHEMA_USE_STDREGEX
  883. void AddType(const ValueType& type) {
  884. if (type == GetNullString() ) type_ |= 1 << kNullSchemaType;
  885. else if (type == GetBooleanString()) type_ |= 1 << kBooleanSchemaType;
  886. else if (type == GetObjectString() ) type_ |= 1 << kObjectSchemaType;
  887. else if (type == GetArrayString() ) type_ |= 1 << kArraySchemaType;
  888. else if (type == GetStringString() ) type_ |= 1 << kStringSchemaType;
  889. else if (type == GetIntegerString()) type_ |= 1 << kIntegerSchemaType;
  890. else if (type == GetNumberString() ) type_ |= (1 << kNumberSchemaType) | (1 << kIntegerSchemaType);
  891. }
  892. bool CreateParallelValidator(Context& context) const {
  893. if (enum_ || context.arrayUniqueness)
  894. context.hasher = context.factory.CreateHasher();
  895. if (validatorCount_) {
  896. RAPIDJSON_ASSERT(context.validators == 0);
  897. context.validators = static_cast<ISchemaValidator**>(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_));
  898. context.validatorCount = validatorCount_;
  899. if (allOf_.schemas)
  900. CreateSchemaValidators(context, allOf_);
  901. if (anyOf_.schemas)
  902. CreateSchemaValidators(context, anyOf_);
  903. if (oneOf_.schemas)
  904. CreateSchemaValidators(context, oneOf_);
  905. if (not_)
  906. context.validators[notValidatorIndex_] = context.factory.CreateSchemaValidator(*not_);
  907. if (hasSchemaDependencies_) {
  908. for (SizeType i = 0; i < propertyCount_; i++)
  909. if (properties_[i].dependenciesSchema)
  910. context.validators[properties_[i].dependenciesValidatorIndex] = context.factory.CreateSchemaValidator(*properties_[i].dependenciesSchema);
  911. }
  912. }
  913. return true;
  914. }
  915. void CreateSchemaValidators(Context& context, const SchemaArray& schemas) const {
  916. for (SizeType i = 0; i < schemas.count; i++)
  917. context.validators[schemas.begin + i] = context.factory.CreateSchemaValidator(*schemas.schemas[i]);
  918. }
  919. // O(n)
  920. bool FindPropertyIndex(const ValueType& name, SizeType* outIndex) const {
  921. SizeType len = name.GetStringLength();
  922. const Ch* str = name.GetString();
  923. for (SizeType index = 0; index < propertyCount_; index++)
  924. if (properties_[index].name.GetStringLength() == len &&
  925. (std::memcmp(properties_[index].name.GetString(), str, sizeof(Ch) * len) == 0))
  926. {
  927. *outIndex = index;
  928. return true;
  929. }
  930. return false;
  931. }
  932. bool CheckInt(Context& context, int64_t i) const {
  933. if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType))))
  934. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  935. if (!minimum_.IsNull()) {
  936. if (minimum_.IsInt64()) {
  937. if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64())
  938. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString());
  939. }
  940. else if (minimum_.IsUint64()) {
  941. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64()
  942. }
  943. else if (!CheckDoubleMinimum(context, static_cast<double>(i)))
  944. return false;
  945. }
  946. if (!maximum_.IsNull()) {
  947. if (maximum_.IsInt64()) {
  948. if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64())
  949. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString());
  950. }
  951. else if (maximum_.IsUint64())
  952. /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64()
  953. else if (!CheckDoubleMaximum(context, static_cast<double>(i)))
  954. return false;
  955. }
  956. if (!multipleOf_.IsNull()) {
  957. if (multipleOf_.IsUint64()) {
  958. if (static_cast<uint64_t>(i >= 0 ? i : -i) % multipleOf_.GetUint64() != 0)
  959. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString());
  960. }
  961. else if (!CheckDoubleMultipleOf(context, static_cast<double>(i)))
  962. return false;
  963. }
  964. return true;
  965. }
  966. bool CheckUint(Context& context, uint64_t i) const {
  967. if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType))))
  968. RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString());
  969. if (!minimum_.IsNull()) {
  970. if (minimum_.IsUint64()) {
  971. if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64())
  972. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString());
  973. }
  974. else if (minimum_.IsInt64())
  975. /* do nothing */; // i >= 0 > minimum.Getint64()
  976. else if (!CheckDoubleMinimum(context, static_cast<double>(i)))
  977. return false;
  978. }
  979. if (!maximum_.IsNull()) {
  980. if (maximum_.IsUint64()) {
  981. if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64())
  982. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString());
  983. }
  984. else if (maximum_.IsInt64())
  985. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_
  986. else if (!CheckDoubleMaximum(context, static_cast<double>(i)))
  987. return false;
  988. }
  989. if (!multipleOf_.IsNull()) {
  990. if (multipleOf_.IsUint64()) {
  991. if (i % multipleOf_.GetUint64() != 0)
  992. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString());
  993. }
  994. else if (!CheckDoubleMultipleOf(context, static_cast<double>(i)))
  995. return false;
  996. }
  997. return true;
  998. }
  999. bool CheckDoubleMinimum(Context& context, double d) const {
  1000. if (exclusiveMinimum_ ? d <= minimum_.GetDouble() : d < minimum_.GetDouble())
  1001. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString());
  1002. return true;
  1003. }
  1004. bool CheckDoubleMaximum(Context& context, double d) const {
  1005. if (exclusiveMaximum_ ? d >= maximum_.GetDouble() : d > maximum_.GetDouble())
  1006. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString());
  1007. return true;
  1008. }
  1009. bool CheckDoubleMultipleOf(Context& context, double d) const {
  1010. double a = std::abs(d), b = std::abs(multipleOf_.GetDouble());
  1011. double q = std::floor(a / b);
  1012. double r = a - q * b;
  1013. if (r > 0.0)
  1014. RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString());
  1015. return true;
  1016. }
  1017. struct Property {
  1018. Property() : schema(), dependenciesSchema(), dependenciesValidatorIndex(), dependencies(), required(false) {}
  1019. ~Property() { AllocatorType::Free(dependencies); }
  1020. SValue name;
  1021. const SchemaType* schema;
  1022. const SchemaType* dependenciesSchema;
  1023. SizeType dependenciesValidatorIndex;
  1024. bool* dependencies;
  1025. bool required;
  1026. };
  1027. struct PatternProperty {
  1028. PatternProperty() : schema(), pattern() {}
  1029. ~PatternProperty() {
  1030. if (pattern) {
  1031. pattern->~RegexType();
  1032. AllocatorType::Free(pattern);
  1033. }
  1034. }
  1035. const SchemaType* schema;
  1036. RegexType* pattern;
  1037. };
  1038. AllocatorType* allocator_;
  1039. uint64_t* enum_;
  1040. SizeType enumCount_;
  1041. SchemaArray allOf_;
  1042. SchemaArray anyOf_;
  1043. SchemaArray oneOf_;
  1044. const SchemaType* not_;
  1045. unsigned type_; // bitmask of kSchemaType
  1046. SizeType validatorCount_;
  1047. SizeType notValidatorIndex_;
  1048. Property* properties_;
  1049. const SchemaType* additionalPropertiesSchema_;
  1050. PatternProperty* patternProperties_;
  1051. SizeType patternPropertyCount_;
  1052. SizeType propertyCount_;
  1053. SizeType minProperties_;
  1054. SizeType maxProperties_;
  1055. bool additionalProperties_;
  1056. bool hasDependencies_;
  1057. bool hasRequired_;
  1058. bool hasSchemaDependencies_;
  1059. const SchemaType* additionalItemsSchema_;
  1060. const SchemaType* itemsList_;
  1061. const SchemaType** itemsTuple_;
  1062. SizeType itemsTupleCount_;
  1063. SizeType minItems_;
  1064. SizeType maxItems_;
  1065. bool additionalItems_;
  1066. bool uniqueItems_;
  1067. RegexType* pattern_;
  1068. SizeType minLength_;
  1069. SizeType maxLength_;
  1070. SValue minimum_;
  1071. SValue maximum_;
  1072. SValue multipleOf_;
  1073. bool exclusiveMinimum_;
  1074. bool exclusiveMaximum_;
  1075. };
  1076. template<typename Stack, typename Ch>
  1077. struct TokenHelper {
  1078. RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) {
  1079. *documentStack.template Push<Ch>() = '/';
  1080. char buffer[21];
  1081. size_t length = static_cast<size_t>((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer);
  1082. for (size_t i = 0; i < length; i++)
  1083. *documentStack.template Push<Ch>() = buffer[i];
  1084. }
  1085. };
  1086. // Partial specialized version for char to prevent buffer copying.
  1087. template <typename Stack>
  1088. struct TokenHelper<Stack, char> {
  1089. RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) {
  1090. if (sizeof(SizeType) == 4) {
  1091. char *buffer = documentStack.template Push<char>(1 + 10); // '/' + uint
  1092. *buffer++ = '/';
  1093. const char* end = internal::u32toa(index, buffer);
  1094. documentStack.template Pop<char>(static_cast<size_t>(10 - (end - buffer)));
  1095. }
  1096. else {
  1097. char *buffer = documentStack.template Push<char>(1 + 20); // '/' + uint64
  1098. *buffer++ = '/';
  1099. const char* end = internal::u64toa(index, buffer);
  1100. documentStack.template Pop<char>(static_cast<size_t>(20 - (end - buffer)));
  1101. }
  1102. }
  1103. };
  1104. } // namespace internal
  1105. ///////////////////////////////////////////////////////////////////////////////
  1106. // IGenericRemoteSchemaDocumentProvider
  1107. template <typename SchemaDocumentType>
  1108. class IGenericRemoteSchemaDocumentProvider {
  1109. public:
  1110. typedef typename SchemaDocumentType::Ch Ch;
  1111. virtual ~IGenericRemoteSchemaDocumentProvider() {}
  1112. virtual const SchemaDocumentType* GetRemoteDocument(const Ch* uri, SizeType length) = 0;
  1113. };
  1114. ///////////////////////////////////////////////////////////////////////////////
  1115. // GenericSchemaDocument
  1116. //! JSON schema document.
  1117. /*!
  1118. A JSON schema document is a compiled version of a JSON schema.
  1119. It is basically a tree of internal::Schema.
  1120. \note This is an immutable class (i.e. its instance cannot be modified after construction).
  1121. \tparam ValueT Type of JSON value (e.g. \c Value ), which also determine the encoding.
  1122. \tparam Allocator Allocator type for allocating memory of this document.
  1123. */
  1124. template <typename ValueT, typename Allocator = CrtAllocator>
  1125. class GenericSchemaDocument {
  1126. public:
  1127. typedef ValueT ValueType;
  1128. typedef IGenericRemoteSchemaDocumentProvider<GenericSchemaDocument> IRemoteSchemaDocumentProviderType;
  1129. typedef Allocator AllocatorType;
  1130. typedef typename ValueType::EncodingType EncodingType;
  1131. typedef typename EncodingType::Ch Ch;
  1132. typedef internal::Schema<GenericSchemaDocument> SchemaType;
  1133. typedef GenericPointer<ValueType, Allocator> PointerType;
  1134. friend class internal::Schema<GenericSchemaDocument>;
  1135. template <typename, typename, typename>
  1136. friend class GenericSchemaValidator;
  1137. //! Constructor.
  1138. /*!
  1139. Compile a JSON document into schema document.
  1140. \param document A JSON document as source.
  1141. \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null.
  1142. \param allocator An optional allocator instance for allocating memory. Can be null.
  1143. */
  1144. explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
  1145. remoteProvider_(remoteProvider),
  1146. allocator_(allocator),
  1147. ownAllocator_(),
  1148. root_(),
  1149. schemaMap_(allocator, kInitialSchemaMapSize),
  1150. schemaRef_(allocator, kInitialSchemaRefSize)
  1151. {
  1152. if (!allocator_)
  1153. ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
  1154. // Generate root schema, it will call CreateSchema() to create sub-schemas,
  1155. // And call AddRefSchema() if there are $ref.
  1156. CreateSchemaRecursive(&root_, PointerType(), document, document);
  1157. // Resolve $ref
  1158. while (!schemaRef_.Empty()) {
  1159. SchemaRefEntry* refEntry = schemaRef_.template Pop<SchemaRefEntry>(1);
  1160. if (const SchemaType* s = GetSchema(refEntry->target)) {
  1161. if (refEntry->schema)
  1162. *refEntry->schema = s;
  1163. // Create entry in map if not exist
  1164. if (!GetSchema(refEntry->source)) {
  1165. new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(refEntry->source, const_cast<SchemaType*>(s), false, allocator_);
  1166. }
  1167. }
  1168. refEntry->~SchemaRefEntry();
  1169. }
  1170. RAPIDJSON_ASSERT(root_ != 0);
  1171. schemaRef_.ShrinkToFit(); // Deallocate all memory for ref
  1172. }
  1173. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  1174. //! Move constructor in C++11
  1175. GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT :
  1176. remoteProvider_(rhs.remoteProvider_),
  1177. allocator_(rhs.allocator_),
  1178. ownAllocator_(rhs.ownAllocator_),
  1179. root_(rhs.root_),
  1180. schemaMap_(std::move(rhs.schemaMap_)),
  1181. schemaRef_(std::move(rhs.schemaRef_))
  1182. {
  1183. rhs.remoteProvider_ = 0;
  1184. rhs.allocator_ = 0;
  1185. rhs.ownAllocator_ = 0;
  1186. }
  1187. #endif
  1188. //! Destructor
  1189. ~GenericSchemaDocument() {
  1190. while (!schemaMap_.Empty())
  1191. schemaMap_.template Pop<SchemaEntry>(1)->~SchemaEntry();
  1192. RAPIDJSON_DELETE(ownAllocator_);
  1193. }
  1194. //! Get the root schema.
  1195. const SchemaType& GetRoot() const { return *root_; }
  1196. private:
  1197. //! Prohibit copying
  1198. GenericSchemaDocument(const GenericSchemaDocument&);
  1199. //! Prohibit assignment
  1200. GenericSchemaDocument& operator=(const GenericSchemaDocument&);
  1201. struct SchemaRefEntry {
  1202. SchemaRefEntry(const PointerType& s, const PointerType& t, const SchemaType** outSchema, Allocator *allocator) : source(s, allocator), target(t, allocator), schema(outSchema) {}
  1203. PointerType source;
  1204. PointerType target;
  1205. const SchemaType** schema;
  1206. };
  1207. struct SchemaEntry {
  1208. SchemaEntry(const PointerType& p, SchemaType* s, bool o, Allocator* allocator) : pointer(p, allocator), schema(s), owned(o) {}
  1209. ~SchemaEntry() {
  1210. if (owned) {
  1211. schema->~SchemaType();
  1212. Allocator::Free(schema);
  1213. }
  1214. }
  1215. PointerType pointer;
  1216. SchemaType* schema;
  1217. bool owned;
  1218. };
  1219. void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) {
  1220. if (schema)
  1221. *schema = SchemaType::GetTypeless();
  1222. if (v.GetType() == kObjectType) {
  1223. const SchemaType* s = GetSchema(pointer);
  1224. if (!s)
  1225. CreateSchema(schema, pointer, v, document);
  1226. for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
  1227. CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document);
  1228. }
  1229. else if (v.GetType() == kArrayType)
  1230. for (SizeType i = 0; i < v.Size(); i++)
  1231. CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document);
  1232. }
  1233. void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) {
  1234. RAPIDJSON_ASSERT(pointer.IsValid());
  1235. if (v.IsObject()) {
  1236. if (!HandleRefSchema(pointer, schema, v, document)) {
  1237. SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_);
  1238. new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true, allocator_);
  1239. if (schema)
  1240. *schema = s;
  1241. }
  1242. }
  1243. }
  1244. bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document) {
  1245. static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' };
  1246. static const ValueType kRefValue(kRefString, 4);
  1247. typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue);
  1248. if (itr == v.MemberEnd())
  1249. return false;
  1250. if (itr->value.IsString()) {
  1251. SizeType len = itr->value.GetStringLength();
  1252. if (len > 0) {
  1253. const Ch* s = itr->value.GetString();
  1254. SizeType i = 0;
  1255. while (i < len && s[i] != '#') // Find the first #
  1256. i++;
  1257. if (i > 0) { // Remote reference, resolve immediately
  1258. if (remoteProvider_) {
  1259. if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) {
  1260. PointerType pointer(&s[i], len - i, allocator_);
  1261. if (pointer.IsValid()) {
  1262. if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) {
  1263. if (schema)
  1264. *schema = sc;
  1265. return true;
  1266. }
  1267. }
  1268. }
  1269. }
  1270. }
  1271. else if (s[i] == '#') { // Local reference, defer resolution
  1272. PointerType pointer(&s[i], len - i, allocator_);
  1273. if (pointer.IsValid()) {
  1274. if (const ValueType* nv = pointer.Get(document))
  1275. if (HandleRefSchema(source, schema, *nv, document))
  1276. return true;
  1277. new (schemaRef_.template Push<SchemaRefEntry>()) SchemaRefEntry(source, pointer, schema, allocator_);
  1278. return true;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. return false;
  1284. }
  1285. const SchemaType* GetSchema(const PointerType& pointer) const {
  1286. for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
  1287. if (pointer == target->pointer)
  1288. return target->schema;
  1289. return 0;
  1290. }
  1291. PointerType GetPointer(const SchemaType* schema) const {
  1292. for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
  1293. if (schema == target->schema)
  1294. return target->pointer;
  1295. return PointerType();
  1296. }
  1297. static const size_t kInitialSchemaMapSize = 64;
  1298. static const size_t kInitialSchemaRefSize = 64;
  1299. IRemoteSchemaDocumentProviderType* remoteProvider_;
  1300. Allocator *allocator_;
  1301. Allocator *ownAllocator_;
  1302. const SchemaType* root_; //!< Root schema.
  1303. internal::Stack<Allocator> schemaMap_; // Stores created Pointer -> Schemas
  1304. internal::Stack<Allocator> schemaRef_; // Stores Pointer from $ref and schema which holds the $ref
  1305. };
  1306. //! GenericSchemaDocument using Value type.
  1307. typedef GenericSchemaDocument<Value> SchemaDocument;
  1308. //! IGenericRemoteSchemaDocumentProvider using SchemaDocument.
  1309. typedef IGenericRemoteSchemaDocumentProvider<SchemaDocument> IRemoteSchemaDocumentProvider;
  1310. ///////////////////////////////////////////////////////////////////////////////
  1311. // GenericSchemaValidator
  1312. //! JSON Schema Validator.
  1313. /*!
  1314. A SAX style JSON schema validator.
  1315. It uses a \c GenericSchemaDocument to validate SAX events.
  1316. It delegates the incoming SAX events to an output handler.
  1317. The default output handler does nothing.
  1318. It can be reused multiple times by calling \c Reset().
  1319. \tparam SchemaDocumentType Type of schema document.
  1320. \tparam OutputHandler Type of output handler. Default handler does nothing.
  1321. \tparam StateAllocator Allocator for storing the internal validation states.
  1322. */
  1323. template <
  1324. typename SchemaDocumentType,
  1325. typename OutputHandler = BaseReaderHandler<typename SchemaDocumentType::SchemaType::EncodingType>,
  1326. typename StateAllocator = CrtAllocator>
  1327. class GenericSchemaValidator :
  1328. public internal::ISchemaStateFactory<typename SchemaDocumentType::SchemaType>,
  1329. public internal::ISchemaValidator
  1330. {
  1331. public:
  1332. typedef typename SchemaDocumentType::SchemaType SchemaType;
  1333. typedef typename SchemaDocumentType::PointerType PointerType;
  1334. typedef typename SchemaType::EncodingType EncodingType;
  1335. typedef typename EncodingType::Ch Ch;
  1336. //! Constructor without output handler.
  1337. /*!
  1338. \param schemaDocument The schema document to conform to.
  1339. \param allocator Optional allocator for storing internal validation states.
  1340. \param schemaStackCapacity Optional initial capacity of schema path stack.
  1341. \param documentStackCapacity Optional initial capacity of document path stack.
  1342. */
  1343. GenericSchemaValidator(
  1344. const SchemaDocumentType& schemaDocument,
  1345. StateAllocator* allocator = 0,
  1346. size_t schemaStackCapacity = kDefaultSchemaStackCapacity,
  1347. size_t documentStackCapacity = kDefaultDocumentStackCapacity)
  1348. :
  1349. schemaDocument_(&schemaDocument),
  1350. root_(schemaDocument.GetRoot()),
  1351. outputHandler_(GetNullHandler()),
  1352. stateAllocator_(allocator),
  1353. ownStateAllocator_(0),
  1354. schemaStack_(allocator, schemaStackCapacity),
  1355. documentStack_(allocator, documentStackCapacity),
  1356. valid_(true)
  1357. #if RAPIDJSON_SCHEMA_VERBOSE
  1358. , depth_(0)
  1359. #endif
  1360. {
  1361. }
  1362. //! Constructor with output handler.
  1363. /*!
  1364. \param schemaDocument The schema document to conform to.
  1365. \param allocator Optional allocator for storing internal validation states.
  1366. \param schemaStackCapacity Optional initial capacity of schema path stack.
  1367. \param documentStackCapacity Optional initial capacity of document path stack.
  1368. */
  1369. GenericSchemaValidator(
  1370. const SchemaDocumentType& schemaDocument,
  1371. OutputHandler& outputHandler,
  1372. StateAllocator* allocator = 0,
  1373. size_t schemaStackCapacity = kDefaultSchemaStackCapacity,
  1374. size_t documentStackCapacity = kDefaultDocumentStackCapacity)
  1375. :
  1376. schemaDocument_(&schemaDocument),
  1377. root_(schemaDocument.GetRoot()),
  1378. outputHandler_(outputHandler),
  1379. stateAllocator_(allocator),
  1380. ownStateAllocator_(0),
  1381. schemaStack_(allocator, schemaStackCapacity),
  1382. documentStack_(allocator, documentStackCapacity),
  1383. valid_(true)
  1384. #if RAPIDJSON_SCHEMA_VERBOSE
  1385. , depth_(0)
  1386. #endif
  1387. {
  1388. }
  1389. //! Destructor.
  1390. ~GenericSchemaValidator() {
  1391. Reset();
  1392. RAPIDJSON_DELETE(ownStateAllocator_);
  1393. }
  1394. //! Reset the internal states.
  1395. void Reset() {
  1396. while (!schemaStack_.Empty())
  1397. PopSchema();
  1398. documentStack_.Clear();
  1399. valid_ = true;
  1400. }
  1401. //! Checks whether the current state is valid.
  1402. // Implementation of ISchemaValidator
  1403. virtual bool IsValid() const { return valid_; }
  1404. //! Gets the JSON pointer pointed to the invalid schema.
  1405. PointerType GetInvalidSchemaPointer() const {
  1406. return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema());
  1407. }
  1408. //! Gets the keyword of invalid schema.
  1409. const Ch* GetInvalidSchemaKeyword() const {
  1410. return schemaStack_.Empty() ? 0 : CurrentContext().invalidKeyword;
  1411. }
  1412. //! Gets the JSON pointer pointed to the invalid value.
  1413. PointerType GetInvalidDocumentPointer() const {
  1414. return documentStack_.Empty() ? PointerType() : PointerType(documentStack_.template Bottom<Ch>(), documentStack_.GetSize() / sizeof(Ch));
  1415. }
  1416. #if RAPIDJSON_SCHEMA_VERBOSE
  1417. #define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() \
  1418. RAPIDJSON_MULTILINEMACRO_BEGIN\
  1419. *documentStack_.template Push<Ch>() = '\0';\
  1420. documentStack_.template Pop<Ch>(1);\
  1421. internal::PrintInvalidDocument(documentStack_.template Bottom<Ch>());\
  1422. RAPIDJSON_MULTILINEMACRO_END
  1423. #else
  1424. #define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_()
  1425. #endif
  1426. #define RAPIDJSON_SCHEMA_HANDLE_BEGIN_(method, arg1)\
  1427. if (!valid_) return false; \
  1428. if (!BeginValue() || !CurrentSchema().method arg1) {\
  1429. RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_();\
  1430. return valid_ = false;\
  1431. }
  1432. #define RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2)\
  1433. for (Context* context = schemaStack_.template Bottom<Context>(); context != schemaStack_.template End<Context>(); context++) {\
  1434. if (context->hasher)\
  1435. static_cast<HasherType*>(context->hasher)->method arg2;\
  1436. if (context->validators)\
  1437. for (SizeType i_ = 0; i_ < context->validatorCount; i_++)\
  1438. static_cast<GenericSchemaValidator*>(context->validators[i_])->method arg2;\
  1439. if (context->patternPropertiesValidators)\
  1440. for (SizeType i_ = 0; i_ < context->patternPropertiesValidatorCount; i_++)\
  1441. static_cast<GenericSchemaValidator*>(context->patternPropertiesValidators[i_])->method arg2;\
  1442. }
  1443. #define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\
  1444. return valid_ = EndValue() && outputHandler_.method arg2
  1445. #define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \
  1446. RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\
  1447. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2);\
  1448. RAPIDJSON_SCHEMA_HANDLE_END_ (method, arg2)
  1449. bool Null() { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Null, (CurrentContext() ), ( )); }
  1450. bool Bool(bool b) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Bool, (CurrentContext(), b), (b)); }
  1451. bool Int(int i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int, (CurrentContext(), i), (i)); }
  1452. bool Uint(unsigned u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint, (CurrentContext(), u), (u)); }
  1453. bool Int64(int64_t i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int64, (CurrentContext(), i), (i)); }
  1454. bool Uint64(uint64_t u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint64, (CurrentContext(), u), (u)); }
  1455. bool Double(double d) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Double, (CurrentContext(), d), (d)); }
  1456. bool RawNumber(const Ch* str, SizeType length, bool copy)
  1457. { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); }
  1458. bool String(const Ch* str, SizeType length, bool copy)
  1459. { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); }
  1460. bool StartObject() {
  1461. RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext()));
  1462. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ());
  1463. return valid_ = outputHandler_.StartObject();
  1464. }
  1465. bool Key(const Ch* str, SizeType len, bool copy) {
  1466. if (!valid_) return false;
  1467. AppendToken(str, len);
  1468. if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false;
  1469. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy));
  1470. return valid_ = outputHandler_.Key(str, len, copy);
  1471. }
  1472. bool EndObject(SizeType memberCount) {
  1473. if (!valid_) return false;
  1474. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndObject, (memberCount));
  1475. if (!CurrentSchema().EndObject(CurrentContext(), memberCount)) return valid_ = false;
  1476. RAPIDJSON_SCHEMA_HANDLE_END_(EndObject, (memberCount));
  1477. }
  1478. bool StartArray() {
  1479. RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext()));
  1480. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ());
  1481. return valid_ = outputHandler_.StartArray();
  1482. }
  1483. bool EndArray(SizeType elementCount) {
  1484. if (!valid_) return false;
  1485. RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndArray, (elementCount));
  1486. if (!CurrentSchema().EndArray(CurrentContext(), elementCount)) return valid_ = false;
  1487. RAPIDJSON_SCHEMA_HANDLE_END_(EndArray, (elementCount));
  1488. }
  1489. #undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_
  1490. #undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_
  1491. #undef RAPIDJSON_SCHEMA_HANDLE_PARALLEL_
  1492. #undef RAPIDJSON_SCHEMA_HANDLE_VALUE_
  1493. // Implementation of ISchemaStateFactory<SchemaType>
  1494. virtual ISchemaValidator* CreateSchemaValidator(const SchemaType& root) {
  1495. return new (GetStateAllocator().Malloc(sizeof(GenericSchemaValidator))) GenericSchemaValidator(*schemaDocument_, root,
  1496. #if RAPIDJSON_SCHEMA_VERBOSE
  1497. depth_ + 1,
  1498. #endif
  1499. &GetStateAllocator());
  1500. }
  1501. virtual void DestroySchemaValidator(ISchemaValidator* validator) {
  1502. GenericSchemaValidator* v = static_cast<GenericSchemaValidator*>(validator);
  1503. v->~GenericSchemaValidator();
  1504. StateAllocator::Free(v);
  1505. }
  1506. virtual void* CreateHasher() {
  1507. return new (GetStateAllocator().Malloc(sizeof(HasherType))) HasherType(&GetStateAllocator());
  1508. }
  1509. virtual uint64_t GetHashCode(void* hasher) {
  1510. return static_cast<HasherType*>(hasher)->GetHashCode();
  1511. }
  1512. virtual void DestroryHasher(void* hasher) {
  1513. HasherType* h = static_cast<HasherType*>(hasher);
  1514. h->~HasherType();
  1515. StateAllocator::Free(h);
  1516. }
  1517. virtual void* MallocState(size_t size) {
  1518. return GetStateAllocator().Malloc(size);
  1519. }
  1520. virtual void FreeState(void* p) {
  1521. return StateAllocator::Free(p);
  1522. }
  1523. private:
  1524. typedef typename SchemaType::Context Context;
  1525. typedef GenericValue<UTF8<>, StateAllocator> HashCodeArray;
  1526. typedef internal::Hasher<EncodingType, StateAllocator> HasherType;
  1527. GenericSchemaValidator(
  1528. const SchemaDocumentType& schemaDocument,
  1529. const SchemaType& root,
  1530. #if RAPIDJSON_SCHEMA_VERBOSE
  1531. unsigned depth,
  1532. #endif
  1533. StateAllocator* allocator = 0,
  1534. size_t schemaStackCapacity = kDefaultSchemaStackCapacity,
  1535. size_t documentStackCapacity = kDefaultDocumentStackCapacity)
  1536. :
  1537. schemaDocument_(&schemaDocument),
  1538. root_(root),
  1539. outputHandler_(GetNullHandler()),
  1540. stateAllocator_(allocator),
  1541. ownStateAllocator_(0),
  1542. schemaStack_(allocator, schemaStackCapacity),
  1543. documentStack_(allocator, documentStackCapacity),
  1544. valid_(true)
  1545. #if RAPIDJSON_SCHEMA_VERBOSE
  1546. , depth_(depth)
  1547. #endif
  1548. {
  1549. }
  1550. StateAllocator& GetStateAllocator() {
  1551. if (!stateAllocator_)
  1552. stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator());
  1553. return *stateAllocator_;
  1554. }
  1555. bool BeginValue() {
  1556. if (schemaStack_.Empty())
  1557. PushSchema(root_);
  1558. else {
  1559. if (CurrentContext().inArray)
  1560. internal::TokenHelper<internal::Stack<StateAllocator>, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex);
  1561. if (!CurrentSchema().BeginValue(CurrentContext()))
  1562. return false;
  1563. SizeType count = CurrentContext().patternPropertiesSchemaCount;
  1564. const SchemaType** sa = CurrentContext().patternPropertiesSchemas;
  1565. typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType;
  1566. bool valueUniqueness = CurrentContext().valueUniqueness;
  1567. if (CurrentContext().valueSchema)
  1568. PushSchema(*CurrentContext().valueSchema);
  1569. if (count > 0) {
  1570. CurrentContext().objectPatternValidatorType = patternValidatorType;
  1571. ISchemaValidator**& va = CurrentContext().patternPropertiesValidators;
  1572. SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount;
  1573. va = static_cast<ISchemaValidator**>(MallocState(sizeof(ISchemaValidator*) * count));
  1574. for (SizeType i = 0; i < count; i++)
  1575. va[validatorCount++] = CreateSchemaValidator(*sa[i]);
  1576. }
  1577. CurrentContext().arrayUniqueness = valueUniqueness;
  1578. }
  1579. return true;
  1580. }
  1581. bool EndValue() {
  1582. if (!CurrentSchema().EndValue(CurrentContext()))
  1583. return false;
  1584. #if RAPIDJSON_SCHEMA_VERBOSE
  1585. GenericStringBuffer<EncodingType> sb;
  1586. schemaDocument_->GetPointer(&CurrentSchema()).Stringify(sb);
  1587. *documentStack_.template Push<Ch>() = '\0';
  1588. documentStack_.template Pop<Ch>(1);
  1589. internal::PrintValidatorPointers(depth_, sb.GetString(), documentStack_.template Bottom<Ch>());
  1590. #endif
  1591. uint64_t h = CurrentContext().arrayUniqueness ? static_cast<HasherType*>(CurrentContext().hasher)->GetHashCode() : 0;
  1592. PopSchema();
  1593. if (!schemaStack_.Empty()) {
  1594. Context& context = CurrentContext();
  1595. if (context.valueUniqueness) {
  1596. HashCodeArray* a = static_cast<HashCodeArray*>(context.arrayElementHashCodes);
  1597. if (!a)
  1598. CurrentContext().arrayElementHashCodes = a = new (GetStateAllocator().Malloc(sizeof(HashCodeArray))) HashCodeArray(kArrayType);
  1599. for (typename HashCodeArray::ConstValueIterator itr = a->Begin(); itr != a->End(); ++itr)
  1600. if (itr->GetUint64() == h)
  1601. RAPIDJSON_INVALID_KEYWORD_RETURN(SchemaType::GetUniqueItemsString());
  1602. a->PushBack(h, GetStateAllocator());
  1603. }
  1604. }
  1605. // Remove the last token of document pointer
  1606. while (!documentStack_.Empty() && *documentStack_.template Pop<Ch>(1) != '/')
  1607. ;
  1608. return true;
  1609. }
  1610. void AppendToken(const Ch* str, SizeType len) {
  1611. documentStack_.template Reserve<Ch>(1 + len * 2); // worst case all characters are escaped as two characters
  1612. *documentStack_.template PushUnsafe<Ch>() = '/';
  1613. for (SizeType i = 0; i < len; i++) {
  1614. if (str[i] == '~') {
  1615. *documentStack_.template PushUnsafe<Ch>() = '~';
  1616. *documentStack_.template PushUnsafe<Ch>() = '0';
  1617. }
  1618. else if (str[i] == '/') {
  1619. *documentStack_.template PushUnsafe<Ch>() = '~';
  1620. *documentStack_.template PushUnsafe<Ch>() = '1';
  1621. }
  1622. else
  1623. *documentStack_.template PushUnsafe<Ch>() = str[i];
  1624. }
  1625. }
  1626. RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push<Context>()) Context(*this, &schema); }
  1627. RAPIDJSON_FORCEINLINE void PopSchema() {
  1628. Context* c = schemaStack_.template Pop<Context>(1);
  1629. if (HashCodeArray* a = static_cast<HashCodeArray*>(c->arrayElementHashCodes)) {
  1630. a->~HashCodeArray();
  1631. StateAllocator::Free(a);
  1632. }
  1633. c->~Context();
  1634. }
  1635. const SchemaType& CurrentSchema() const { return *schemaStack_.template Top<Context>()->schema; }
  1636. Context& CurrentContext() { return *schemaStack_.template Top<Context>(); }
  1637. const Context& CurrentContext() const { return *schemaStack_.template Top<Context>(); }
  1638. static OutputHandler& GetNullHandler() {
  1639. static OutputHandler nullHandler;
  1640. return nullHandler;
  1641. }
  1642. static const size_t kDefaultSchemaStackCapacity = 1024;
  1643. static const size_t kDefaultDocumentStackCapacity = 256;
  1644. const SchemaDocumentType* schemaDocument_;
  1645. const SchemaType& root_;
  1646. OutputHandler& outputHandler_;
  1647. StateAllocator* stateAllocator_;
  1648. StateAllocator* ownStateAllocator_;
  1649. internal::Stack<StateAllocator> schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *)
  1650. internal::Stack<StateAllocator> documentStack_; //!< stack to store the current path of validating document (Ch)
  1651. bool valid_;
  1652. #if RAPIDJSON_SCHEMA_VERBOSE
  1653. unsigned depth_;
  1654. #endif
  1655. };
  1656. typedef GenericSchemaValidator<SchemaDocument> SchemaValidator;
  1657. ///////////////////////////////////////////////////////////////////////////////
  1658. // SchemaValidatingReader
  1659. //! A helper class for parsing with validation.
  1660. /*!
  1661. This helper class is a functor, designed as a parameter of \ref GenericDocument::Populate().
  1662. \tparam parseFlags Combination of \ref ParseFlag.
  1663. \tparam InputStream Type of input stream, implementing Stream concept.
  1664. \tparam SourceEncoding Encoding of the input stream.
  1665. \tparam SchemaDocumentType Type of schema document.
  1666. \tparam StackAllocator Allocator type for stack.
  1667. */
  1668. template <
  1669. unsigned parseFlags,
  1670. typename InputStream,
  1671. typename SourceEncoding,
  1672. typename SchemaDocumentType = SchemaDocument,
  1673. typename StackAllocator = CrtAllocator>
  1674. class SchemaValidatingReader {
  1675. public:
  1676. typedef typename SchemaDocumentType::PointerType PointerType;
  1677. typedef typename InputStream::Ch Ch;
  1678. //! Constructor
  1679. /*!
  1680. \param is Input stream.
  1681. \param sd Schema document.
  1682. */
  1683. SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), isValid_(true) {}
  1684. template <typename Handler>
  1685. bool operator()(Handler& handler) {
  1686. GenericReader<SourceEncoding, typename SchemaDocumentType::EncodingType, StackAllocator> reader;
  1687. GenericSchemaValidator<SchemaDocumentType, Handler> validator(sd_, handler);
  1688. parseResult_ = reader.template Parse<parseFlags>(is_, validator);
  1689. isValid_ = validator.IsValid();
  1690. if (isValid_) {
  1691. invalidSchemaPointer_ = PointerType();
  1692. invalidSchemaKeyword_ = 0;
  1693. invalidDocumentPointer_ = PointerType();
  1694. }
  1695. else {
  1696. invalidSchemaPointer_ = validator.GetInvalidSchemaPointer();
  1697. invalidSchemaKeyword_ = validator.GetInvalidSchemaKeyword();
  1698. invalidDocumentPointer_ = validator.GetInvalidDocumentPointer();
  1699. }
  1700. return parseResult_;
  1701. }
  1702. const ParseResult& GetParseResult() const { return parseResult_; }
  1703. bool IsValid() const { return isValid_; }
  1704. const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; }
  1705. const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; }
  1706. const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; }
  1707. private:
  1708. InputStream& is_;
  1709. const SchemaDocumentType& sd_;
  1710. ParseResult parseResult_;
  1711. PointerType invalidSchemaPointer_;
  1712. const Ch* invalidSchemaKeyword_;
  1713. PointerType invalidDocumentPointer_;
  1714. bool isValid_;
  1715. };
  1716. RAPIDJSON_NAMESPACE_END
  1717. RAPIDJSON_DIAG_POP
  1718. #endif // RAPIDJSON_SCHEMA_H_