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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@vtaits/form-schema

v3.1.1

Published

Serialization and parsing form values by schema

Downloads

218

Readme

NPM dependencies status

@vtaits/form-schema

A set of utilities for easy work with form values and errors:

  • Serialize form values before submit by schema
  • Parse initial values of form by schema
  • Map submission errors by schema

Installation

yarn add @vtaits/form-schema

or

npm install @vtaits/form-schema --save

or

bun add @vtaits/form-schema

Concept

There are next entities:

  • names - array of field names, required for serialization and parsing of initial values;

  • getFieldSchema - function that returns full schema of field by name (it can contain placeholder for input, options for select, label, help text etc.);

  • getFieldType - function that returns type declaration of field by full schema.

Type declaration

Type declaration is an object with next params:

  • serializer - function for serializing form values before submit. E.g. local value of select can be an object { value, label }, but only value should be submitted. Parameters:

    • value - current value of the field;
    • values - all values of form;
    • name - name of current field;
    • fieldSchema - full schema of current field;
    • getFieldSchema - see above;
    • getFieldType - see above;
    • parents - stack of parent fields above current field with runtime values.

    Should return OBJECT of values or Promise with object of values (can be async). By default returns

    {
      [name]: values[name],
    }
  • parser - function for parsing initial values of form before initialize. Parameters:

    • value - current value of the field;
    • values - all values of the form;
    • name - name of current field;
    • fieldSchema - full schema of current field;
    • getFieldSchema - see above;
    • getFieldType - see above;
    • parents - stack of parent fields above current field with raw values.

    Should return OBJECT of values or Promise with object of values (can be async). By default returns

    {
      [name]: values[name],
    }
  • validatorBeforeSubmit - function for collecting validation errors of form before submit. Receives next parameters:

    • setError - a function for setting errors;
    • value - current value of the field;
    • values - all values of the form;
    • name - name of current field;
    • fieldSchema - full schema of current field;
    • getFieldSchema - see above;
    • getFieldType - see above;
    • parents - stack of parent fields above current field with runtime values.

    Example:

    {
      validateBeforeSubmit: ({
        setError,
        values,
        name,
        fieldSchema: { required },
        parents,
      }) => {
        if (required && !values[name]) {
          setError(name, parents, 'This field is required');
        }
      },
    }
  • errorsSetter - function for mapping errors of field from backend format to format of field. Receives next parameters:

    • setError - a function for setting errors;
    • setCurrentError - a function to set error on the current field;
    • errors - errors object;
    • name - name of current field;
    • fieldSchema - full schema of current field;
    • getFieldSchema - see above;
    • getFieldType - see above;
    • values - serialized values of form using serializer functions of field;
    • value - current serialized value of the field;
    • rawValues - all values of form without processing;
    • rawValue - current value without processing of the field;
    • parents - stack of parent fields above current field with runtime values.

    Should return OBJECT of values. By default returns

    {
      [name]: errors[name],
    }
  • valueSetter - function for setting current runtime values. Receives next parameters:

    • setValue - a function to set value of the current field;
    • name - name of current field;
    • fieldSchema - full schema of current field;
    • getFieldSchema - see above;
    • getFieldType - see above;
    • value - current value of the field;
    • values - all values of the form;
    • parents - stack of parent fields above current field with runtime values.
  • createGetFieldSchema - function for create getFieldSchema for nested fields. Can be helpful for arrays of repeating fields etc. Parameters:

    • fieldSchema - schema of current field;
    • getFieldSchema - default getFieldSchema;
    • getFieldType - see above;
    • values - current values (values of form during render and serialization or raw values during parsing);
    • phase - one of next values: 'parse', 'serialize', 'render';
    • parents - stack of parent fields above current field, raw values for phase 'parse' and runtime values otherwise.

Usage

Serialization

import { serialize } from '@vtaits/form-schema';

// ...

serialize({
	values,
	names,
	getFieldSchema,
	getFieldType,
	parents,
});

Parsing

import { parse } from '@vtaits/form-schema';

// ...

parse({
	values,
	names,
	getFieldSchema,
	getFieldType,
	parents,
});

Validation before submit

import { validateBeforeSubmit } from '@vtaits/form-schema';

// ...

validateBeforeSubmit({
	setError,
	values,
	names,
	getFieldSchema,
	getFieldType,
	parents,
});

Mapping of errors of fields

import { setFieldErrors } from '@vtaits/form-schema';

// ...

setFieldErrors({
	setError,
	errors,
	names,
	getFieldSchema,
	getFieldType,
	values,
	rawValues,
	parents,
});
  • values - serialied values of form (result of serialize);
  • rawValues - raw values of form (before serialize).