@dxos/echo 0.8.4-main.dedc0f3 → 0.8.4-main.ead640a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/dist/lib/browser/chunk-HKFCK2GL.mjs +175 -0
  2. package/dist/lib/browser/chunk-HKFCK2GL.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-MTR3E5S2.mjs → chunk-MAAYELT7.mjs} +501 -457
  4. package/dist/lib/browser/chunk-MAAYELT7.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-MB6MMNFP.mjs +3857 -0
  6. package/dist/lib/browser/chunk-MB6MMNFP.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +14 -5
  8. package/dist/lib/browser/internal/index.mjs +332 -0
  9. package/dist/lib/browser/internal/index.mjs.map +7 -0
  10. package/dist/lib/browser/meta.json +1 -1
  11. package/dist/lib/browser/query/index.mjs +15 -0
  12. package/dist/lib/browser/query/index.mjs.map +7 -0
  13. package/dist/lib/browser/testing/index.mjs +260 -32
  14. package/dist/lib/browser/testing/index.mjs.map +4 -4
  15. package/dist/lib/node-esm/{chunk-MYZLAJSR.mjs → chunk-5NWDGIBT.mjs} +501 -457
  16. package/dist/lib/node-esm/chunk-5NWDGIBT.mjs.map +7 -0
  17. package/dist/lib/node-esm/chunk-AUAH4E2J.mjs +175 -0
  18. package/dist/lib/node-esm/chunk-AUAH4E2J.mjs.map +7 -0
  19. package/dist/lib/node-esm/chunk-BQRA4VLX.mjs +3857 -0
  20. package/dist/lib/node-esm/chunk-BQRA4VLX.mjs.map +7 -0
  21. package/dist/lib/node-esm/index.mjs +14 -5
  22. package/dist/lib/node-esm/internal/index.mjs +332 -0
  23. package/dist/lib/node-esm/internal/index.mjs.map +7 -0
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/query/index.mjs +15 -0
  26. package/dist/lib/node-esm/query/index.mjs.map +7 -0
  27. package/dist/lib/node-esm/testing/index.mjs +260 -32
  28. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  29. package/dist/types/src/Obj.d.ts +22 -5
  30. package/dist/types/src/Obj.d.ts.map +1 -1
  31. package/dist/types/src/Ref.d.ts +1 -1
  32. package/dist/types/src/Ref.d.ts.map +1 -1
  33. package/dist/types/src/Relation.d.ts +5 -4
  34. package/dist/types/src/Relation.d.ts.map +1 -1
  35. package/dist/types/src/Type.d.ts +16 -17
  36. package/dist/types/src/Type.d.ts.map +1 -1
  37. package/dist/types/src/errors.d.ts +72 -0
  38. package/dist/types/src/errors.d.ts.map +1 -0
  39. package/dist/types/src/index.d.ts +3 -2
  40. package/dist/types/src/index.d.ts.map +1 -1
  41. package/dist/types/src/internal/ast/annotation-helper.d.ts +8 -0
  42. package/dist/types/src/internal/ast/annotation-helper.d.ts.map +1 -0
  43. package/dist/types/src/internal/ast/annotations.d.ts +125 -0
  44. package/dist/types/src/internal/ast/annotations.d.ts.map +1 -0
  45. package/dist/types/src/internal/ast/annotations.test.d.ts +2 -0
  46. package/dist/types/src/internal/ast/annotations.test.d.ts.map +1 -0
  47. package/dist/types/src/internal/ast/entity-kind.d.ts +10 -0
  48. package/dist/types/src/internal/ast/entity-kind.d.ts.map +1 -0
  49. package/dist/types/src/internal/ast/index.d.ts +5 -0
  50. package/dist/types/src/internal/ast/index.d.ts.map +1 -0
  51. package/dist/types/src/internal/ast/types.d.ts +6 -0
  52. package/dist/types/src/internal/ast/types.d.ts.map +1 -0
  53. package/dist/types/src/internal/formats/date.d.ts +63 -0
  54. package/dist/types/src/internal/formats/date.d.ts.map +1 -0
  55. package/dist/types/src/internal/formats/date.test.d.ts +2 -0
  56. package/dist/types/src/internal/formats/date.test.d.ts.map +1 -0
  57. package/dist/types/src/internal/formats/format.d.ts +30 -0
  58. package/dist/types/src/internal/formats/format.d.ts.map +1 -0
  59. package/dist/types/src/internal/formats/format.test.d.ts +2 -0
  60. package/dist/types/src/internal/formats/format.test.d.ts.map +1 -0
  61. package/dist/types/src/internal/formats/index.d.ts +8 -0
  62. package/dist/types/src/internal/formats/index.d.ts.map +1 -0
  63. package/dist/types/src/internal/formats/number.d.ts +31 -0
  64. package/dist/types/src/internal/formats/number.d.ts.map +1 -0
  65. package/dist/types/src/internal/formats/object.d.ts +35 -0
  66. package/dist/types/src/internal/formats/object.d.ts.map +1 -0
  67. package/dist/types/src/internal/formats/select.d.ts +11 -0
  68. package/dist/types/src/internal/formats/select.d.ts.map +1 -0
  69. package/dist/types/src/internal/formats/string.d.ts +38 -0
  70. package/dist/types/src/internal/formats/string.d.ts.map +1 -0
  71. package/dist/types/src/internal/formats/types.d.ts +68 -0
  72. package/dist/types/src/internal/formats/types.d.ts.map +1 -0
  73. package/dist/types/src/internal/index.d.ts +15 -0
  74. package/dist/types/src/internal/index.d.ts.map +1 -0
  75. package/dist/types/src/internal/json/annotations.d.ts +19 -0
  76. package/dist/types/src/internal/json/annotations.d.ts.map +1 -0
  77. package/dist/types/src/internal/json/effect-schema.test.d.ts +2 -0
  78. package/dist/types/src/internal/json/effect-schema.test.d.ts.map +1 -0
  79. package/dist/types/src/internal/json/index.d.ts +2 -0
  80. package/dist/types/src/internal/json/index.d.ts.map +1 -0
  81. package/dist/types/src/internal/json/json-schema.d.ts +28 -0
  82. package/dist/types/src/internal/json/json-schema.d.ts.map +1 -0
  83. package/dist/types/src/internal/json/json-schema.test.d.ts +2 -0
  84. package/dist/types/src/internal/json/json-schema.test.d.ts.map +1 -0
  85. package/dist/types/src/internal/json-schema/index.d.ts +3 -0
  86. package/dist/types/src/internal/json-schema/index.d.ts.map +1 -0
  87. package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts +7 -0
  88. package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts.map +1 -0
  89. package/dist/types/src/internal/json-schema/json-schema-type.d.ts +250 -0
  90. package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +1 -0
  91. package/dist/types/src/internal/object/accessors.d.ts +37 -0
  92. package/dist/types/src/internal/object/accessors.d.ts.map +1 -0
  93. package/dist/types/src/internal/object/common.d.ts +18 -0
  94. package/dist/types/src/internal/object/common.d.ts.map +1 -0
  95. package/dist/types/src/internal/object/create.d.ts +40 -0
  96. package/dist/types/src/internal/object/create.d.ts.map +1 -0
  97. package/dist/types/src/internal/object/create.test.d.ts +2 -0
  98. package/dist/types/src/internal/object/create.test.d.ts.map +1 -0
  99. package/dist/types/src/internal/object/deleted.d.ts +6 -0
  100. package/dist/types/src/internal/object/deleted.d.ts.map +1 -0
  101. package/dist/types/src/internal/object/entity.d.ts +33 -0
  102. package/dist/types/src/internal/object/entity.d.ts.map +1 -0
  103. package/dist/types/src/internal/object/expando.d.ts +14 -0
  104. package/dist/types/src/internal/object/expando.d.ts.map +1 -0
  105. package/dist/types/src/internal/object/ids.d.ts +6 -0
  106. package/dist/types/src/internal/object/ids.d.ts.map +1 -0
  107. package/dist/types/src/internal/object/index.d.ts +16 -0
  108. package/dist/types/src/internal/object/index.d.ts.map +1 -0
  109. package/dist/types/src/internal/object/inspect.d.ts +2 -0
  110. package/dist/types/src/internal/object/inspect.d.ts.map +1 -0
  111. package/dist/types/src/internal/object/json-serializer.d.ts +32 -0
  112. package/dist/types/src/internal/object/json-serializer.d.ts.map +1 -0
  113. package/dist/types/src/internal/object/json-serializer.test.d.ts +2 -0
  114. package/dist/types/src/internal/object/json-serializer.test.d.ts.map +1 -0
  115. package/dist/types/src/internal/object/meta.d.ts +31 -0
  116. package/dist/types/src/internal/object/meta.d.ts.map +1 -0
  117. package/dist/types/src/internal/object/model.d.ts +117 -0
  118. package/dist/types/src/internal/object/model.d.ts.map +1 -0
  119. package/dist/types/src/internal/object/relation.d.ts +17 -0
  120. package/dist/types/src/internal/object/relation.d.ts.map +1 -0
  121. package/dist/types/src/internal/object/schema-validator.d.ts +15 -0
  122. package/dist/types/src/internal/object/schema-validator.d.ts.map +1 -0
  123. package/dist/types/src/internal/object/schema-validator.test.d.ts +2 -0
  124. package/dist/types/src/internal/object/schema-validator.test.d.ts.map +1 -0
  125. package/dist/types/src/internal/object/typed-object.d.ts +31 -0
  126. package/dist/types/src/internal/object/typed-object.d.ts.map +1 -0
  127. package/dist/types/src/internal/object/typed-object.test.d.ts +2 -0
  128. package/dist/types/src/internal/object/typed-object.test.d.ts.map +1 -0
  129. package/dist/types/src/internal/object/typename.d.ts +15 -0
  130. package/dist/types/src/internal/object/typename.d.ts.map +1 -0
  131. package/dist/types/src/internal/object/version.d.ts +14 -0
  132. package/dist/types/src/internal/object/version.d.ts.map +1 -0
  133. package/dist/types/src/internal/projection/compose.d.ts +6 -0
  134. package/dist/types/src/internal/projection/compose.d.ts.map +1 -0
  135. package/dist/types/src/internal/projection/compose.test.d.ts +2 -0
  136. package/dist/types/src/internal/projection/compose.test.d.ts.map +1 -0
  137. package/dist/types/src/internal/projection/index.d.ts +2 -0
  138. package/dist/types/src/internal/projection/index.d.ts.map +1 -0
  139. package/dist/types/src/internal/proxy/handler.test.d.ts +2 -0
  140. package/dist/types/src/internal/proxy/handler.test.d.ts.map +1 -0
  141. package/dist/types/src/internal/proxy/reactive-object.d.ts +15 -0
  142. package/dist/types/src/internal/proxy/reactive-object.d.ts.map +1 -0
  143. package/dist/types/src/internal/proxy/schema.test.d.ts +2 -0
  144. package/dist/types/src/internal/proxy/schema.test.d.ts.map +1 -0
  145. package/dist/types/src/internal/proxy/typed-handler.d.ts +44 -0
  146. package/dist/types/src/internal/proxy/typed-handler.d.ts.map +1 -0
  147. package/dist/types/src/internal/proxy/typed-handler.test.d.ts +2 -0
  148. package/dist/types/src/internal/proxy/typed-handler.test.d.ts.map +1 -0
  149. package/dist/types/src/internal/proxy/typed-object.test.d.ts +2 -0
  150. package/dist/types/src/internal/proxy/typed-object.test.d.ts.map +1 -0
  151. package/dist/types/src/internal/query/index.d.ts +2 -0
  152. package/dist/types/src/internal/query/index.d.ts.map +1 -0
  153. package/dist/types/src/internal/query/query.d.ts +17 -0
  154. package/dist/types/src/internal/query/query.d.ts.map +1 -0
  155. package/dist/types/src/internal/ref/index.d.ts +3 -0
  156. package/dist/types/src/internal/ref/index.d.ts.map +1 -0
  157. package/dist/types/src/internal/ref/ref-array.d.ts +21 -0
  158. package/dist/types/src/internal/ref/ref-array.d.ts.map +1 -0
  159. package/dist/types/src/internal/ref/ref.d.ts +206 -0
  160. package/dist/types/src/internal/ref/ref.d.ts.map +1 -0
  161. package/dist/types/src/internal/ref/ref.test.d.ts +2 -0
  162. package/dist/types/src/internal/ref/ref.test.d.ts.map +1 -0
  163. package/dist/types/src/internal/schema/echo-schema.d.ts +168 -0
  164. package/dist/types/src/internal/schema/echo-schema.d.ts.map +1 -0
  165. package/dist/types/src/internal/schema/index.d.ts +7 -0
  166. package/dist/types/src/internal/schema/index.d.ts.map +1 -0
  167. package/dist/types/src/internal/schema/manipulation.d.ts +10 -0
  168. package/dist/types/src/internal/schema/manipulation.d.ts.map +1 -0
  169. package/dist/types/src/internal/schema/runtime-schema-registry.d.ts +18 -0
  170. package/dist/types/src/internal/schema/runtime-schema-registry.d.ts.map +1 -0
  171. package/dist/types/src/internal/schema/snapshot.d.ts +6 -0
  172. package/dist/types/src/internal/schema/snapshot.d.ts.map +1 -0
  173. package/dist/types/src/internal/schema/stored-schema.d.ts +13 -0
  174. package/dist/types/src/internal/schema/stored-schema.d.ts.map +1 -0
  175. package/dist/types/src/internal/testing/index.d.ts +3 -0
  176. package/dist/types/src/internal/testing/index.d.ts.map +1 -0
  177. package/dist/types/src/internal/testing/types.d.ts +455 -0
  178. package/dist/types/src/internal/testing/types.d.ts.map +1 -0
  179. package/dist/types/src/internal/testing/utils.d.ts +10 -0
  180. package/dist/types/src/internal/testing/utils.d.ts.map +1 -0
  181. package/dist/types/src/internal/types/index.d.ts +3 -0
  182. package/dist/types/src/internal/types/index.d.ts.map +1 -0
  183. package/dist/types/src/internal/types/types.d.ts +79 -0
  184. package/dist/types/src/internal/types/types.d.ts.map +1 -0
  185. package/dist/types/src/internal/types/types.test.d.ts +2 -0
  186. package/dist/types/src/internal/types/types.test.d.ts.map +1 -0
  187. package/dist/types/src/internal/types/util.d.ts +5 -0
  188. package/dist/types/src/internal/types/util.d.ts.map +1 -0
  189. package/dist/types/src/query/index.d.ts +2 -1
  190. package/dist/types/src/query/index.d.ts.map +1 -1
  191. package/dist/types/src/query/{dsl.d.ts → query.d.ts} +34 -24
  192. package/dist/types/src/query/query.d.ts.map +1 -0
  193. package/dist/types/src/query/query.test.d.ts +2 -0
  194. package/dist/types/src/query/query.test.d.ts.map +1 -0
  195. package/dist/types/src/query/tag.d.ts +17 -0
  196. package/dist/types/src/query/tag.d.ts.map +1 -0
  197. package/dist/types/src/testing/echo-schema.d.ts +7 -0
  198. package/dist/types/src/testing/echo-schema.d.ts.map +1 -0
  199. package/dist/types/src/testing/index.d.ts +2 -0
  200. package/dist/types/src/testing/index.d.ts.map +1 -1
  201. package/dist/types/src/testing/types.d.ts +18 -18
  202. package/dist/types/src/testing/types.d.ts.map +1 -1
  203. package/dist/types/tsconfig.tsbuildinfo +1 -1
  204. package/package.json +29 -17
  205. package/src/Obj.ts +77 -12
  206. package/src/Ref.ts +1 -2
  207. package/src/Relation.ts +15 -5
  208. package/src/Type.ts +29 -30
  209. package/src/errors.ts +18 -0
  210. package/src/index.ts +4 -2
  211. package/src/internal/ast/annotation-helper.ts +22 -0
  212. package/src/internal/ast/annotations.test.ts +98 -0
  213. package/src/internal/ast/annotations.ts +219 -0
  214. package/src/internal/ast/entity-kind.ts +15 -0
  215. package/src/internal/ast/index.ts +8 -0
  216. package/src/internal/ast/types.ts +17 -0
  217. package/src/internal/formats/date.test.ts +56 -0
  218. package/src/internal/formats/date.ts +217 -0
  219. package/src/internal/formats/format.test.ts +77 -0
  220. package/src/internal/formats/format.ts +52 -0
  221. package/src/internal/formats/index.ts +12 -0
  222. package/src/internal/formats/number.ts +89 -0
  223. package/src/internal/formats/object.ts +80 -0
  224. package/src/internal/formats/select.ts +16 -0
  225. package/src/internal/formats/string.ts +76 -0
  226. package/src/internal/formats/types.ts +175 -0
  227. package/src/internal/index.ts +22 -0
  228. package/src/internal/json/annotations.ts +50 -0
  229. package/src/internal/json/effect-schema.test.ts +143 -0
  230. package/src/internal/json/index.ts +5 -0
  231. package/src/internal/json/json-schema.test.ts +857 -0
  232. package/src/internal/json/json-schema.ts +519 -0
  233. package/src/internal/json-schema/index.ts +6 -0
  234. package/src/internal/json-schema/json-schema-normalize.ts +109 -0
  235. package/src/internal/json-schema/json-schema-type.ts +403 -0
  236. package/src/internal/object/accessors.ts +153 -0
  237. package/src/internal/object/common.ts +76 -0
  238. package/src/internal/object/create.test.ts +118 -0
  239. package/src/internal/object/create.ts +96 -0
  240. package/src/internal/object/deleted.ts +19 -0
  241. package/src/internal/object/entity.ts +248 -0
  242. package/src/internal/object/expando.ts +21 -0
  243. package/src/internal/object/ids.ts +12 -0
  244. package/src/internal/object/index.ts +19 -0
  245. package/src/internal/object/inspect.ts +48 -0
  246. package/src/internal/object/json-serializer.test.ts +99 -0
  247. package/src/internal/object/json-serializer.ts +225 -0
  248. package/src/internal/object/meta.ts +61 -0
  249. package/src/internal/object/model.ts +170 -0
  250. package/src/internal/object/relation.ts +24 -0
  251. package/src/internal/object/schema-validator.test.ts +186 -0
  252. package/src/internal/object/schema-validator.ts +241 -0
  253. package/src/internal/object/typed-object.test.ts +34 -0
  254. package/src/internal/object/typed-object.ts +88 -0
  255. package/src/internal/object/typename.ts +61 -0
  256. package/src/internal/object/version.ts +22 -0
  257. package/src/internal/projection/compose.test.ts +43 -0
  258. package/src/internal/projection/compose.ts +36 -0
  259. package/src/internal/projection/index.ts +5 -0
  260. package/src/internal/proxy/handler.test.ts +163 -0
  261. package/src/internal/proxy/reactive-object.ts +108 -0
  262. package/src/internal/proxy/schema.test.ts +136 -0
  263. package/src/internal/proxy/typed-handler.test.ts +102 -0
  264. package/src/internal/proxy/typed-handler.ts +228 -0
  265. package/src/internal/proxy/typed-object.test.ts +100 -0
  266. package/src/internal/query/index.ts +5 -0
  267. package/src/internal/query/query.ts +23 -0
  268. package/src/internal/ref/index.ts +6 -0
  269. package/src/internal/ref/ref-array.ts +39 -0
  270. package/src/internal/ref/ref.test.ts +100 -0
  271. package/src/internal/ref/ref.ts +521 -0
  272. package/src/internal/schema/echo-schema.ts +383 -0
  273. package/src/internal/schema/index.ts +10 -0
  274. package/src/internal/schema/manipulation.ts +92 -0
  275. package/src/internal/schema/runtime-schema-registry.ts +78 -0
  276. package/src/internal/schema/snapshot.ts +25 -0
  277. package/src/internal/schema/stored-schema.ts +26 -0
  278. package/src/internal/testing/index.ts +6 -0
  279. package/src/internal/testing/types.ts +211 -0
  280. package/src/internal/testing/utils.ts +54 -0
  281. package/src/internal/types/index.ts +6 -0
  282. package/src/internal/types/types.test.ts +48 -0
  283. package/src/internal/types/types.ts +176 -0
  284. package/src/internal/types/util.ts +9 -0
  285. package/src/query/index.ts +2 -1
  286. package/src/query/query.test.ts +401 -0
  287. package/src/query/{dsl.ts → query.ts} +131 -46
  288. package/src/query/tag.ts +37 -0
  289. package/src/test/api.test.ts +9 -9
  290. package/src/testing/echo-schema.ts +39 -0
  291. package/src/testing/index.ts +2 -0
  292. package/src/testing/types.ts +1 -1
  293. package/dist/lib/browser/chunk-MTR3E5S2.mjs.map +0 -7
  294. package/dist/lib/node-esm/chunk-MYZLAJSR.mjs.map +0 -7
  295. package/dist/types/src/experimental/database.d.ts +0 -8
  296. package/dist/types/src/experimental/database.d.ts.map +0 -1
  297. package/dist/types/src/experimental/index.d.ts +0 -1
  298. package/dist/types/src/experimental/index.d.ts.map +0 -1
  299. package/dist/types/src/experimental/queue.d.ts +0 -8
  300. package/dist/types/src/experimental/queue.d.ts.map +0 -1
  301. package/dist/types/src/experimental/space.d.ts +0 -8
  302. package/dist/types/src/experimental/space.d.ts.map +0 -1
  303. package/dist/types/src/query/dsl.d.ts.map +0 -1
  304. package/dist/types/src/query/dsl.test.d.ts +0 -2
  305. package/dist/types/src/query/dsl.test.d.ts.map +0 -1
  306. package/src/experimental/database.ts +0 -11
  307. package/src/experimental/index.ts +0 -7
  308. package/src/experimental/queue.ts +0 -11
  309. package/src/experimental/space.ts +0 -11
  310. package/src/query/dsl.test.ts +0 -332
