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

@nobleai/rescript-fields

v1.0.0-beta.4

Published

Elaborate form validation system for Rescript

Downloads

400

Readme

Rescript Fields

Elaborate form validation for Rescript

Fields is a module for managing the update and validation of complex input. The singular module type Field.T describes the requirements for creating, modifying and validating an input to produce either an output value or an error. Using Rescript "Module Functors" Fields can be built that compose other Fields (producing a tree of Fields) allowing validation and change reduction at each level. An attempt was made to implement common Fields and Field Patterns, and those are a large body of the library. see FieldArray or FieldProduct.
Each field is represented by a Field module that declares the storage, validation, and update of a value type.

Installation

  • Add @nobleai/rescript-fields to your project as you like
  • Add @nobleai/rescript-fields to bs-dev-dependenciesin bsconfig.json
  • build -with-deps

Provided Fields

  • FieldIdentity
  • FieldParse
  • FieldArray
  • FieldSum (FieldEither)
  • FieldProduct (FieldVector)
  • FieldOpt

Live example

yarn install; yarn rescript build; yarn run examples:everything

Login Form Example


module FieldPassword = FieldParse.String.Field
module FieldUsername = FieldParse.String.Field

module InputString = {
  // Render a single input controlled by the form
  @react.component
  let make = (~label, ~form: Fields.Form.t<FieldParse.String.Field.t, FieldParse.String.Field.actions<()>>) => {
    <div>
      <label>{label->React.string}</label>
      <input
        // input comes from the form.field
        value={form.field->FieldParse.String.Field.input}
        // Changes are made by calling form.actions functions
        onChange={e => {
          let target = e->ReactEvent.Form.target
          target["value"]->form.actions.set
        }}
      />
      // Errors are taken from form.field
      {form.field->FieldParse.String.Field.printError->Option.map(React.string)->Option.or(React.null)}
    </div>
  }
}

// Declare the structure of your desired output type
// This is outside of Generic to make accessors more easily available
@deriving(accessors)
type structure<'a, 'b> = {
  username: 'b,
  password: 'a,
}

// Give fields a map from your output type to a generic container (tuple)
module Generic = {
  type structure<'a, 'b> = structure<'a, 'b>

  let order = (password, username)
  let fromTuple = ((password, username)) => {username, password}
}

// Combine the Generic and child Fields to create a product field
module Field = FieldProduct.Product2.Make(
  Generic,
  FieldUsername,
  FieldPassword,
)

// Create a runtime context that includes validations
let contextValidate: Field.context = {
  validate: ({username, password}) => {
    Ok()
    ->Result.guard(_ => username != password, "Username and password must be different")
    ->Promise.return
    // Simulate async
    ->Promise.delay(~ms=1000)
  },
  inner: {
    username: {validate: FieldParse.String.length(~min=2, ())},
    password: {validate: FieldParse.String.length(~min=6, ())},
  }
}

// Create a hook for running this field
module Form = UseField.Make(Field)

module Input = {
  // Render the inputs for a longin
  @react.component
  let make = (~form: Fields.Form.t<Field.t, Field.actions<()>>) => {
    // All composite fields will provide "split" to traverse into their children
    // These children will boe Form.t for each child field respectively
    let {username, password} = form->Field.split

    <div>
      <InputString label="Username" form=username />
      <InputString label="Password" form=password />
    </div>
  }
}

@react.component
let make = (~onSubmit) => {
  let form = Form.use(. 
    ~context=contextValidate,
    ~init=None,
    ~validateInit=false,
  )

  let handleSubmit = React.useMemo1( () => {
    (_) => form.field->Field.output->Option.map(onSubmit)->Option.void
  }, [form.field->Field.output])

  <form onSubmit={handleSubmit}>
    <div className="container">
      <h1>{"Login"->React.string}</h1> 
      <Input form />
      <button type_="submit"> {"Sign In"->React.string} </button>
    </div>
  </form>
}

TODO

  • debounced asyc validation
  • graphql async validation based on hook provided function
  • optimistic update chaging form twice?
  • Realign Polyvariant #A, #B, etc with sum 'a, 'b etc.