Skip to content

Commit d7d7fae

Browse files
committed
Full project restructured.
1 parent 6fc4b73 commit d7d7fae

File tree

114 files changed

+69765
-1639
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+69765
-1639
lines changed

.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
node: true,
5+
es6: true
6+
},
7+
parser: 'babel-eslint',
8+
plugins: ['jsdoc'],
9+
extends: ['eslint:recommended', 'plugin:jsdoc/recommended'],
10+
rules: {
11+
'jsdoc/check-examples': 1,
12+
// Use only as guidance. Set to 0 before "git add".
13+
'jsdoc/check-indentation': 0,
14+
'jsdoc/check-syntax': 1,
15+
'jsdoc/match-description': 1,
16+
'jsdoc/require-description': 1,
17+
// Use only as guidance. Set to 0 before "git add".
18+
'jsdoc/require-description-complete-sentence': 0
19+
}
20+
};

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*.adoc
2+
node_modules
3+
.cache
4+
/serve
5+
/tests
6+
/yarn-error.log

.jsdoc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"source": {
3+
"include": [
4+
"./src",
5+
"./README.md"
6+
]
7+
},
8+
"opts": {
9+
"destination": "./docs",
10+
"recurse": true
11+
},
12+
"recurseDepth": 3,
13+
"plugins": [
14+
"plugins/markdown"
15+
]
16+
}

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

README.md

Lines changed: 141 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,160 @@
1-
# json-schema-forms
1+
# JsonSchemaForms
2+
23
A JavaScript tool that generates HTML forms from JSON Schemas.
34

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.
56

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.
78

89
## Usage
9-
[Yarn](https://yarnpkg.com/) is required in order to generate a bundled version of the script (using [Parcel](https://parceljs.org/)).
1010

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.
1212

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+
});
16125
```
17126

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
19130

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:
22132

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+
```
24136

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
26138

27139
```console
28-
$ yarn <dev|build>
140+
npm i json-schema-forms
29141
```
30142

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?
32153

33154
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
37155

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)._

_config.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

css/json-schema-forms.css

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
@charset "UTF-8";
2+
3+
body {
4+
background-color: #f5f6f7;
5+
}
6+
7+
.help-icon {
8+
color: grey;
9+
transition: 0.3s;
10+
opacity: 0.5;
11+
}
12+
13+
.help-icon:hover {
14+
color: #0275d8;
15+
opacity: 1;
16+
transition: 0.3s;
17+
cursor: default;
18+
}
19+
20+
.fas.fa-bullseye {
21+
color: #ff8b8b;
22+
font-size: 1.25rem;
23+
}
24+
25+
.fas.fa-asterisk {
26+
color: orange;
27+
font-size: 1.25rem;
28+
}
29+
30+
.fab.fa-sourcetree {
31+
color: #ef5350;
32+
font-size: 1.25rem;
33+
}
34+
35+
.btn,
36+
input.form-control,
37+
option,
38+
.dropdown-item {
39+
transition: 0.5s;
40+
}
41+
42+
.form-control {
43+
max-width: 300px;
44+
}
45+
46+
.btn[disabled],
47+
input.form-control[disabled],
48+
option[disabled],
49+
.dropdown-item[disabled] {
50+
transition: 0.5s;
51+
}
52+
53+
label.custom-control-label {
54+
cursor: pointer;
55+
}
56+
57+
.disabled {
58+
display: none;
59+
}
60+
61+
.container-fluid {
62+
max-width: var(--container-max-width);
63+
}
64+
65+
.icon {
66+
width: var(--icon-offset);
67+
min-width: var(--icon-offset);
68+
}
69+
70+
.primitive {
71+
padding-left: var(--icon-offset);
72+
}
73+
74+
:root {
75+
--icon-offset: 40px;
76+
--container-max-width: 900px;
77+
}

dist/json-schema-forms.min.js

Lines changed: 260 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)