@@ -0,0 +1,857 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import * as Option from 'effect/Option';
6
+ import * as Schema from 'effect/Schema';
7
+ import * as SchemaAST from 'effect/SchemaAST';
8
+ import { describe, expect, test } from 'vitest';
9
+
10
+ import { type JsonProp, findAnnotation } from '@dxos/effect';
11
+ import { ObjectId } from '@dxos/keys';
12
+ import { log } from '@dxos/log';
13
+
14
+ import { Type } from '../..';
15
+ import {
16
+ EntityKind,
17
+ FieldLookupAnnotationId,
18
+ GeneratorAnnotation,
19
+ LabelAnnotation,
20
+ PropertyMeta,
21
+ getTypeAnnotation,
22
+ getTypeIdentifierAnnotation,
23
+ } from '../ast';
24
+ import { Email, FormatAnnotation, FormatEnum } from '../formats';
25
+ import { JsonSchemaType, getNormalizedEchoAnnotations, getSchemaProperty, setSchemaProperty } from '../json-schema';
26
+ import { EchoObject } from '../object';
27
+ import { Ref, createSchemaReference, getReferenceAst, getSchemaReference } from '../ref';
28
+ import { StoredSchema } from '../schema';
29
+ import { Testing, prepareAstForCompare } from '../testing';
30
+
31
+ import { toEffectSchema, toJsonSchema } from './json-schema';
32
+
33
+ const EXAMPLE_NAMESPACE = '@example';
34
+
35
+ describe('effect-to-json', () => {
36
+ test('type annotation', () => {
37
+ const Test = Schema.Struct({ name: Schema.String }).pipe(
38
+ Type.Obj({
39
+ typename: 'example.com/type/Test',
40
+ version: '0.1.0',
41
+ }),
42
+ );
43
+ const jsonSchema = toJsonSchema(Test);
44
+ expect((jsonSchema as any).$id).toEqual('dxn:type:example.com/type/Test');
45
+ expect((jsonSchema as any).version).toEqual('0.1.0');
46
+ });
47
+
48
+ test('property meta annotation', () => {
49
+ const meta = { maxLength: 0 };
50
+ const Test = Schema.Struct({
51
+ name: Schema.String.pipe(PropertyMeta(EXAMPLE_NAMESPACE, meta)),
52
+ }).pipe(
53
+ Type.Obj({
54
+ typename: 'example.com/type/Test',
55
+ version: '0.1.0',
56
+ }),
57
+ );
58
+ const jsonSchema = toJsonSchema(Test);
59
+ expect(getNormalizedEchoAnnotations(jsonSchema.properties!.name!)!.meta![EXAMPLE_NAMESPACE]).to.deep.eq(meta);
60
+ });
61
+
62
+ test('reference annotation', () => {
63
+ const Nested = Schema.Struct({
64
+ name: Schema.String,
65
+ }).pipe(
66
+ Type.Obj({
67
+ typename: 'example.com/type/TestNested',
68
+ version: '0.1.0',
69
+ }),
70
+ );
71
+ const Test = Schema.Struct({
72
+ name: Ref(Nested),
73
+ }).pipe(
74
+ Type.Obj({
75
+ typename: 'example.com/type/Test',
76
+ version: '0.1.0',
77
+ }),
78
+ );
79
+ const jsonSchema = toJsonSchema(Test);
80
+ // log.info('schema', { jsonSchema });
81
+ const nested = jsonSchema.properties!.name;
82
+ expectReferenceAnnotation(nested);
83
+ });
84
+
85
+ // TODO(ZaymonFC): @dmaretskyi we still need to fix this.
86
+ // TODO(dmaretskyi): Remove FieldLookupAnnotationId.
87
+ test.skip('reference annotation with lookup property', () => {
88
+ const Nested = Schema.Struct({
89
+ name: Schema.String,
90
+ }).pipe(
91
+ Type.Obj({
92
+ typename: 'example.com/type/TestNested',
93
+ version: '0.1.0',
94
+ }),
95
+ );
96
+ const Test = Schema.Struct({
97
+ name: Ref(Nested).annotations({ [FieldLookupAnnotationId]: 'name' }),
98
+ }).pipe(
99
+ Type.Obj({
100
+ typename: 'example.com/type/Test',
101
+ version: '0.1.0',
102
+ }),
103
+ );
104
+ const jsonSchema = toJsonSchema(Test);
105
+ const effectSchema = toEffectSchema(jsonSchema);
106
+
107
+ const annotation = findAnnotation<string>(effectSchema.ast, FieldLookupAnnotationId);
108
+ expect(annotation).to.not.toBeUndefined();
109
+ });
110
+
111
+ test('array of references', () => {
112
+ const Nested = Schema.Struct({
113
+ name: Schema.String,
114
+ }).pipe(
115
+ Type.Obj({
116
+ typename: 'example.com/type/TestNested',
117
+ version: '0.1.0',
118
+ }),
119
+ );
120
+ const Test = Schema.Struct({
121
+ name: Schema.Array(Ref(Nested)),
122
+ }).pipe(
123
+ Type.Obj({
124
+ typename: 'example.com/type/Test',
125
+ version: '0.1.0',
126
+ }),
127
+ );
128
+
129
+ const jsonSchema = toJsonSchema(Test);
130
+ expectReferenceAnnotation((jsonSchema.properties!.name as any).items);
131
+ });
132
+
133
+ test('optional references', () => {
134
+ const Nested = Schema.Struct({
135
+ name: Schema.String,
136
+ }).pipe(
137
+ Type.Obj({
138
+ typename: 'example.com/type/TestNested',
139
+ version: '0.1.0',
140
+ }),
141
+ );
142
+ const Test = Schema.Struct({
143
+ name: Schema.optional(Ref(Nested)),
144
+ }).pipe(
145
+ Type.Obj({
146
+ typename: 'example.com/type/Test',
147
+ version: '0.1.0',
148
+ }),
149
+ );
150
+ const jsonSchema = toJsonSchema(Test);
151
+ expectReferenceAnnotation(jsonSchema.properties!.name);
152
+ });
153
+
154
+ test('regular objects are not annotated', () => {
155
+ const object = Schema.Struct({ name: Schema.Struct({ name: Schema.String }) });
156
+ const jsonSchema = toJsonSchema(object);
157
+ expect(getNormalizedEchoAnnotations(jsonSchema)).to.be.undefined;
158
+ expect(getNormalizedEchoAnnotations(jsonSchema.properties!.name!)).to.be.undefined;
159
+ });
160
+
161
+ test('annotations', () => {
162
+ const TestSchema = Schema.Struct({
163
+ name: Schema.String.annotations({ description: 'Person name', title: 'Name' }),
164
+ email: Schema.String.pipe(FormatAnnotation.set(FormatEnum.Email)).annotations({
165
+ description: 'Email address',
166
+ }),
167
+ }).pipe(
168
+ Type.Obj({
169
+ typename: 'example.com/type/Contact',
170
+ version: '0.1.0',
171
+ }),
172
+ );
173
+ const jsonSchema = toJsonSchema(TestSchema);
174
+ expect(jsonSchema).to.deep.eq({
175
+ $schema: 'http://json-schema.org/draft-07/schema#',
176
+ $id: 'dxn:type:example.com/type/Contact',
177
+
178
+ entityKind: EntityKind.Object,
179
+ typename: 'example.com/type/Contact',
180
+ version: '0.1.0',
181
+
182
+ type: 'object',
183
+ required: ['name', 'email', 'id'],
184
+ properties: {
185
+ id: { type: 'string' },
186
+ name: { type: 'string', title: 'Name', description: 'Person name' },
187
+ email: {
188
+ type: 'string',
189
+
190
+ description: 'Email address',
191
+ format: 'email',
192
+ },
193
+ },
194
+ propertyOrder: ['name', 'email', 'id'],
195
+ additionalProperties: false,
196
+ });
197
+ });
198
+
199
+ test('handles suspend -- Contact schema serialization', () => {
200
+ const schema = toJsonSchema(Testing.Contact);
201
+ expect(Object.keys(schema.properties!)).toEqual([
202
+ 'id',
203
+ 'name',
204
+ 'username',
205
+ 'email',
206
+ 'phoneNumbers',
207
+ 'tasks',
208
+ 'address',
209
+ ]);
210
+ });
211
+
212
+ test('reference property by ref', () => {
213
+ const Organization = Schema.Struct({
214
+ field: Schema.String,
215
+ }).pipe(
216
+ Type.Obj({
217
+ typename: 'example.com/type/Organization',
218
+ version: '0.1.0',
219
+ }),
220
+ );
221
+
222
+ const Contact = Schema.Struct({
223
+ name: Schema.String,
224
+ organization: Ref(Organization).annotations({ description: 'Contact organization' }),
225
+ }).pipe(
226
+ Type.Obj({
227
+ typename: 'example.com/type/Contact',
228
+ version: '0.1.0',
229
+ }),
230
+ );
231
+
232
+ // log.info('Contact', { ast: Contact.ast });
233
+
234
+ const jsonSchema = toJsonSchema(Contact);
235
+ expect(jsonSchema).toEqual({
236
+ $schema: 'http://json-schema.org/draft-07/schema#',
237
+ $id: 'dxn:type:example.com/type/Contact',
238
+
239
+ entityKind: EntityKind.Object,
240
+ typename: 'example.com/type/Contact',
241
+ version: '0.1.0',
242
+
243
+ type: 'object',
244
+ additionalProperties: false,
245
+
246
+ properties: {
247
+ id: {
248
+ type: 'string',
249
+ },
250
+ name: {
251
+ type: 'string',
252
+ },
253
+ organization: {
254
+ allOf: [
255
+ {
256
+ $id: '/schemas/echo/ref',
257
+ $ref: '/schemas/echo/ref',
258
+ reference: {
259
+ schema: {
260
+ $ref: 'dxn:type:example.com/type/Organization',
261
+ },
262
+ schemaVersion: '0.1.0',
263
+ },
264
+ },
265
+ ],
266
+ description: 'Contact organization',
267
+ },
268
+ },
269
+ required: ['name', 'organization', 'id'],
270
+ propertyOrder: ['name', 'organization', 'id'],
271
+ });
272
+ });
273
+
274
+ test('add reference property', () => {
275
+ const Organization = Schema.Struct({
276
+ field: Schema.String,
277
+ }).pipe(
278
+ Type.Obj({
279
+ typename: 'example.com/type/Organization',
280
+ version: '0.1.0',
281
+ }),
282
+ );
283
+
284
+ const Contact = Schema.Struct({
285
+ name: Schema.String,
286
+ organization: Ref(Organization).annotations({ description: 'Contact organization' }),
287
+ }).pipe(
288
+ Type.Obj({
289
+ typename: 'example.com/type/Contact',
290
+ version: '0.1.0',
291
+ }),
292
+ );
293
+
294
+ const jsonSchema = toJsonSchema(Contact);
295
+ setSchemaProperty(jsonSchema, 'organization' as JsonProp, createSchemaReference(Organization.typename));
296
+ const { typename } = getSchemaReference(getSchemaProperty(jsonSchema, 'organization' as JsonProp) ?? {}) ?? {};
297
+ expect(typename).to.eq(Organization.typename);
298
+ });
299
+
300
+ test('serialize circular schema (StoredSchema)', () => {
301
+ const jsonSchema = toJsonSchema(StoredSchema);
302
+ expect(Object.keys(jsonSchema.properties!).length).toBeGreaterThan(0);
303
+
304
+ // TODO(dmaretskyi): Currently unable to deserialize.
305
+ // const effectSchema = toEffectSchema(jsonSchema);
306
+ log('schema', { jsonSchema });
307
+ });
308
+
309
+ test('tuple schema with description', () => {
310
+ const schema = Schema.Struct({
311
+ args: Schema.Tuple(
312
+ Schema.String.annotations({ description: 'The source currency' }),
313
+ Schema.String.annotations({ description: 'The target currency' }),
314
+ ),
315
+ });
316
+ const jsonSchema = toJsonSchema(schema);
317
+ log('schema', { jsonSchema });
318
+
319
+ (Schema.asserts(JsonSchemaType) as any)(jsonSchema);
320
+ });
321
+
322
+ test('reference with title annotation', () => {
323
+ const schema = Schema.Struct({
324
+ contact: Ref(Testing.Contact).annotations({ title: 'Custom Title' }),
325
+ });
326
+
327
+ // log.info('schema before', { ast: schema.ast });
328
+
329
+ const jsonSchema = toJsonSchema(schema);
330
+ // log.info('json schema', { jsonSchema });
331
+
332
+ const effectSchema = toEffectSchema(jsonSchema);
333
+ // log.info('effect schema', { ast: effectSchema.ast });
334
+
335
+ expect(
336
+ effectSchema.pipe(
337
+ Schema.pluck('contact'),
338
+ Schema.typeSchema,
339
+ (s) => s.ast,
340
+ SchemaAST.getAnnotation(SchemaAST.TitleAnnotationId),
341
+ Option.getOrUndefined,
342
+ ),
343
+ ).to.eq('Custom Title');
344
+ });
345
+
346
+ test('relation schema', () => {
347
+ const schema = Testing.HasManager;
348
+ const jsonSchema = toJsonSchema(schema);
349
+ expect(jsonSchema).toEqual({
350
+ $id: 'dxn:type:example.com/type/HasManager',
351
+ $schema: 'http://json-schema.org/draft-07/schema#',
352
+ entityKind: 'relation',
353
+ typename: 'example.com/type/HasManager',
354
+ version: '0.1.0',
355
+ relationSource: {
356
+ // TODO(dmaretskyi): Should those point to specific schema version?
357
+ $ref: 'dxn:type:example.com/type/Contact',
358
+ },
359
+ relationTarget: {
360
+ // TODO(dmaretskyi): Should those point to specific schema version?
361
+ $ref: 'dxn:type:example.com/type/Contact',
362
+ },
363
+ type: 'object',
364
+ properties: {
365
+ id: {
366
+ type: 'string',
367
+ },
368
+ since: {
369
+ type: 'string',
370
+ },
371
+ },
372
+ propertyOrder: ['since', 'id'],
373
+ required: ['id'],
374
+ additionalProperties: false,
375
+ });
376
+ });
377
+
378
+ test('label prop', () => {
379
+ const Organization = Schema.Struct({
380
+ id: ObjectId,
381
+ name: Schema.String,
382
+ }).pipe(
383
+ EchoObject({
384
+ typename: 'example.com/type/Organization',
385
+ version: '0.1.0',
386
+ }),
387
+ LabelAnnotation.set(['name']),
388
+ );
389
+
390
+ const jsonSchema = toJsonSchema(Organization);
391
+ expect(jsonSchema).toEqual({
392
+ $id: 'dxn:type:example.com/type/Organization',
393
+ $schema: 'http://json-schema.org/draft-07/schema#',
394
+ typename: 'example.com/type/Organization',
395
+ version: '0.1.0',
396
+ entityKind: 'object',
397
+ type: 'object',
398
+ properties: {
399
+ id: {
400
+ type: 'string',
401
+ pattern: '^[0-7][0-9A-HJKMNP-TV-Z]{25}$',
402
+ description: 'A Universally Unique Lexicographically Sortable Identifier',
403
+ },
404
+ name: {
405
+ type: 'string',
406
+ },
407
+ },
408
+ annotations: {
409
+ labelProp: ['name'],
410
+ },
411
+ propertyOrder: ['id', 'name'],
412
+ required: ['id', 'name'],
413
+ additionalProperties: false,
414
+ });
415
+ });
416
+
417
+ test('object id with description', () => {
418
+ const schema = Schema.Struct({
419
+ id: ObjectId.annotations({ description: 'The id' }),
420
+ });
421
+ // log.info('schema', { schema: ObjectId.ast });
422
+ const jsonSchema = toJsonSchema(schema);
423
+ expect(jsonSchema).toMatchInlineSnapshot(`
424
+ {
425
+ "$schema": "http://json-schema.org/draft-07/schema#",
426
+ "additionalProperties": false,
427
+ "properties": {
428
+ "id": {
429
+ "description": "The id",
430
+ "pattern": "^[0-7][0-9A-HJKMNP-TV-Z]{25}$",
431
+ "type": "string",
432
+ },
433
+ },
434
+ "propertyOrder": [
435
+ "id",
436
+ ],
437
+ "required": [
438
+ "id",
439
+ ],
440
+ "type": "object",
441
+ }
442
+ `);
443
+ });
444
+
445
+ test('email schema', () => {
446
+ const schema = Email;
447
+ const jsonSchema = toJsonSchema(schema);
448
+ expect(jsonSchema).toMatchInlineSnapshot(`
449
+ {
450
+ "$schema": "http://json-schema.org/draft-07/schema#",
451
+ "description": "Email address",
452
+ "format": "email",
453
+ "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
454
+ "title": "Email",
455
+ "type": "string",
456
+ }
457
+ `);
458
+ const effectSchema = toEffectSchema(jsonSchema);
459
+ expect(prepareAstForCompare(effectSchema.ast)).to.deep.eq(prepareAstForCompare(schema.ast));
460
+ });
461
+
462
+ const expectReferenceAnnotation = (object: JsonSchemaType) => {
463
+ expect(object.reference).to.deep.eq({
464
+ schema: {
465
+ $ref: 'dxn:type:example.com/type/TestNested',
466
+ },
467
+ schemaVersion: '0.1.0',
468
+ });
469
+ };
470
+ });
471
+
472
+ describe('json-to-effect', () => {
473
+ describe('field schema', () => {
474
+ test('email', () => {
475
+ const schema = Email;
476
+ expect(toJsonSchema(schema)).to.deep.eq({
477
+ $schema: 'http://json-schema.org/draft-07/schema#',
478
+ type: 'string',
479
+ format: 'email',
480
+ title: 'Email',
481
+ description: 'Email address',
482
+ // TODO(dmaretskyi): omit pattern.
483
+ pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
484
+ });
485
+ });
486
+ });
487
+
488
+ for (const partial of [false, true]) {
489
+ test(`deserialized equals original ${partial ? 'with partial' : ''}`, () => {
490
+ const Organization = Schema.Struct({
491
+ field: Schema.String,
492
+ }).pipe(
493
+ Type.Obj({
494
+ typename: 'example.com/type/Organization',
495
+ version: '0.1.0',
496
+ }),
497
+ );
498
+
499
+ const fields = {
500
+ string: Schema.String,
501
+ number: Schema.Number.pipe(PropertyMeta(EXAMPLE_NAMESPACE, { is_date: true })),
502
+ boolean: Schema.Boolean,
503
+ array: Schema.Array(Schema.String),
504
+ twoDArray: Schema.Array(Schema.Array(Schema.String)),
505
+ record: Schema.Record({ key: Schema.String, value: Schema.Number }),
506
+ object: Schema.Struct({ id: Schema.String, field: Ref(Organization) }),
507
+ echoObject: Ref(Organization),
508
+ echoObjectArray: Schema.Array(Ref(Organization)),
509
+ email: Schema.String.pipe(FormatAnnotation.set(FormatEnum.Email)),
510
+ null: Schema.Null,
511
+ } as const;
512
+
513
+ const Test = (partial ? Schema.partial(Schema.Struct(fields)) : Schema.Struct(fields)).pipe(
514
+ Type.Obj({
515
+ typename: 'example.com/type/Test',
516
+ version: '0.1.0',
517
+ }),
518
+ );
519
+
520
+ const jsonSchema = toJsonSchema(Test);
521
+ // log.info('schema', { jsonSchema });
522
+
523
+ const schema = toEffectSchema(jsonSchema);
524
+
525
+ expect(() => expect(schema.ast).to.deep.eq(Test.ast)).to.throw();
526
+ expect(() => expect(prepareAstForCompare(Test.ast)).to.deep.eq(Test.ast)).to.throw();
527
+ expect(() => expect(schema.ast).to.deep.eq(prepareAstForCompare(Test.ast))).to.throw();
528
+ // log.info('', { original: prepareAstForCompare(Schema.ast), deserialized: prepareAstForCompare(schema.ast) });
529
+ expect(prepareAstForCompare(schema.ast)).to.deep.eq(prepareAstForCompare(Test.ast));
530
+
531
+ // TODO(dmaretskyi): Fix.
532
+ // expect(
533
+ // SchemaAST.getPropertySignatures(schema.ast).find((prop) => prop.name === 'email')!.type.annotations[
534
+ // FormatAnnotationId
535
+ // ],
536
+ // ).toEqual('email');
537
+ });
538
+ }
539
+
540
+ test('legacy schema with dxn:type $id gets decoded', () => {
541
+ const jsonSchema: JsonSchemaType = {
542
+ $id: 'dxn:type:example.com/type/Project',
543
+ $schema: 'http://json-schema.org/draft-07/schema#',
544
+ additionalProperties: false,
545
+ echo: {
546
+ type: {
547
+ schemaId: '01JERV1HQCQZDQ4NVCJ42QB38F',
548
+ typename: 'example.com/type/Project',
549
+ version: '0.1.0',
550
+ },
551
+ },
552
+ properties: {
553
+ description: {
554
+ type: 'string',
555
+ },
556
+ id: {
557
+ type: 'string',
558
+ },
559
+ name: {
560
+ echo: {
561
+ generator: 'commerce.productName',
562
+ },
563
+ type: 'string',
564
+ },
565
+ },
566
+ required: ['id'],
567
+ type: 'object',
568
+ version: '0.1.0',
569
+ };
570
+
571
+ const schema = toEffectSchema(jsonSchema);
572
+ expect(getTypeAnnotation(schema)).to.deep.eq({
573
+ kind: EntityKind.Object,
574
+ typename: 'example.com/type/Project',
575
+ version: '0.1.0',
576
+ });
577
+ expect(getTypeIdentifierAnnotation(schema)).to.deep.eq('dxn:echo:@:01JERV1HQCQZDQ4NVCJ42QB38F');
578
+ });
579
+
580
+ test('symbol annotations get compared', () => {
581
+ const schema1 = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Email));
582
+ const schema2 = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Currency));
583
+ expect(prepareAstForCompare(schema1.ast)).not.to.deep.eq(prepareAstForCompare(schema2.ast));
584
+ });
585
+
586
+ test('description gets preserved', () => {
587
+ const schema = Schema.Struct({
588
+ name: Schema.String.annotations({ description: 'Name' }),
589
+ });
590
+ const jsonSchema = toJsonSchema(schema);
591
+ const effectSchema = toEffectSchema(jsonSchema);
592
+ const jsonSchema2 = toJsonSchema(effectSchema);
593
+ expect(jsonSchema2.properties!.name.description).to.eq('Name');
594
+ });
595
+
596
+ test('relation schema roundtrip', () => {
597
+ const schema = Testing.HasManager;
598
+ const jsonSchema = toJsonSchema(schema);
599
+ const effectSchema = toEffectSchema(jsonSchema);
600
+ expect(prepareAstForCompare(effectSchema.ast)).to.deep.eq(prepareAstForCompare(schema.ast));
601
+ });
602
+
603
+ test('generator annotation', () => {
604
+ const schema = Schema.Struct({
605
+ name: Schema.String.pipe(GeneratorAnnotation.set('commerce.productName')),
606
+ });
607
+ const jsonSchema = toJsonSchema(schema);
608
+ expect(jsonSchema).toMatchInlineSnapshot(`
609
+ {
610
+ "$schema": "http://json-schema.org/draft-07/schema#",
611
+ "additionalProperties": false,
612
+ "properties": {
613
+ "name": {
614
+ "annotations": {
615
+ "generator": "commerce.productName",
616
+ },
617
+ "type": "string",
618
+ },
619
+ },
620
+ "propertyOrder": [
621
+ "name",
622
+ ],
623
+ "required": [
624
+ "name",
625
+ ],
626
+ "type": "object",
627
+ }
628
+ `);
629
+ });
630
+
631
+ // test('generator annotation on object', () => {
632
+ // const schema = Schema.Struct({
633
+ // });
634
+ // const jsonSchema = toJsonSchema(schema);
635
+ // expect(jsonSchema).toMatchInlineSnapshot();
636
+ // });
637
+
638
+ test('default annotation ', () => {
639
+ const schema = Schema.Struct({
640
+ str: Schema.String.annotations({
641
+ default: 'foo',
642
+ }),
643
+ arr: Schema.Array(Schema.String).annotations({
644
+ default: [],
645
+ }),
646
+ obj: Schema.Struct({
647
+ foo: Schema.optional(Schema.String).annotations({
648
+ default: 'bar',
649
+ }),
650
+ }),
651
+ });
652
+ const jsonSchema = toJsonSchema(schema);
653
+ expect(jsonSchema).toMatchInlineSnapshot(`
654
+ {
655
+ "$schema": "http://json-schema.org/draft-07/schema#",
656
+ "additionalProperties": false,
657
+ "properties": {
658
+ "arr": {
659
+ "default": [],
660
+ "items": {
661
+ "type": "string",
662
+ },
663
+ "type": "array",
664
+ },
665
+ "obj": {
666
+ "additionalProperties": false,
667
+ "properties": {
668
+ "foo": {
669
+ "default": "bar",
670
+ "type": "string",
671
+ },
672
+ },
673
+ "propertyOrder": [
674
+ "foo",
675
+ ],
676
+ "required": [],
677
+ "type": "object",
678
+ },
679
+ "str": {
680
+ "default": "foo",
681
+ "type": "string",
682
+ },
683
+ },
684
+ "propertyOrder": [
685
+ "str",
686
+ "arr",
687
+ "obj",
688
+ ],
689
+ "required": [
690
+ "str",
691
+ "arr",
692
+ "obj",
693
+ ],
694
+ "type": "object",
695
+ }
696
+ `);
697
+ });
698
+
699
+ test('schema with optional referece', () => {
700
+ const TestSchema = Schema.Struct({
701
+ contact: Schema.optional(Ref(Testing.Contact)),
702
+ });
703
+ const jsonSchema = toJsonSchema(TestSchema);
704
+ expect(jsonSchema).toMatchInlineSnapshot(`
705
+ {
706
+ "$schema": "http://json-schema.org/draft-07/schema#",
707
+ "additionalProperties": false,
708
+ "properties": {
709
+ "contact": {
710
+ "$id": "/schemas/echo/ref",
711
+ "$ref": "/schemas/echo/ref",
712
+ "reference": {
713
+ "schema": {
714
+ "$ref": "dxn:type:example.com/type/Contact",
715
+ },
716
+ "schemaVersion": "0.1.0",
717
+ },
718
+ },
719
+ },
720
+ "propertyOrder": [
721
+ "contact",
722
+ ],
723
+ "required": [],
724
+ "type": "object",
725
+ }
726
+ `);
727
+
728
+ const effectSchema = toEffectSchema(jsonSchema);
729
+ expect(prepareAstForCompare(effectSchema.ast)).to.deep.eq(prepareAstForCompare(TestSchema.ast));
730
+ });
731
+
732
+ test('object nested inside another struct', () => {
733
+ const Contact = Schema.Struct({
734
+ name: Schema.String,
735
+ }).pipe(EchoObject({ typename: 'example.com/type/Contact', version: '0.1.0' }));
736
+ const input = Schema.Struct({
737
+ contact: Contact,
738
+ });
739
+ const jsonSchema = toJsonSchema(input);
740
+ expect(jsonSchema).toMatchInlineSnapshot(`
741
+ {
742
+ "$schema": "http://json-schema.org/draft-07/schema#",
743
+ "additionalProperties": false,
744
+ "properties": {
745
+ "contact": {
746
+ "$id": "dxn:type:example.com/type/Contact",
747
+ "additionalProperties": false,
748
+ "entityKind": "object",
749
+ "properties": {
750
+ "id": {
751
+ "type": "string",
752
+ },
753
+ "name": {
754
+ "type": "string",
755
+ },
756
+ },
757
+ "propertyOrder": [
758
+ "name",
759
+ "id",
760
+ ],
761
+ "required": [
762
+ "name",
763
+ "id",
764
+ ],
765
+ "type": "object",
766
+ "typename": "example.com/type/Contact",
767
+ "version": "0.1.0",
768
+ },
769
+ },
770
+ "propertyOrder": [
771
+ "contact",
772
+ ],
773
+ "required": [
774
+ "contact",
775
+ ],
776
+ "type": "object",
777
+ }
778
+ `);
779
+
780
+ const effectSchema = toEffectSchema(jsonSchema);
781
+ expect(prepareAstForCompare(effectSchema.ast)).to.deep.eq(prepareAstForCompare(input.ast));
782
+ });
783
+ });
784
+
785
+ describe('reference', () => {
786
+ test('reference annotation', () => {
787
+ const schema = Ref(Testing.Contact);
788
+ const jsonSchema = toJsonSchema(schema);
789
+ expect(jsonSchema).toEqual({
790
+ $id: '/schemas/echo/ref',
791
+ $ref: '/schemas/echo/ref',
792
+ $schema: 'http://json-schema.org/draft-07/schema#',
793
+ reference: {
794
+ schema: {
795
+ $ref: 'dxn:type:example.com/type/Contact',
796
+ },
797
+ schemaVersion: '0.1.0',
798
+ },
799
+ });
800
+ });
801
+
802
+ test('title annotation', () => {
803
+ const schema = Ref(Testing.Contact).annotations({ title: 'My custom title' });
804
+ const jsonSchema = toJsonSchema(schema);
805
+ expect(jsonSchema).toEqual({
806
+ $schema: 'http://json-schema.org/draft-07/schema#',
807
+ allOf: [
808
+ {
809
+ $id: '/schemas/echo/ref',
810
+ $ref: '/schemas/echo/ref',
811
+ reference: {
812
+ schema: {
813
+ $ref: 'dxn:type:example.com/type/Contact',
814
+ },
815
+ schemaVersion: '0.1.0',
816
+ },
817
+ },
818
+ ],
819
+ title: 'My custom title',
820
+ });
821
+ });
822
+
823
+ test('description annotation', () => {
824
+ const schema = Ref(Testing.Contact).annotations({ description: 'My custom description' });
825
+ const jsonSchema = toJsonSchema(schema);
826
+ expect(jsonSchema).toEqual({
827
+ $schema: 'http://json-schema.org/draft-07/schema#',
828
+ allOf: [
829
+ {
830
+ $id: '/schemas/echo/ref',
831
+ $ref: '/schemas/echo/ref',
832
+ reference: {
833
+ schema: {
834
+ $ref: 'dxn:type:example.com/type/Contact',
835
+ },
836
+ schemaVersion: '0.1.0',
837
+ },
838
+ },
839
+ ],
840
+ description: 'My custom description',
841
+ });
842
+
843
+ const effectSchema = toEffectSchema(jsonSchema);
844
+ expect(prepareAstForCompare(effectSchema.ast)).to.deep.eq(prepareAstForCompare(schema.ast));
845
+ });
846
+
847
+ test('serialize and deserialize', () => {
848
+ const schema = Ref(Testing.Contact);
849
+ const jsonSchema = toJsonSchema(schema);
850
+ const deserializedSchema = toEffectSchema(jsonSchema);
851
+ const refAst = getReferenceAst(deserializedSchema.ast);
852
+ expect(refAst).toEqual({
853
+ typename: Testing.Contact.typename,
854
+ version: Testing.Contact.version,
855
+ });
856
+ });
857
+ });