@wirenboard/json-editor 2.5.3-wb13

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 (283) hide show
  1. package/.env-dist +2 -0
  2. package/.eslintrc +7 -0
  3. package/.gitattributes +1 -0
  4. package/.github/PULL_REQUEST_TEMPLATE.md +9 -0
  5. package/.github/issue_template +25 -0
  6. package/.github/workflows/build.yml +58 -0
  7. package/.travis.yml +70 -0
  8. package/CHANGELOG.md +915 -0
  9. package/CONTRIBUTING.md +92 -0
  10. package/LICENSE +20 -0
  11. package/Makefile +26 -0
  12. package/README.md +1646 -0
  13. package/README_ADDON.md +573 -0
  14. package/UPGRADING.md +46 -0
  15. package/build/CssToJson.js +55 -0
  16. package/codecept.conf.js +35 -0
  17. package/config/.eslintrc +7 -0
  18. package/config/codeceptjs_helpers.js +139 -0
  19. package/config/helpers.js +10 -0
  20. package/config/karma.conf.js +93 -0
  21. package/config/readme.md +31 -0
  22. package/config/webpack.common.js +75 -0
  23. package/config/webpack.dev.js +15 -0
  24. package/config/webpack.nonmin.js +19 -0
  25. package/config/webpack.prod.js +25 -0
  26. package/dist/jsoneditor.js +14 -0
  27. package/dist/nonmin/jsoneditor.js +29097 -0
  28. package/dist/nonmin/jsoneditor.js.map +1 -0
  29. package/docs/ace_editor.html +56 -0
  30. package/docs/advanced.html +136 -0
  31. package/docs/basic.html +63 -0
  32. package/docs/basic_person.json +26 -0
  33. package/docs/choices.html +86 -0
  34. package/docs/cleave.html +132 -0
  35. package/docs/colorpicker.html +194 -0
  36. package/docs/css_integration.html +135 -0
  37. package/docs/datetime.html +305 -0
  38. package/docs/describedby.html +161 -0
  39. package/docs/enumsource.html +67 -0
  40. package/docs/images/categoriesDemo.png +0 -0
  41. package/docs/images/inheritance_tree.png +0 -0
  42. package/docs/images/jsoneditor.png +0 -0
  43. package/docs/imask.html +192 -0
  44. package/docs/index.html +579 -0
  45. package/docs/materialize_css.html +164 -0
  46. package/docs/meta_schema.json +705 -0
  47. package/docs/multiple_upload_base64.html +65 -0
  48. package/docs/person.json +73 -0
  49. package/docs/polyfills/assign.js +29 -0
  50. package/docs/radio.html +156 -0
  51. package/docs/recursive.html +170 -0
  52. package/docs/select2.html +99 -0
  53. package/docs/selectize.html +100 -0
  54. package/docs/signature.html +42 -0
  55. package/docs/starrating.html +137 -0
  56. package/docs/upload.html +131 -0
  57. package/docs/uuid.html +70 -0
  58. package/docs/wysiwyg.html +56 -0
  59. package/jasmine.json +11 -0
  60. package/json-editor-json-editor-2.5.3-wb13.tgz +0 -0
  61. package/package.json +100 -0
  62. package/release-notes.md +88 -0
  63. package/src/core.js +412 -0
  64. package/src/defaults.js +402 -0
  65. package/src/editor.js +707 -0
  66. package/src/editors/ace.js +89 -0
  67. package/src/editors/array/choices.js +103 -0
  68. package/src/editors/array/select2.js +110 -0
  69. package/src/editors/array/selectize.js +103 -0
  70. package/src/editors/array.css +9 -0
  71. package/src/editors/array.css.js +3 -0
  72. package/src/editors/array.js +818 -0
  73. package/src/editors/autocomplete.js +58 -0
  74. package/src/editors/base64.js +157 -0
  75. package/src/editors/button.js +97 -0
  76. package/src/editors/checkbox.js +95 -0
  77. package/src/editors/choices.css +3 -0
  78. package/src/editors/choices.css.js +3 -0
  79. package/src/editors/choices.js +69 -0
  80. package/src/editors/colorpicker.js +103 -0
  81. package/src/editors/datetime.js +141 -0
  82. package/src/editors/describedby.js +188 -0
  83. package/src/editors/enum.js +136 -0
  84. package/src/editors/hidden.js +127 -0
  85. package/src/editors/index.js +81 -0
  86. package/src/editors/info.js +20 -0
  87. package/src/editors/integer.js +19 -0
  88. package/src/editors/ip.js +36 -0
  89. package/src/editors/jodit.js +64 -0
  90. package/src/editors/multiple.js +409 -0
  91. package/src/editors/multiselect.js +218 -0
  92. package/src/editors/null.js +18 -0
  93. package/src/editors/number.js +51 -0
  94. package/src/editors/object.css +41 -0
  95. package/src/editors/object.css.js +3 -0
  96. package/src/editors/object.js +1290 -0
  97. package/src/editors/radio.js +111 -0
  98. package/src/editors/sceditor.js +72 -0
  99. package/src/editors/select.js +370 -0
  100. package/src/editors/select2.js +110 -0
  101. package/src/editors/selectize.js +112 -0
  102. package/src/editors/signature.js +113 -0
  103. package/src/editors/simplemde.js +100 -0
  104. package/src/editors/starrating.css +52 -0
  105. package/src/editors/starrating.css.js +3 -0
  106. package/src/editors/starrating.js +135 -0
  107. package/src/editors/stepper.js +27 -0
  108. package/src/editors/string.js +372 -0
  109. package/src/editors/table.js +516 -0
  110. package/src/editors/upload.js +321 -0
  111. package/src/editors/uuid.js +56 -0
  112. package/src/iconlib.js +24 -0
  113. package/src/iconlibs/bootstrap2.js +28 -0
  114. package/src/iconlibs/bootstrap3.js +28 -0
  115. package/src/iconlibs/fontawesome3.js +28 -0
  116. package/src/iconlibs/fontawesome4.js +28 -0
  117. package/src/iconlibs/fontawesome5.js +28 -0
  118. package/src/iconlibs/foundation2.js +24 -0
  119. package/src/iconlibs/foundation3.js +28 -0
  120. package/src/iconlibs/index.js +25 -0
  121. package/src/iconlibs/jqueryui.js +28 -0
  122. package/src/iconlibs/materialicons.js +49 -0
  123. package/src/iconlibs/openiconic.js +28 -0
  124. package/src/iconlibs/spectre.js +28 -0
  125. package/src/resolvers.js +128 -0
  126. package/src/schemaloader.js +408 -0
  127. package/src/style.css +150 -0
  128. package/src/style.css.js +3 -0
  129. package/src/templates/default.js +52 -0
  130. package/src/templates/ejs.js +13 -0
  131. package/src/templates/handlebars.js +1 -0
  132. package/src/templates/hogan.js +10 -0
  133. package/src/templates/index.js +21 -0
  134. package/src/templates/lodash.js +9 -0
  135. package/src/templates/markup.js +9 -0
  136. package/src/templates/mustache.js +9 -0
  137. package/src/templates/swig.js +1 -0
  138. package/src/templates/underscore.js +9 -0
  139. package/src/theme.js +659 -0
  140. package/src/themes/barebones.css +35 -0
  141. package/src/themes/barebones.css.js +3 -0
  142. package/src/themes/barebones.js +28 -0
  143. package/src/themes/bootstrap2.js +319 -0
  144. package/src/themes/bootstrap3.css +0 -0
  145. package/src/themes/bootstrap3.css.js +3 -0
  146. package/src/themes/bootstrap3.js +315 -0
  147. package/src/themes/bootstrap4.css +89 -0
  148. package/src/themes/bootstrap4.css.js +3 -0
  149. package/src/themes/bootstrap4.js +690 -0
  150. package/src/themes/bootstrap5.css.js +3 -0
  151. package/src/themes/foundation.js +569 -0
  152. package/src/themes/html.css +60 -0
  153. package/src/themes/html.css.js +3 -0
  154. package/src/themes/html.js +71 -0
  155. package/src/themes/index.js +28 -0
  156. package/src/themes/jqueryui.js +198 -0
  157. package/src/themes/materialize.js +426 -0
  158. package/src/themes/spectre.css +208 -0
  159. package/src/themes/spectre.css.js +3 -0
  160. package/src/themes/spectre.js +406 -0
  161. package/src/themes/tailwind.css +249 -0
  162. package/src/themes/tailwind.css.js +3 -0
  163. package/src/themes/tailwind.js +443 -0
  164. package/src/utilities.js +138 -0
  165. package/src/validator.js +877 -0
  166. package/src/validators/ip-validator.js +51 -0
  167. package/tests/Dockerfile +3 -0
  168. package/tests/README.md +48 -0
  169. package/tests/codeceptjs/codecept.json +42 -0
  170. package/tests/codeceptjs/constrains/if-then-else_test.js +143 -0
  171. package/tests/codeceptjs/core_test.js +217 -0
  172. package/tests/codeceptjs/editors/advanced_test.js +13 -0
  173. package/tests/codeceptjs/editors/array_any_of_test.js +50 -0
  174. package/tests/codeceptjs/editors/array_test.js +900 -0
  175. package/tests/codeceptjs/editors/button_test.js +35 -0
  176. package/tests/codeceptjs/editors/checkbox_test.js +21 -0
  177. package/tests/codeceptjs/editors/colorpicker_test.js +27 -0
  178. package/tests/codeceptjs/editors/datetime_test.js +33 -0
  179. package/tests/codeceptjs/editors/inheritance_test.js +11 -0
  180. package/tests/codeceptjs/editors/integer_test.js +84 -0
  181. package/tests/codeceptjs/editors/issues/issue-gh-812_test.js +32 -0
  182. package/tests/codeceptjs/editors/jodit_test.js +24 -0
  183. package/tests/codeceptjs/editors/multiselect_test.js +8 -0
  184. package/tests/codeceptjs/editors/number_test.js +82 -0
  185. package/tests/codeceptjs/editors/object_test.js +204 -0
  186. package/tests/codeceptjs/editors/option-no_default_values_test.js +42 -0
  187. package/tests/codeceptjs/editors/programmatic-changes_test.js +20 -0
  188. package/tests/codeceptjs/editors/radio_test.js +10 -0
  189. package/tests/codeceptjs/editors/rating_test.js +13 -0
  190. package/tests/codeceptjs/editors/select_test.js +22 -0
  191. package/tests/codeceptjs/editors/stepper_test.js +27 -0
  192. package/tests/codeceptjs/editors/string_test.js +118 -0
  193. package/tests/codeceptjs/editors/table-confirm-delete_test.js +67 -0
  194. package/tests/codeceptjs/editors/tabs_test.js +14 -0
  195. package/tests/codeceptjs/editors/uuid_test.js +21 -0
  196. package/tests/codeceptjs/editors/validation_test.js +14 -0
  197. package/tests/codeceptjs/meta-schema_test.js +17 -0
  198. package/tests/codeceptjs/schemaloader_test.js +13 -0
  199. package/tests/codeceptjs/steps.d.ts +13 -0
  200. package/tests/codeceptjs/steps_file.js +12 -0
  201. package/tests/codeceptjs/themes_test.js +519 -0
  202. package/tests/docker-compose.yml +34 -0
  203. package/tests/fixtures/basic_person.json +26 -0
  204. package/tests/fixtures/nested_object.json +26 -0
  205. package/tests/fixtures/person.json +55 -0
  206. package/tests/fixtures/recursive.json +8 -0
  207. package/tests/fixtures/some_types.json +32 -0
  208. package/tests/fixtures/string.json +3 -0
  209. package/tests/fixtures/validation.json +1140 -0
  210. package/tests/pages/_demo.html +475 -0
  211. package/tests/pages/advanced.html +137 -0
  212. package/tests/pages/anyof.html +80 -0
  213. package/tests/pages/array-anyof.html +142 -0
  214. package/tests/pages/array-checkboxes.html +41 -0
  215. package/tests/pages/array-choices.html +45 -0
  216. package/tests/pages/array-integers.html +37 -0
  217. package/tests/pages/array-move-events.html +61 -0
  218. package/tests/pages/array-multiselects.html +42 -0
  219. package/tests/pages/array-nested-arrays.html +40 -0
  220. package/tests/pages/array-numbers.html +37 -0
  221. package/tests/pages/array-objects.html +42 -0
  222. package/tests/pages/array-ratings.html +40 -0
  223. package/tests/pages/array-selectize.html +51 -0
  224. package/tests/pages/array-selects.html +36 -0
  225. package/tests/pages/array-strings.html +36 -0
  226. package/tests/pages/array.html +42 -0
  227. package/tests/pages/assets/pages.css +130 -0
  228. package/tests/pages/button-callbacks.html +77 -0
  229. package/tests/pages/checkbox-labels.html +114 -0
  230. package/tests/pages/colorpicker-no-3rd-party.html +43 -0
  231. package/tests/pages/colorpicker-use-vanilla-picker.html +50 -0
  232. package/tests/pages/core.html +118 -0
  233. package/tests/pages/datetime.html +76 -0
  234. package/tests/pages/form-name.html +108 -0
  235. package/tests/pages/grid-strict.html +311 -0
  236. package/tests/pages/grid.html +284 -0
  237. package/tests/pages/if-then-else-allOf.html +117 -0
  238. package/tests/pages/inheritance.html +76 -0
  239. package/tests/pages/integer.html +68 -0
  240. package/tests/pages/issues/_template.html +50 -0
  241. package/tests/pages/issues/issue-gh-812.html +110 -0
  242. package/tests/pages/issues/issue-gh-823-meta-schema.html +35 -0
  243. package/tests/pages/issues/issue-gh-848.html +81 -0
  244. package/tests/pages/meta_schema.json +705 -0
  245. package/tests/pages/number.html +89 -0
  246. package/tests/pages/object-no-additional-properties.html +65 -0
  247. package/tests/pages/object-no-duplicated-id.html +68 -0
  248. package/tests/pages/object-required-properties.html +236 -0
  249. package/tests/pages/object-with-dependencies-array.html +46 -0
  250. package/tests/pages/object-with-dependencies.html +60 -0
  251. package/tests/pages/object.html +79 -0
  252. package/tests/pages/oneof.html +103 -0
  253. package/tests/pages/option-no_default_values.html +58 -0
  254. package/tests/pages/programmatic-changes.html +120 -0
  255. package/tests/pages/read-only.html +105 -0
  256. package/tests/pages/select.html +41 -0
  257. package/tests/pages/stepper.html +59 -0
  258. package/tests/pages/string-ace-editor.html +52 -0
  259. package/tests/pages/string-cleave.html +46 -0
  260. package/tests/pages/string-custom-attributes.html +62 -0
  261. package/tests/pages/string-formats.html +52 -0
  262. package/tests/pages/string-formats2.html +57 -0
  263. package/tests/pages/string-jodit-editor.html +49 -0
  264. package/tests/pages/string-sceditor.html +62 -0
  265. package/tests/pages/table-move-events.html +56 -0
  266. package/tests/pages/table.html +46 -0
  267. package/tests/pages/tabs.html +131 -0
  268. package/tests/pages/themes.html +527 -0
  269. package/tests/pages/translate-property.html +247 -0
  270. package/tests/pages/urn.html +93 -0
  271. package/tests/pages/uuid.html +72 -0
  272. package/tests/pages/validation.html +99 -0
  273. package/tests/unit/.eslintrc +8 -0
  274. package/tests/unit/core.spec.js +309 -0
  275. package/tests/unit/defaults.spec.js +40 -0
  276. package/tests/unit/editor.spec.js +160 -0
  277. package/tests/unit/editors/array.spec.js +86 -0
  278. package/tests/unit/editors/object.spec.js +79 -0
  279. package/tests/unit/editors/table.spec.js +91 -0
  280. package/tests/unit/readme.md +35 -0
  281. package/tests/unit/schemaloader.spec.js +498 -0
  282. package/tests/unit/validator.spec.js +94 -0
  283. package/tests/unit/validators/ip-validator.spec.js +62 -0
