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

react-jsonschema-form-async

v0.2.0

Published

An alternative Form for Mozilla's JSON Schema Form library with async validation

Downloads

15

Readme

React JSON Schema Async Form

A wrapper for react-jsonschema-form which add asynchronous validation capabilities.

Build Status Dependencies Dev Dependencies

Motivation behind this library

react-jsonschema-form is an excellent library, but for many reason it doesn't cover the async validation.

There's work in progress about this (see here) so maybe in future this library will not be useful anymore.

Know/unknow issues and TODO

I'm pretty sure this library does not cover every usecase:

  • [x] supporting basic async validation
  • [x] should onSubmit be called with a server response instead of formData submitted?
  • [ ] testing with onChange
  • [ ] testing with liveValidate
  • [ ] testing with noValidate
  • [x] testing with onError
  • [ ] different endpoints for validation and submit? Make any sense?

Contributions are welcome!

How to use

Installation

npm install --save react-jsonschema-form-async

You will also need react-jsonschema-form as peer dependency.

Example

Live example at https://bopen.github.io/react-jsonschema-form-async


import Form from 'react-jsonschema-form-async';

const App = (props) => (
  <Form
    schema={yourSchema}
    onAsyncValidate={asyncValidate}
    onSubmit={handleSubmit}
    onError={handleError}
  />    
)

Note that:

  • you will import and use the Form component from react-jsonschema-form-async instead of the one from react-jsonschema-form.
  • the onSubmit prop has a similar format/signature of the original library, but probably you will not use it for call an API to store data on a backend (see below). The object passed can also contains a result entry, which can be the whole success response.
  • the onError receive two parameters: the (standard) errors array and the parameter and the reject reason (commonly an exception in case of promises).
  • onAsyncValidate is the only new props you need to care about.

The onAsyncValidate function is called when the form is submitted, so it receives the formData object as parameter.

It must return a Promise or promise-like object:

  • if the promise resolves, onSubmit is called (if provided).
  • if the promise is rejected, is must contains a JSON structure where error messages are stored and onError in called (is provided).

An example of onAsyncValidate:


const asyncValidate = (formData) => {
  return api.post('/api/v1/create', formData);
}

In case of errors this API post should return a failure JSON response with an errors entry (by default). An example:

{
  "errors": {
    "username": "The username is already used",
    "birthday": "The date is invalid"
  }
}

An onSubmit implementation can be:

const onSubmit = ({formData, result}) => {
  // formData works the same as in react-jsonschema-form
  // result depends on your Promise implementation, commonly it can be the whole JSON response
}

An onError implementation can be:

const onError = (errors, err formData) => {
  // errors works the same as in react-jsonschema-form (array of errors)
  // err depends on your Promise implementation, commonly it is an exception passed when rejecting
  // formData is the original field set sent to onAsyncValidate
}

Note: onError is called also when default client side validations fails, so latter parameters are not always present.

Custom JSON response

If you can't control your JSON response format and your error messages are stored differently, you can change the default errorsAccessor props (default is "errors").

For example:

  <Form
    schema={yourSchema}
    onAsyncValidate={asyncValidate}
    onSubmit={handleSubmit}
    errorsAccessor="json[1].errorMessages"
  />

See lodash get syntax.

About onSubmit usage

If you are interacting with a remote server, validation is commonly performed during the attempt to store data to the server so onAsyncValidate is probably the only prop you need to interact with the server. For this reason the onSubmit prop is less important, you can use it for updating local state, if needed.