|
1 | | -# json-schema-forms |
| 1 | +# JsonSchemaForms |
| 2 | + |
2 | 3 | A JavaScript tool that generates HTML forms from JSON Schemas. |
3 | 4 |
|
4 | | -This implementation accepts schemas following the [JSON Schema Draft 2019-09 specification](https://json-schema.org/), and provides [Bootstrap](https://getbootstrap.com/) support (4.4+) to organize and decorate the layout. |
| 5 | +This implementation accepts schemas following the [JSON Schema Draft 2019-09 specification](https://json-schema.org/), and provides [Bootstrap](https://getbootstrap.com/) (4.5+) and [Font Awesome](https://fontawesome.com/) (5.13+) to organize and decorate the layout. While these libraries are not required, they are highly recommended to get the form properly rendered by the browser. |
5 | 6 |
|
6 | | -`json-schema-forms` makes use of the [JSON Schema $Ref Parser](https://github.com/APIDevTools/json-schema-ref-parser) in order to resolve and dereference the relevant JSON Schemas. |
| 7 | +JsonSchemaForms makes use of the [JSON Schema \$Ref Parser](https://github.com/APIDevTools/json-schema-ref-parser) in order to resolve and dereference the schemas to be processed. |
7 | 8 |
|
8 | 9 | ## Usage |
9 | | -[Yarn](https://yarnpkg.com/) is required in order to generate a bundled version of the script (using [Parcel](https://parceljs.org/)). |
10 | 10 |
|
11 | | -Simply download and install both the `json-schema-forms` and the required packages with the following commands: |
| 11 | +The JsonSchemaForms module provides a `build()` function that performs the whole process of analyzing a JSON Schema and generating the DOM and internal representation of the form. Have a look at the [`JsonSchemaForms.build()` API](docs/module-JsonSchemaForms.html) for usage details. |
12 | 12 |
|
13 | | -```console |
14 | | -$ git clone https://github.com/hblanko/json-schema-forms.git |
15 | | -$ yarn setup |
| 13 | +### Through CDN |
| 14 | + |
| 15 | +The quickly and easy way to make JsonSchemaForms available to your scripts, by adding a few CDN links to your HTML code. |
| 16 | + |
| 17 | +JsonSchemaForms provides a script and style sheet that can be linked adding the following tags: |
| 18 | + |
| 19 | +```html |
| 20 | +<link |
| 21 | + rel="stylesheet" |
| 22 | + type="text/css" |
| 23 | + href="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/css/json-schema-forms.min.css" |
| 24 | +/> |
| 25 | +``` |
| 26 | + |
| 27 | +```html |
| 28 | +<script src="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/dist/json-schema-forms.min.js"></script> |
| 29 | +``` |
| 30 | + |
| 31 | +On top of that, as mentioned before, Bootstrap and Font Awesome allow for a nice-looking result, so their CDN links are recommended to be included, too. |
| 32 | + |
| 33 | +Hence, the full picture of a barebone `example.html` using JsonSchemaForms CDN ends up looking like this: |
| 34 | + |
| 35 | +```html |
| 36 | +<!DOCTYPE html> |
| 37 | +<html lang="en"> |
| 38 | + <head> |
| 39 | + <meta charset="utf-8" /> |
| 40 | + <!-- Bootstrap-related style --> |
| 41 | + <meta |
| 42 | + name="viewport" |
| 43 | + content="width=device-width, initial-scale=1, shrink-to-fit=no" |
| 44 | + /> |
| 45 | + <link |
| 46 | + rel="stylesheet" |
| 47 | + href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" |
| 48 | + integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" |
| 49 | + crossorigin="anonymous" |
| 50 | + /> |
| 51 | + <!-- JsonSchemaForms style sheet --> |
| 52 | + <link |
| 53 | + rel="stylesheet" |
| 54 | + type="text/css" |
| 55 | + href="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/css/json-schema-forms.min.css" |
| 56 | + /> |
| 57 | + </head> |
| 58 | + |
| 59 | + <body> |
| 60 | + <!-- Bootstrap and Font Awesome scripts --> |
| 61 | + <script |
| 62 | + src="https://code.jquery.com/jquery-3.5.1.slim.min.js" |
| 63 | + integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" |
| 64 | + crossorigin="anonymous" |
| 65 | + ></script> |
| 66 | + <script |
| 67 | + src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" |
| 68 | + integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" |
| 69 | + crossorigin="anonymous" |
| 70 | + ></script> |
| 71 | + <script |
| 72 | + src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" |
| 73 | + integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" |
| 74 | + crossorigin="anonymous" |
| 75 | + ></script> |
| 76 | + <script |
| 77 | + src="https://kit.fontawesome.com/64968f57be.js" |
| 78 | + crossorigin="anonymous" |
| 79 | + ></script> |
| 80 | + <!-- JsonSchemaForms script --> |
| 81 | + <script src="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/dist/json-schema-forms.min.js"></script> |
| 82 | + <!-- Your script --> |
| 83 | + <script src="./example.js"></script> |
| 84 | + |
| 85 | + <!-- Some containers to use by your script --> |
| 86 | + <div id="form-container"></div> |
| 87 | + <pre id="json-result"></pre> |
| 88 | + </body> |
| 89 | +</html> |
| 90 | +``` |
| 91 | + |
| 92 | +Now, let us define our basic `example.js` script. |
| 93 | + |
| 94 | +```javascript |
| 95 | +// You've got two options in order to plug your JSON Schema: |
| 96 | +// 1. Provide a URL to a JSON Schema. |
| 97 | +// 2. Directly assign an object following the JSON Schema format. |
| 98 | + |
| 99 | +// const schema = 'http://landarltracker.com/schemas/test.json'; |
| 100 | +const schema = { |
| 101 | + title: 'The Root Form Element', |
| 102 | + description: 'Easy, right?', |
| 103 | + type: 'string', |
| 104 | +}; |
| 105 | + |
| 106 | +// Also, you can define the form behavior on submission, e.g.: |
| 107 | +const submitCallback = (rootFormElement) => { |
| 108 | + // Show the resulting JSON instance in your page. |
| 109 | + document.getElementById('json-result').innerText = JSON.stringify( |
| 110 | + rootFormElement.getInstance(), |
| 111 | + null, |
| 112 | + 2 |
| 113 | + ); |
| 114 | + // (For testing purposes, return false to prevent automatic redirect.) |
| 115 | + return false; |
| 116 | +}; |
| 117 | + |
| 118 | +// Finally, get your form... |
| 119 | +const jsonSchemaForm = JsonSchemaForms.build(schema, submitCallback); |
| 120 | + |
| 121 | +// ... and attach it somewhere to your page. |
| 122 | +window.addEventListener('load', () => { |
| 123 | + document.getElementById('form-container').appendChild(jsonSchemaForm); |
| 124 | +}); |
16 | 125 | ``` |
17 | 126 |
|
18 | | -The `package.json` document comes prepared with two scripts: |
| 127 | +This example works directly out of the box. Feel free to copy, paste, and play around with it! |
| 128 | + |
| 129 | +### Custom bundle |
19 | 130 |
|
20 | | -- `dev`: Runs on development mode, initializing a live server that refreshes automatically on script changes. |
21 | | -- `build`: Generates the bundled script together with the other output files. |
| 131 | +If you prefer to import it into your own project, use your favorite package manager to install it: |
22 | 132 |
|
23 | | -The options for each of these scripts can be adapted to your needs inside the `package.json` document. |
| 133 | +```console |
| 134 | +yarn add json-schema-forms |
| 135 | +``` |
24 | 136 |
|
25 | | -Given so, you can change the `index.js` script to suit your needs and just run the following to produce the expected output: |
| 137 | +or |
26 | 138 |
|
27 | 139 | ```console |
28 | | -$ yarn <dev|build> |
| 140 | +npm i json-schema-forms |
29 | 141 | ``` |
30 | 142 |
|
31 | | -## Forthcoming |
| 143 | +And just make it available by including at the top of your script: |
| 144 | + |
| 145 | +```javascript |
| 146 | +import JsonSchemaForms from 'json-schema-forms'; |
| 147 | +``` |
| 148 | + |
| 149 | +Then, you can use it as shown in `example.js` through the `build()` function |
| 150 | +(check [the API docs](docs/module-JsonSchemaForms.html) for detailed information). |
| 151 | + |
| 152 | +## What's to come? |
32 | 153 |
|
33 | 154 | Base code is still under work, being several features not yet covered (but expected to be): |
34 | | - - The 'allOf' keyword |
35 | | - - A number of features from JSON Schema types, e.g. the 'contains' directive from the 'object' type |
36 | | - - General bugfixing |
37 | 155 |
|
38 | | -This script was initially conceived to be used in the framework of [the Cookbase Project](https://github.com/hblanko/cookbase). |
| 156 | +- Conditional in-place applicators. |
| 157 | +- Some child applicators (e.g. `patternProperties`) and validation keywords. |
| 158 | +- Aggregation logic yet to be implemented for several keywords. |
| 159 | + |
| 160 | +_JsonSchemaForms was initially conceived as a basis for a specialized version to be used in the framework of [the Cookbase Project](https://github.com/hblanko/cookbase)._ |
0 commit comments