@@ -0,0 +1,91 @@
1
+ import { JSONEditor } from '../../../src/core'
2
+
3
+ const fixture = [
4
+ {
5
+ title: 'Table Editor Test',
6
+ schema: {
7
+ type: 'array',
8
+ format: 'table',
9
+ items: {
10
+ type: ['boolean', 'string', 'null', 'number']
11
+ }
12
+ },
13
+ value: [false]
14
+ },
15
+ {
16
+ title: 'Table Editor Test',
17
+ schema: {
18
+ type: 'array',
19
+ format: 'table',
20
+ items: {
21
+ type: ['boolean', 'string', 'null', 'number']
22
+ }
23
+ },
24
+ value: ['']
25
+ },
26
+ {
27
+ title: 'Table Editor Test',
28
+ schema: {
29
+ type: 'array',
30
+ format: 'table',
31
+ items: {
32
+ type: ['boolean', 'string', 'null', 'number']
33
+ }
34
+ },
35
+ value: [0]
36
+ },
37
+ {
38
+ title: 'Table Editor Test',
39
+ schema: {
40
+ type: 'array',
41
+ format: 'table',
42
+ items: {
43
+ type: 'string'
44
+ }
45
+ },
46
+ options: {
47
+ use_default_values: false
48
+ },
49
+ value: ['']
50
+ },
51
+ {
52
+ title: 'Table Editor Test',
53
+ schema: {
54
+ type: 'array',
55
+ format: 'table',
56
+ items: {
57
+ type: 'number'
58
+ }
59
+ },
60
+ options: {
61
+ use_default_values: false
62
+ },
63
+ value: [0]
64
+ }
65
+ ]
66
+
67
+ describe('Table Editor', () => {
68
+ let element
69
+ let editor
70
+
71
+ beforeEach(() => {
72
+ document.body.insertAdjacentHTML(
73
+ 'afterbegin',
74
+ '<div id="fixture"></div>')
75
+ element = document.getElementById('fixture')
76
+ })
77
+
78
+ afterEach(() => {
79
+ editor.destroy()
80
+ })
81
+
82
+ fixture.forEach(spec => {
83
+ it(spec.title, () => {
84
+ editor = new JSONEditor(element, {
85
+ schema: spec.schema
86
+ })
87
+ editor.setValue(spec.value);
88
+ expect(editor.getValue()).toEqual(spec.value)
89
+ })
90
+ })
91
+ })
@@ -0,0 +1,35 @@
1
+ # Unit Tests
2
+
3
+ ## Introduction
4
+
5
+ Unit tests are implemented using the [Jasmine](https://jasmine.github.io/) framework.
6
+
7
+ ## Karma Test Runner
8
+
9
+ ### Running
10
+
11
+ You can continuously watch and debug unit tests using:
12
+
13
+ ```bash
14
+ npm run test
15
+ ```
16
+
17
+ The browser will start and run each unit test in a random sequence, showing the result. If tests are target code is edited, the code will be recompiled and the tests re-run.
18
+
19
+ ### Debugging
20
+
21
+ To debug tests click the debug button in the browser - this will allow you to select each test to run in debug mode.
22
+
23
+ You can set breakpoints on the original code by:
24
+
25
+ 1. Opening Chrome developer tools
26
+ 2. Opening the Sources tab
27
+ 3. Expanding the Webpack://./tests/unit/ hierarchy to display your test file
28
+ 4. Clicking on the line number to place the breakpoint
29
+
30
+ ## File hierarchy
31
+
32
+ Test files should have the same name as their target files but ending with `.spec.js` instead of `.js` and their subpath relative to this directory should be the same as the target file's relative to the `./src/` directory.
33
+
34
+ So a file testing `/src/editors/validator.js` should have the path `./tests/unit/editors/validator.spec.js`
35
+
@@ -0,0 +1,498 @@
1
+ /* global describe beforeEach it expect */
2
+
3
+ import { SchemaLoader } from '../../src/schemaloader'
4
+ import { createFakeServer } from 'sinon'
5
+
6
+ describe('SchemaLoader', () => {
7
+ let loader
8
+ let fileBase
9
+ let fetchUrl
10
+
11
+ describe('when no external ref', () => {
12
+ beforeEach(() => {
13
+ fetchUrl =
14
+ document.location.origin + document.location.pathname.toString()
15
+ loader = new SchemaLoader()
16
+ fileBase = loader._getFileBase(document.location.toString())
17
+ })
18
+
19
+ it('should create a loader', () => {
20
+ expect(loader).toBeTruthy()
21
+ })
22
+
23
+ it('load schema without $ref', () => {
24
+ const schema = {
25
+ type: 'object',
26
+ properties: {
27
+ name: { type: 'string' }
28
+ }
29
+ }
30
+ loader.load(schema, schema => {}, fetchUrl, fileBase)
31
+ const urls = Object.keys(loader.refs)
32
+ expect(urls.length).toEqual(0)
33
+ })
34
+
35
+ it('load schema with $ref', () => {
36
+ const schema = {
37
+ definitions: {
38
+ name: {
39
+ type: 'string',
40
+ minLength: 4
41
+ }
42
+ },
43
+ type: 'object',
44
+ properties: {
45
+ fname: { $ref: '#/definitions/name' },
46
+ lname: { $ref: '#/definitions/name' }
47
+ }
48
+ }
49
+ loader.load(schema, schema => {}, fetchUrl, fileBase)
50
+ const urls = Object.keys(loader.refs)
51
+ expect(urls.length).toEqual(1)
52
+ })
53
+
54
+ it('load schema with urn: $ref', () => {
55
+ const schema = {
56
+ definitions: {
57
+ fname: {
58
+ id: 'urn:fname',
59
+ type: 'string',
60
+ default: 'John',
61
+ minLength: 4
62
+ },
63
+ lname: {
64
+ $id: 'urn:lname',
65
+ type: 'string',
66
+ default: 'Doe',
67
+ minLength: 4
68
+ }
69
+ },
70
+ type: 'object',
71
+ properties: {
72
+ fname: { $ref: 'urn:fname' },
73
+ lname: { $ref: 'urn:lname' }
74
+ }
75
+ }
76
+ loader.load(schema, schema => {}, fetchUrl, fileBase)
77
+ const urls = Object.keys(loader.refs)
78
+ expect(urls.length).toEqual(4)
79
+ })
80
+ })
81
+
82
+ describe('when external absolute ref exists', () => {
83
+ it('should set oprion { ajax: true }', done => {
84
+ const response = {
85
+ type: 'string',
86
+ minLength: 4
87
+ }
88
+ const server = createFakeServer()
89
+ server.autoRespond = true
90
+ window.XMLHttpRequest = server.xhr
91
+ server.respondWith('/string.json', [
92
+ 200,
93
+ { 'Content-Type': 'application/json' },
94
+ JSON.stringify(response)
95
+ ])
96
+ fetchUrl =
97
+ document.location.origin + document.location.pathname.toString()
98
+ loader = new SchemaLoader({ ajax: true })
99
+ fileBase = loader._getFileBase(document.location.toString())
100
+
101
+ const schema = {
102
+ type: 'object',
103
+ properties: {
104
+ fname: { $ref: '/string.json' },
105
+ lname: { $ref: '/string.json' }
106
+ }
107
+ }
108
+ loader.load(
109
+ schema,
110
+ schema => {
111
+ const urls = Object.keys(loader.refs)
112
+ expect(urls.length).toEqual(1)
113
+ done()
114
+ server.restore()
115
+ },
116
+ fetchUrl,
117
+ fileBase
118
+ )
119
+ })
120
+ })
121
+
122
+ describe('when external relative $ref exists', () => {
123
+ it('should set oprion { ajax: true }', done => {
124
+ const response = {
125
+ type: 'string',
126
+ minLength: 4
127
+ }
128
+ const server = createFakeServer()
129
+ server.autoRespond = true
130
+ window.XMLHttpRequest = server.xhr
131
+ server.respondWith('/string.json', [
132
+ 200,
133
+ { 'Content-Type': 'application/json' },
134
+ JSON.stringify(response)
135
+ ])
136
+ fetchUrl =
137
+ document.location.origin + document.location.pathname.toString()
138
+ loader = new SchemaLoader({ ajax: true })
139
+ fileBase = loader._getFileBase(document.location.toString())
140
+
141
+ const schema = {
142
+ type: 'object',
143
+ properties: {
144
+ fname: { $ref: 'string.json' },
145
+ lname: { $ref: 'string.json' }
146
+ }
147
+ }
148
+ loader.load(
149
+ schema,
150
+ schema => {
151
+ const urls = Object.keys(loader.refs)
152
+ expect(urls.length).toEqual(1)
153
+ done()
154
+ server.restore()
155
+ },
156
+ fetchUrl,
157
+ fileBase
158
+ )
159
+ })
160
+ })
161
+
162
+ describe('when external absolute-to-relative $ref exists', () => {
163
+ it('can get refs recursively', done => {
164
+ const schema1 = {
165
+ type: 'object',
166
+ properties: {
167
+ fname: { $ref: '/schema/main.json' },
168
+ lname: { $ref: '/schema/main.json' }
169
+ }
170
+ }
171
+ const schema2 = {
172
+ $ref: 'registry/sub.json'
173
+ }
174
+ const schema3 = {
175
+ type: 'string',
176
+ minLength: 4
177
+ }
178
+ const server = createFakeServer()
179
+ server.autoRespond = true
180
+ window.XMLHttpRequest = server.xhr
181
+ server.respondWith('/schema/main.json', [
182
+ 200,
183
+ { 'Content-Type': 'application/json' },
184
+ JSON.stringify(schema2)
185
+ ])
186
+ server.respondWith('/schema/registry/sub.json', [
187
+ 200,
188
+ { 'Content-Type': 'application/json' },
189
+ JSON.stringify(schema3)
190
+ ])
191
+ fetchUrl =
192
+ document.location.origin + document.location.pathname.toString()
193
+ loader = new SchemaLoader({ ajax: true })
194
+ fileBase = loader._getFileBase(document.location.toString())
195
+ loader.load(
196
+ schema1,
197
+ schema => {
198
+ expect(Object.keys(loader.refs).length).toBe(2)
199
+ done()
200
+ server.restore()
201
+ },
202
+ fetchUrl,
203
+ fileBase
204
+ )
205
+ })
206
+ })
207
+
208
+ describe('when external relative-to-relative $ref exists', () => {
209
+ it('can get refs recursively', done => {
210
+ const schema1 = {
211
+ type: 'object',
212
+ properties: {
213
+ fname: { $ref: 'schema/main.json' },
214
+ lname: { $ref: 'schema/main.json' }
215
+ }
216
+ }
217
+ const schema2 = {
218
+ $ref: 'registry/sub.json'
219
+ }
220
+ const schema3 = {
221
+ type: 'string',
222
+ minLength: 4
223
+ }
224
+ const server = createFakeServer()
225
+ server.autoRespond = true
226
+ window.XMLHttpRequest = server.xhr
227
+ server.respondWith('/schema/main.json', [
228
+ 200,
229
+ { 'Content-Type': 'application/json' },
230
+ JSON.stringify(schema2)
231
+ ])
232
+ server.respondWith('/schema/registry/sub.json', [
233
+ 200,
234
+ { 'Content-Type': 'application/json' },
235
+ JSON.stringify(schema3)
236
+ ])
237
+ fetchUrl =
238
+ document.location.origin + document.location.pathname.toString()
239
+ loader = new SchemaLoader({ ajax: true })
240
+ fileBase = loader._getFileBase(document.location.toString())
241
+ loader.load(
242
+ schema1,
243
+ schema => {
244
+ console.log(loader.refs)
245
+ expect(Object.keys(loader.refs).length).toBe(2)
246
+ done()
247
+ server.restore()
248
+ },
249
+ fetchUrl,
250
+ fileBase
251
+ )
252
+ })
253
+ })
254
+
255
+ describe('when external absolute $ref with json pointer exists', () => {
256
+ it('can get refs', done => {
257
+ const schema1 = {
258
+ type: 'object',
259
+ properties: {
260
+ 'test-1': { $ref: '/common.schema.json#' },
261
+ 'test-2': { $ref: '/common.schema.json#/definitions/known-product' },
262
+ 'test-3': {
263
+ type: 'object',
264
+ properties: {
265
+ Product: { $ref: '/common.schema.json#/definitions/known-product' }
266
+ }
267
+ }
268
+ }
269
+ }
270
+ const schema2 = {
271
+ definitions: {
272
+ 'known-product': {
273
+ title: 'product',
274
+ type: 'string',
275
+ enum: [ 'power', 'hydrogen', 'heat' ]
276
+ }
277
+ },
278
+ title: 'test',
279
+ properties: {
280
+ customer: { type: 'string' },
281
+ product: { $ref: '#/definitions/known-product' }
282
+ }
283
+ }
284
+ const server = createFakeServer()
285
+ server.autoRespond = true
286
+ window.XMLHttpRequest = server.xhr
287
+ server.respondWith('/common.schema.json', [
288
+ 200,
289
+ { 'Content-Type': 'application/json' },
290
+ JSON.stringify(schema2)
291
+ ])
292
+ fetchUrl =
293
+ document.location.origin + document.location.pathname.toString()
294
+ loader = new SchemaLoader({ ajax: true })
295
+ fileBase = loader._getFileBase(document.location.toString())
296
+ loader.load(
297
+ schema1,
298
+ schema => {
299
+ console.log(loader.refs)
300
+ expect(Object.keys(loader.refs).length).toBe(2)
301
+ done()
302
+ server.restore()
303
+ },
304
+ fetchUrl,
305
+ fileBase
306
+ )
307
+ })
308
+ })
309
+
310
+ describe('when external relative $ref with json pointer exists', () => {
311
+ it('can get refs', done => {
312
+ const schema1 = {
313
+ type: 'object',
314
+ properties: {
315
+ 'test-1': { $ref: 'common.schema.json#' },
316
+ 'test-2': { $ref: 'common.schema.json#/definitions/known-product' },
317
+ 'test-3': {
318
+ type: 'object',
319
+ properties: {
320
+ Product: { $ref: 'common.schema.json#/definitions/known-product' }
321
+ }
322
+ }
323
+ }
324
+ }
325
+ const schema2 = {
326
+ definitions: {
327
+ 'known-product': {
328
+ title: 'product',
329
+ type: 'string',
330
+ enum: [ 'power', 'hydrogen', 'heat' ]
331
+ }
332
+ },
333
+ title: 'test',
334
+ properties: {
335
+ customer: { type: 'string' },
336
+ product: { $ref: '#/definitions/known-product' }
337
+ }
338
+ }
339
+ const server = createFakeServer()
340
+ server.autoRespond = true
341
+ window.XMLHttpRequest = server.xhr
342
+ server.respondWith('/common.schema.json', [
343
+ 200,
344
+ { 'Content-Type': 'application/json' },
345
+ JSON.stringify(schema2)
346
+ ])
347
+ fetchUrl =
348
+ document.location.origin + document.location.pathname.toString()
349
+ loader = new SchemaLoader({ ajax: true })
350
+ fileBase = loader._getFileBase(document.location.toString())
351
+ loader.load(
352
+ schema1,
353
+ schema => {
354
+ console.log(loader.refs)
355
+ expect(Object.keys(loader.refs).length).toBe(2)
356
+ done()
357
+ server.restore()
358
+ },
359
+ fetchUrl,
360
+ fileBase
361
+ )
362
+ })
363
+ })
364
+
365
+ describe('when external ref exists with json pointer', () => {
366
+ it('should get ref and resolve json pointer', done => {
367
+ const response = {
368
+ definitions: {
369
+ fruits: {
370
+ enum: [
371
+ 'apple',
372
+ 'banana',
373
+ 'cherry'
374
+ ]
375
+ }
376
+ }
377
+ }
378
+ const server = createFakeServer()
379
+ server.autoRespond = true
380
+ window.XMLHttpRequest = server.xhr
381
+ server.respondWith([
382
+ 200,
383
+ { 'Content-Type': 'application/json' },
384
+ JSON.stringify(response)
385
+ ])
386
+ fetchUrl =
387
+ document.location.origin + document.location.pathname.toString()
388
+ loader = new SchemaLoader({ ajax: true })
389
+ fileBase = loader._getFileBase(document.location.toString())
390
+
391
+ const schema = {
392
+ type: 'object',
393
+ properties: {
394
+ fruits: { $ref: '/fruits.json#/definitions/fruits' }
395
+ }
396
+ }
397
+ loader.load(
398
+ schema,
399
+ schema => {
400
+ const urls = Object.keys(loader.refs)
401
+ expect(urls.length).toEqual(1)
402
+ expect(urls[0]).toEqual('/fruits.json#/definitions/fruits')
403
+ expect(loader.refs['/fruits.json#/definitions/fruits']).toEqual({ enum: ['apple', 'banana', 'cherry'] })
404
+ done()
405
+ server.restore()
406
+ },
407
+ fetchUrl,
408
+ fileBase
409
+ )
410
+ })
411
+ })
412
+
413
+ describe('when resolving undeclared URN $ref', () => {
414
+ it('can get refs recursively', done => {
415
+ const schema1 = {
416
+ type: 'object',
417
+ properties: {
418
+ fname: { $ref: 'urn:main' },
419
+ lname: { $ref: 'urn:sub' }
420
+ }
421
+ }
422
+ const schema2 = {
423
+ definitions: {
424
+ name: {
425
+ id: 'urn:main',
426
+ $ref: 'urn:sub'
427
+ }
428
+ }
429
+ }
430
+ const schema3 = {
431
+ definitions: {
432
+ name: {
433
+ $id: 'urn:sub',
434
+ type: 'string',
435
+ default: 'Waldo',
436
+ minLength: 4
437
+ }
438
+ }
439
+ }
440
+ loader = new SchemaLoader({ urn_resolver: (urn, callback) => {
441
+ if (urn === 'urn:main') {
442
+ callback(JSON.stringify(schema2))
443
+ return true
444
+ }
445
+ if (urn === 'urn:sub') {
446
+ callback(JSON.stringify(schema3))
447
+ return true
448
+ }
449
+ return false
450
+ }})
451
+ loader.load(
452
+ schema1,
453
+ schema => {
454
+ console.log(loader.refs_with_info)
455
+ expect(Object.keys(loader.refs).length).toBe(4)
456
+ done()
457
+ }
458
+ )
459
+ })
460
+ })
461
+
462
+ describe('when resolving undeclared URN $ref with fragment', () => {
463
+ it('can get refs recursively', done => {
464
+ const schema1 = {
465
+ type: 'object',
466
+ properties: {
467
+ fname: { $ref: 'urn:main#/definitions/name' },
468
+ lname: { $ref: 'urn:main#/definitions/name' }
469
+ }
470
+ }
471
+ const schema2 = {
472
+ definitions: {
473
+ name: {
474
+ $id: 'urn:main',
475
+ type: 'string',
476
+ default: 'Waldo',
477
+ minLength: 4
478
+ }
479
+ }
480
+ }
481
+ loader = new SchemaLoader({ urn_resolver: (urn, callback) => {
482
+ if (urn === 'urn:main') {
483
+ callback(JSON.stringify(schema2))
484
+ return true
485
+ }
486
+ return false
487
+ }})
488
+ loader.load(
489
+ schema1,
490
+ schema => {
491
+ console.log(loader.refs_with_info)
492
+ expect(Object.keys(loader.refs).length).toBe(2)
493
+ done()
494
+ }
495
+ )
496
+ })
497
+ })
498
+ })
@@ -0,0 +1,94 @@
1
+ /* global describe beforeAll beforeEach afterEach afterAll it expect */
2
+
3
+ import { JSONEditor } from '../../src/core'
4
+ import { Validator } from '../../src/validator'
5
+ import { defaults } from '../../src/defaults'
6
+ import fixture from '../fixtures/validation.json'
7
+ import fixtureString from '../fixtures/string.json'
8
+ import fixtureRecursive from '../fixtures/recursive.json'
9
+ import * as math from 'mathjs'
10
+ import { createFakeServer } from 'sinon'
11
+
12
+ describe('Validator', () => {
13
+ it('mathjs test', () => {
14
+ window.math = math
15
+ expect(window.math.mod(window.math.bignumber(1), window.math.bignumber(0.01))).toEqual(window.math.bignumber(0))
16
+ })
17
+ it('should exist', () => {
18
+ expect(Validator).toBeTruthy()
19
+ })
20
+
21
+ describe('Constructor()', () => {
22
+ it('should create a new object', () => {
23
+ const jsonEditor = {}
24
+ const schema = {}
25
+ const options = {}
26
+ const instance = new Validator(jsonEditor, schema, options, defaults)
27
+ expect(instance).toBeTruthy()
28
+ })
29
+ })
30
+ })
31
+
32
+ describe('Validation Test', () => {
33
+ let element
34
+ let server
35
+
36
+ beforeAll(() => {
37
+ server = createFakeServer()
38
+ server.autoRespond = true
39
+ window.XMLHttpRequest = server.xhr
40
+ const response = {
41
+ string: fixtureString,
42
+ recursive: fixtureRecursive
43
+ }
44
+ server.respondWith(/(\w+)\.json/, (xhr, name) => {
45
+ xhr.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(response[name]))
46
+ })
47
+ })
48
+
49
+ afterAll(() => {
50
+ server.restore()
51
+ })
52
+
53
+ beforeEach(() => {
54
+ window.math = math
55
+ document.body.insertAdjacentHTML(
56
+ 'afterbegin',
57
+ '<div id="fixture"></div>')
58
+ element = document.getElementById('fixture')
59
+ })
60
+
61
+ Object.entries(fixture).forEach(([subject, spec]) => {
62
+ describe(subject, () => {
63
+ let editor
64
+ beforeEach(() => {
65
+ editor = new JSONEditor(element, {
66
+ schema: spec.schema,
67
+ ajax: true
68
+ })
69
+ })
70
+
71
+ afterEach(() => {
72
+ editor.destroy()
73
+ })
74
+ spec.valid.forEach((v, i) => {
75
+ it(`valid data ${i + 1}`, (done) => {
76
+ editor.on('ready', () => {
77
+ const result = editor.validate(v)
78
+ expect(result.length).toBe(0)
79
+ done()
80
+ })
81
+ })
82
+ })
83
+ spec.invalid.forEach((v, i) => {
84
+ it(`invalid data ${i + 1}`, (done) => {
85
+ editor.on('ready', () => {
86
+ const result = editor.validate(v)
87
+ expect(result.length).toBeGreaterThan(0)
88
+ done()
89
+ })
90
+ })
91
+ })
92
+ })
93
+ })
94
+ })