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

tcomb-builder

v5.0.0

Published

A declarative syntax for building Tcomb type and options objects

Downloads

9

Readme

CircleCI dependency status npm downloads

tcomb-builder

tcomb-builder is an immutable interface for tcomb-form

Install

npm install tcomb-builder tcomb-validation

Benefits

  1. Immutability

  2. Replaces deeply nested options objects in your codebase

  3. Yields a standard tcomb type and options object

Syntax

If you use tcomb-form, you probably have a lot of code that looks like this:

import tcomb from 'tcomb-form';

function validation(value) {
  return value.length > 0
      ? null
      : 'Please provide a value';
}

const FirstName = tcomb.refinement(
  tcomb.String,
  validation,
  'FirstName',
);

FirstName.getValidationErrorMessage = validation;

const options = {
  factory: function TextField(props)...,
  label: 'First Name',
};

// <tcomb.Form type={FirstName} options={options} />

This code has a few problems:

  1. Mutability makes the type hard to reuse.

    It is hard to reuse the FirstName type in another place that requires slightly modified validation, e.g. a max length restriction, because getValidationErrorMessage is set as a static function on the type.

  2. Mutability makes the options object hard to reuse.

    Creating another field, e.g. 'Last Name', would require copy-pasting or Object.assigning the options object.

  3. Nested options objects are hard to maintain.

    If the above field is placed inside a tcomb.struct, the options object is deeply nested. The more structs, the more nesting.

Thanks to its immutable design, tcomb-builder offers a cleaner syntax that encourages code-reuse.

import tcomb from 'tcomb-form';

import { BaseBuilder } from 'tcomb-builder';

function validation(value) {
  return value.length > 0
      ? null
      : 'Please provide a value';
}

const TextBuilder = new BaseBuilder()
  .setTypeAndValidate('Text', tcomb.String)
  .setValidation('LengthValidation', validation);

const firstName = TextBuilder.setLabel('First Name');

// <tcomb.Form type={firstName.getType()} options={firstName.getOptions()} />

This code is equivalent to the first example, with the following advantages:

  1. Immutability makes the type and options objects easy to reuse.

    Making a lastName field is as easy as TextBuilder.setLabel('Last Name'). Every .set method returns a new builder, so TextBuilder itself is never modified.

  2. Type and options objects are built together.

    When using structs, you don't have to worry about maintaining parallel type and options structures. tcomb-builder is one interface for all of tcomb, tcomb-validation, and tcomb-form. The parallel structures are maintained internally and accessed with the .getType and .getOptions methods.

Documentation

There is thorough documentation within the docs folder.

Start with the documentation on what this library exports (api.md) and the builder documentation (builder.md).

Background Reading

Example

Here is an example of a simple form. All primitives and widgets builders extend the BaseBuilder. Note that widgets are primitives that have a template set on them.

import { primitives, validators, widgets } from 'tcomb-builder';

const name = widgets.TextBuilder.setLabel('Name');

const dateOfBirth = widgets.TextBuilder
  .setValidation('BirthdateValidation', validators.date.birthdate)
  .setLabel('Date of Birth');

const occupation = widgets.TextBuilder
  .setLabel('Occupation')
  .makeOptional();

const bananaStand = widgets.CheckboxBuilder
  .setLabel('Worked at banana stand');
const chickenDance = widgets.CheckboxBuilder
  .setLabel('Does chicken dance');
const hugeMistake = widgets.CheckboxBuilder
  .setLabel('Has made a huge mistake');
const none = widgets.CheckboxBuilder
  .setLabel('None of the above');

const noneKey = 'isNone';
const crossValidation = validators.combine([
  validators.checkbox.noneOfAbove(noneKey),
  validators.checkbox.minMax({ min: 1, max: 2 }),
]);

const foodGroup = widgets.CheckboxGroupBuilder
  .setValidation('SelectOneOrTwo', crossValidation)
  .setField('bananaStand', bananaStand)
  .setField('chickenDance', chickenDance)
  .setField('hugeMistake', hugeMistake)
  .setLabel('Character Traits')
  .setField(noneKey, none);

export default widgets.StructBuilder
  .setField('name', name)
  .setField('dateOfBirth', dateOfBirth)
  .setField('occupation', occupation)
  .setField('foodGroup', foodGroup)
  .setColumns(2);

Once exported, the page builder provides us with its tcomb type and options blob, which can be passed directly to tcomb-form without modification.

const type = InvestorInformationBuilder.getType();
const options = InvestorInformationBuilder.getOptions();
<tcomb.Form type={type} options={options} />

The end result, once tcomb template factories have been created for the underlying primitive types—text field, drop down, checkbox, and struct template primitives in this example—is that the form will render exactly as we expect it to.

Builders

Builders are composable, and this library uses three terms to describe them:

  1. Primitives: Builders that define a single input (e.g. a text field) or a structure (e.g. struct); primitive builders are found in the primitives folder.

  2. Widgets: Primitives that have a template provider already set on them. They are found in the widgets folder.

  3. Forms: Builders that define an entire page (e.g. Credit Card Information); examples are found in the examples folder

Setting Templates

When you first set up tcomb-builder, it is recommended to create an instance of LazyTemplateInterface, which defines a mapping of component types to templates. With this interface and the setLazyTemplateFactory function, your templates are defined in one place (the LazyTemplateInterface), which makes refactoring easier.

If you have a Node backend, you can use the same builders on both the client and server. The builders on the server will never realize the lazy templates, so you don't have to require React as a dependency on the server. See the API docs for more details.