fpjson.schema.types.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. {
  2. This file is part of the Free Component Library
  3. JSON Schema basic types and helpers
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit FpJson.Schema.Types;
  12. {$mode ObjFPC}
  13. {$H+}
  14. {$modeswitch typehelpers}
  15. interface
  16. uses
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. System.SysUtils;
  19. {$ELSE}
  20. SysUtils;
  21. {$ENDIF}
  22. Type
  23. EJSONSchema = Class(exception);
  24. TJSONSchemaKeyword = (
  25. jskUnknown,
  26. jskId,
  27. jskAnchor,
  28. jskIdDraft4,
  29. jskSchema,
  30. jskDefs,
  31. jskTitle,
  32. jskDescription,
  33. jskDefault,
  34. jskMultipleOf,
  35. jskMaximum,
  36. jskExclusiveMaximum,
  37. jskMinimum,
  38. jskExclusiveMinimum,
  39. jskMaxLength,
  40. jskMinLength,
  41. jskPattern,
  42. jskAdditionalItems, //old
  43. jskItems,
  44. jskPrefixItems,
  45. jskMaxItems,
  46. jskMinItems,
  47. jskUniqueItems,
  48. jskMaxProperties,
  49. jskMinProperties,
  50. jskMaxContains,
  51. jskMinContains,
  52. jskRequired,
  53. jskDefinitions,
  54. jskProperties,
  55. jskPatternProperties,
  56. jskAdditionalProperties,
  57. jskPropertyNames,
  58. jskDependentSchemas,
  59. jskDependentRequired,
  60. jskEnum,
  61. jskType,
  62. jskAllOf,
  63. jskAnyOf,
  64. jskOneOf,
  65. jskNot,
  66. jskFormat,
  67. jskRef,
  68. jskIf,
  69. jskElse,
  70. jskThen,
  71. jskDynamicRef,
  72. jskDynamicAnchor,
  73. jskContains,
  74. jskComment,
  75. jskConst,
  76. jskUnevaluatedItems,
  77. jskUnevaluatedProperties,
  78. jskContentEncoding,
  79. jskContentMediaType,
  80. jskContentSchema,
  81. jskExamples,
  82. jskDeprecated,
  83. jskReadOnly,
  84. jskWriteOnly,
  85. jskVocabulary
  86. );
  87. TJSONSchemaKeywords = Set of TJSONSchemaKeyword;
  88. TJSONSubschema = (
  89. ssNot,
  90. ssIf,
  91. ssThen,
  92. ssElse,
  93. ssContains,
  94. ssUnevaluatedItems,
  95. ssUnevaluatedProperties,
  96. ssPropertyNames,
  97. ssAdditionalProperties
  98. );
  99. TJSONSubschemas = set of TJSONSubschema;
  100. TStringFormatValidator = (
  101. sfvCustom,
  102. sfvDatetime,
  103. sfvDate,
  104. sfvTime,
  105. sfvDuration,
  106. sfvEmail,
  107. sfvIdnEmail,
  108. sfvHostname,
  109. sfvIdnHostname,
  110. sfvIPV4,
  111. sfvIPV6,
  112. sfvURI,
  113. sfvURIReference,
  114. sfvIRI,
  115. sfvIRIReference,
  116. sfvUUID,
  117. sfvURITemplate,
  118. sfvJSONPointer,
  119. sfvRelativeJSONPointer,
  120. sfvRegex
  121. );
  122. TStringFormatValidators = set of TStringFormatValidator;
  123. TSchemaSimpleType = (
  124. sstNone,
  125. sstNull,
  126. sstBoolean,
  127. sstInteger,
  128. sstNumber,
  129. sstString,
  130. sstArray,
  131. sstObject,
  132. sstAny
  133. );
  134. TSchemaSimpleTypes = set of TSchemaSimpleType;
  135. TSchemaMatchType = (smAny,smNone,smConstrained); // corresponds to True, false or object schemas
  136. TSchemaMatchTypes = set of TSchemaMatchType;
  137. { TJSONSchemaKeywordHelper }
  138. TJSONSchemaKeywordHelper = Type helper for TJSONSchemaKeyword
  139. Function GetAsString: String;
  140. procedure SetAsString(const aValue: String);
  141. class function FromString(const aValue : String) : TJSONSchemaKeyword; static;
  142. property AsString : String Read GetAsString Write SetAsString;
  143. end;
  144. { TStringFormatValidatorHelper }
  145. TStringFormatValidatorHelper = Type helper for TStringFormatValidator
  146. Function GetAsString: String;
  147. procedure SetAsString(const aValue: String);
  148. class function FromString(const aValue : String) : TStringFormatValidator; static;
  149. property AsString : String Read GetAsString Write SetAsString;
  150. end;
  151. { TSchemaSimpleTypeHelper }
  152. TSchemaSimpleTypeHelper = Type Helper for TSchemaSimpleType
  153. Function GetAsString: String;
  154. procedure SetAsString(const aValue: String);
  155. class function FromString(const aValue : String) : TSchemaSimpleType; static;
  156. property AsString : String Read GetAsString Write SetAsString;
  157. end;
  158. TSchemaSimpleTypesHelper = Type Helper for TSchemaSimpleTypes
  159. Function ToString: String;
  160. property AsString : String Read ToString;
  161. end;
  162. { TJSONSubschemaHelper }
  163. TJSONSubschemaHelper = Type helper for TJSONSubschema
  164. private
  165. function GetAsSchemaKeyword: TJSONSchemaKeyword;
  166. Public
  167. Function GetAsString: String;
  168. procedure SetAsString(const aValue: String);
  169. class function FromString(const aValue : String) : TJSONSubschema; static;
  170. property AsString : String Read GetAsString Write SetAsString;
  171. property AsSchemaKeyword : TJSONSchemaKeyword Read GetAsSchemaKeyword;
  172. end;
  173. const
  174. ValidatorKeywords = [
  175. jskType, jskconst, jskEnum, jskExclusiveMaximum, jskExclusiveMinimum, jskMaximum,
  176. jskMinimum, jskMaxItems, jskMinItems, jskRequired, jskMaxLength, jskMinLength, jskMaxProperties,
  177. jskMinProperties, jskPattern, jskUniqueItems, jskMinContains, jskMaxContains, jskMultipleOf,
  178. jskDependentRequired, jskFormat, jskcontentMediaType, jskcontentEncoding, jskcontentSchema];
  179. MetadataKeywords = [
  180. jskTitle,jskDescription,jskDefault,jskDeprecated,jskExamples,jskReadOnly,jskWriteOnly
  181. ];
  182. implementation
  183. uses fpjson.schema.consts;
  184. const
  185. SimpleTypeNames : Array[TSchemaSimpleType] of string = (
  186. STNone,
  187. STNull,
  188. STBoolean,
  189. STInteger,
  190. STNumber,
  191. STString,
  192. STArray,
  193. STObject,
  194. STAny
  195. );
  196. KeywordNames : Array[TJSONSchemaKeyWord] of string = (
  197. SJKWUnknown, { jskUnknown }
  198. SJKWId, { jskId }
  199. SJKWAnchor, { jskAnchor }
  200. SJKWOldId, { jskId }
  201. SJKWSchema, { jskSchema }
  202. SJKWDefs, { jskDefs }
  203. SJKWTitle, { jskTitle }
  204. SJKWDescription, { jskDescription }
  205. SJKWDefault, { jskDefault }
  206. SJKWMultipleOf, { jskMultipleOf }
  207. SJKWMaximum, { jskMaximum }
  208. SJKWExclusiveMaximum, { jskExclusiveMaximum }
  209. SJKWMinimum, { jskMinimum }
  210. SJKWExclusiveMinimum, { jskExclusiveMinimum }
  211. SJKWMaxLength, { jskMaxLength }
  212. SJKWMinLength, { jskMinLength }
  213. SJKWPattern, { jskPattern }
  214. SJKWAdditionalItems, { jskAdditionalItems }
  215. SJKWItems, { jskItems }
  216. SJKWPrefixItems, { jskPrefixItems }
  217. SJKWMaxItems, { jskMaxItems }
  218. SJKWMinItems, { jskMinItems }
  219. SJKWUniqueItems, { jskUniqueItems }
  220. SJKWMaxProperties, { jskMaxProperties }
  221. SJKWMinProperties, { jskMinProperties }
  222. SJKWMaxContains, { jskMaxContains }
  223. SJKWMinContains, { jskMinContains }
  224. SJKWRequired, { jskRequired }
  225. SJKWDefinitions, { jskDefinitions }
  226. SJKWProperties, { jskProperties }
  227. SJKWPatternProperties, { jskPatternProperties }
  228. SJKWAdditionalProperties, { jskAdditionalProperties }
  229. SJKWPropertyNames, { jskPropertyNames }
  230. SJKWDependentSchemas, { jskDependentSchemas }
  231. SJKWDependentRequired, { jskDependentRequired }
  232. SJKWEnum, { jskEnum }
  233. SJKWType, { jskType }
  234. SJKWAllOf, { jskAllOf }
  235. SJKWAnyOf, { jskAnyOf }
  236. SJKWOneOf, { jskOneOf }
  237. SJKWNot, { jskNot }
  238. SJKWFormat, { jskFormat }
  239. SJKWRef, { jskRef }
  240. SJKWIf, { jskIf }
  241. SJKWElse, { jskElse }
  242. SJKWThen, { jskThen }
  243. SJKWDynamicRef, { jskDynamicRef }
  244. SJKWDynamicAnchor, { jskDynamicAnchor }
  245. SJKWContains, { jskContains }
  246. SJKWComment, { jskComment }
  247. SJKWConst, { jskConst}
  248. SJKWUnevaluatedItems, { jskUnevaluatedItems }
  249. SJKWUnevaluatedProperties, {jskUnevaluatedProperties}
  250. SJKWContentEncoding, { jskContentEncoding }
  251. SJKWContentMediaType, { jskContentMediaType }
  252. SJKWContentSchema, { jskContentSchema }
  253. SJKWExamples, { jskExamples }
  254. SJKWDeprecated, { jskDeprecated}
  255. SJKWReadOnly, {jskReadOnly }
  256. SJKWWriteOnly, {jskWriteOnly }
  257. SJKWVocabulary { jskVocabulary }
  258. );
  259. JSONSubschemaKeys : Array[TJSONSubschema] of TJSONSchemaKeyWord = (
  260. jskNot,
  261. jskIf,
  262. jskThen,
  263. jskElse,
  264. jskContains,
  265. jskUnevaluatedItems,
  266. jskUnevaluatedProperties,
  267. jskPropertyNames,
  268. jskAdditionalProperties
  269. );
  270. StringFormatValidatorNames : Array[TStringFormatValidator] of string = (
  271. '',
  272. SFmtDatetime,
  273. SFmtDate,
  274. SFmtTime,
  275. SFmtDuration,
  276. SFmtEmail,
  277. SFmtIdnEmail,
  278. SFmtHostname,
  279. SFmtIdnHostname,
  280. SFmtIPV4,
  281. SFmtIPV6,
  282. SFmtURI,
  283. SFmtURIReference,
  284. SFmtIRI,
  285. SFmtIRIReference,
  286. SFmtUUID,
  287. SFmtURITemplate,
  288. SFmtJSONPointer,
  289. SFmtRelativeJSONPointer,
  290. SFmtRegex
  291. );
  292. { TJSONSchemaKeywordHelper }
  293. function TJSONSchemaKeywordHelper.GetAsString: String;
  294. begin
  295. Result:=KeyWordNames[Self];
  296. end;
  297. procedure TJSONSchemaKeywordHelper.SetAsString(const aValue: String);
  298. var
  299. Kw : TJSONSchemaKeyword;
  300. begin
  301. Self:=jskUnknown;
  302. for Kw in TJSONSchemaKeyword do
  303. if aValue=KeywordNames[kw] then
  304. begin
  305. Self:=kw;
  306. Exit;
  307. end;
  308. end;
  309. class function TJSONSchemaKeywordHelper.FromString(const aValue: String): TJSONSchemaKeyword;
  310. begin
  311. Result:=jskUnknown;
  312. Result.SetAsString(aValue);
  313. end;
  314. { TStringFormatValidatorHelper }
  315. function TStringFormatValidatorHelper.GetAsString: String;
  316. begin
  317. Result:=StringFormatValidatorNames[Self];
  318. end;
  319. procedure TStringFormatValidatorHelper.SetAsString(const aValue: String);
  320. var
  321. sfv : TStringFormatValidator;
  322. begin
  323. Self:=sfvCustom;
  324. For sfv in TStringFormatValidator do
  325. if StringFormatValidatorNames[sfv]=aValue then
  326. begin
  327. Self:=sfv;
  328. Exit;
  329. end;
  330. end;
  331. class function TStringFormatValidatorHelper.FromString(const aValue: String): TStringFormatValidator;
  332. begin
  333. Result:=sfvCustom;
  334. Result.AsString:=aValue;
  335. end;
  336. { TSchemaSimpleTypeHelper }
  337. function TSchemaSimpleTypeHelper.GetAsString: String;
  338. begin
  339. Result:=SimpleTypeNames[Self]
  340. end;
  341. procedure TSchemaSimpleTypeHelper.SetAsString(const aValue: String);
  342. var
  343. st : TSchemaSimpleType;
  344. begin
  345. Self:=sstNone;
  346. for ST in TSchemaSimpleType do
  347. if aValue=SimpleTypeNames[st] then
  348. begin
  349. Self:=st;
  350. Exit;
  351. end;
  352. end;
  353. class function TSchemaSimpleTypeHelper.FromString(const aValue: String): TSchemaSimpleType;
  354. begin
  355. Result:=sstNone;
  356. Result.AsString:=aValue;
  357. end;
  358. function TSchemaSimpleTypesHelper.ToString: String;
  359. var
  360. s : TSchemaSimpleType;
  361. begin
  362. Result:='';
  363. For S in Self do
  364. begin
  365. if (Result<>'') then
  366. Result:=Result+',';
  367. Result:=Result+S.AsString;
  368. end;
  369. end;
  370. { TJSONSubschemaHelper }
  371. function TJSONSubschemaHelper.GetAsSchemaKeyword: TJSONSchemaKeyword;
  372. begin
  373. Result:=JSONSubschemaKeys[Self];
  374. end;
  375. function TJSONSubschemaHelper.GetAsString: String;
  376. begin
  377. Result:=GetAsSchemaKeyword.AsString;
  378. end;
  379. procedure TJSONSubschemaHelper.SetAsString(const aValue: String);
  380. var
  381. Kw : TJSONSchemaKeyword;
  382. T : TJSONSubschema;
  383. begin
  384. kw:=Default(TJSONSchemaKeyword);
  385. Kw.AsString:=aValue;
  386. For T in TJSONSubSchema do
  387. if JSONSubschemaKeys[T]=kw then
  388. begin
  389. Self:=T;
  390. exit;
  391. end;
  392. end;
  393. class function TJSONSubschemaHelper.FromString(const aValue: String): TJSONSubschema;
  394. begin
  395. Result:=ssNot;
  396. Result.AsString:=aValue;
  397. end;
  398. end.