npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-schema-forms

v0.2.0-alpha.2

Published

A pure JavaScript tool that generates HTML forms from JSON Schemas.

Downloads

54

Readme

JsonSchemaForms

A JavaScript tool that generates HTML forms from JSON Schemas.

This implementation accepts schemas following the JSON Schema Draft 2019-09 specification, and provides Bootstrap (4.5+) and Font Awesome (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.

JsonSchemaForms makes use of the JSON Schema $Ref Parser in order to resolve and dereference the schemas to be processed.

Usage

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 for usage details.

Through CDN

The quickly and easy way to make JsonSchemaForms available to your scripts, by adding a few CDN links to your HTML code.

JsonSchemaForms provides a script and style sheet that can be linked adding the following tags:

<link
  rel="stylesheet"
  type="text/css"
  href="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/css/json-schema-forms.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/dist/json-schema-forms.min.js"></script>

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.

Hence, the full picture of a barebone example.html using JsonSchemaForms CDN ends up looking like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <!-- Bootstrap-related style -->
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
      integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
      crossorigin="anonymous"
    />
    <!-- JsonSchemaForms style sheet -->
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/css/json-schema-forms.min.css"
    />
  </head>

  <body>
    <!-- Bootstrap and Font Awesome scripts -->
    <script
      src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
      integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
      integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
      integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://kit.fontawesome.com/64968f57be.js"
      crossorigin="anonymous"
    ></script>
    <!-- JsonSchemaForms script -->
    <script src="https://cdn.jsdelivr.net/npm/json-schema-forms@latest/dist/json-schema-forms.min.js"></script>
    <!-- Your script -->
    <script src="./example.js"></script>

    <!-- Some containers to use by your script -->
    <div id="form-container"></div>
    <pre id="json-result"></pre>
  </body>
</html>

Now, let us define our basic example.js script.

// You've got two options in order to plug your JSON Schema:
//   1. Provide a URL to a JSON Schema.
//   2. Directly assign an object following the JSON Schema format.

// const schema = 'http://landarltracker.com/schemas/test.json';
const schema = {
  title: 'The Root Form Element',
  description: 'Easy, right?',
  type: 'string',
};

// Also, you can define the form behavior on submission, e.g.:
const submitCallback = (rootFormElement) => {
  // Show the resulting JSON instance in your page.
  document.getElementById('json-result').innerText = JSON.stringify(
    rootFormElement.getInstance(),
    null,
    2
  );
  // (For testing purposes, return false to prevent automatic redirect.)
  return false;
};

// Finally, get your form...
const jsonSchemaForm = JsonSchemaForms.build(schema, submitCallback);

// ... and attach it somewhere to your page.
window.addEventListener('load', () => {
  document.getElementById('form-container').appendChild(jsonSchemaForm);
});

This example works directly out of the box. Feel free to copy, paste, and play around with it!

Custom bundle

If you prefer to import it into your own project, use your favorite package manager to install it:

yarn add json-schema-forms

or

npm i json-schema-forms

And just make it available by including at the top of your script:

import JsonSchemaForms from 'json-schema-forms';

Then, you can use it as shown in example.js through the build() function (check the API docs for detailed information).

What's to come?

Base code is still under work, being several features not yet covered (but expected to be):

  • Conditional in-place applicators.
  • Some child applicators (e.g. patternProperties) and validation keywords.
  • Aggregation logic yet to be implemented for several keywords.

JsonSchemaForms was initially conceived as a basis for a specialized version to be used in the framework of the Cookbase Project.