New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@conform-to/validitystate

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@conform-to/validitystate

Validate on the server using the same rules as the browser

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23
decreased by-34.29%
Maintainers
1
Weekly downloads
 
Created
Source

@conform-to/validitystate

The current version is not compatible with conform react adapter.

Conform helpers for server validation based on the validation attributes.

API Reference

parse

A function to parse FormData or URLSearchParams on the server based on the constraints and an optional error formatter.

import { type FormConstraints, type FormatErrorArgs, parse } from '@conform-to/validitystate';

const constraints = {
    email: { type: 'email', required: true },
    password: { type: 'password', required: true },
    remember: { type: 'checkbox' },
} satisify FormConstraints;

function formatError({ input }: FormatErrorArgs) {
    switch (input.name) {
        case 'email': {
            if (input.validity.valueMissing) {
                return 'Email is required';
            } else if (input.validity.typeMismatch) {
                return 'Email is invalid';
            }
            break;
        }
        case 'password': {
            if (input.validity.valueMissing) {
                return 'Password is required';
            }
            break;
        }
     }

     return '';
}

const submission = parse(formData, {
  constraints,
  formatError,
});

// The error will be a dictinioary mapping input name to the corresponding errors
// e.g. { email: 'Email is required', password: 'Password is required' }
console.log(submission.error);

if (!submission.error) {
    // If no error, the parsed data will be available with the inferred type.
    // e.g. { email: string; password: string; remember: boolean; }
    console.log(submission.value);
}

The error formatter can also return multiple error.

function formatError({ input }: FormatErrorArgs) {
  const error = [];

  switch (input.name) {
    case 'email': {
      if (input.validity.valueMissing) {
        error.push('Email is required');
      }
      if (input.validity.typeMismatch) {
        error.push('Email is invalid');
      }
      break;
    }
    case 'password': {
      if (input.validity.valueMissing) {
        error.push('Password is required');
      }
      if (input.validity.tooShort) {
        error.push('Passowrd is too short');
      }
      break;
    }
  }

  return error;
}

If no error formatter is provided, check the defaultFormatError helpers for the default behavior.

validate

A helper to customize client validation by reusing the constraints and error formatter. Error will be set on the form control element using the setCustomValidity method. It should be called before reporting new error (i.e. triggering form.reportValidity()).

import { validate } from '@conform-to/validitystate';

function Example() {
  return (
    <form
      onSubmit={(event) => {
        const form = event.currentTarget;

        // validate before reporting new error
        validate(form, {
          constraints,
          formatError,
        });

        if (!form.reportValidity()) {
          event.preventDefault();
        }
      }}
      noValidate
    >
      {/* ... */}
    </form>
  );
}

defaultFormatError

This is the default error formatter used by parse to represent error by all failed validation attributes. For example:

{ "email": ["required", "type"], "password": ["required"] }

This helper is useful if you want to customize the error based on the default error formatter.

import { type FormConstraints, type FormatErrorArgs, defaultFormatError } from '@conform-to/validitystate';

const constraints = {
    email: { type: 'email', required: true },
    password: { type: 'password', required: true },
    confirmPassowrd: { type: 'password', required: true },
} satisify FormConstraints;

function formatError({ input }: FormatErrorArgs<typeof constraints>) {
    const error = defaultFormatError({ input });

    if (input.name === 'confirmPassword' && error.length === 0 && value.password !== value.confirmPassword) {
        error.push('notmatch');
    }

    return error;
}

const submission = parse(formData, {
    constraints,
    formatError,
});

getError

It gets the actual error messages stored on the validationMessage property. This is needed if the custom error formatter returns multiple error.

import { getError } from '@conform-to/validitystate';

function Example() {
  const [error, setError] = useState({});

  return (
    <form
      onInvalid={(event) => {
        const input = event.target as HTMLInputElement;

        setError((prev) => ({
          ...prev,
          [input.name]: getError(input.validationMessage),
        }));

        event.preventDefault();
      }}
    >
      {/* ... */}
    </form>
  );
}

Attributes supported

month and week input type are not implemented due to limited browser support

SupporttyperequiredminLengthmaxLengthpatternminmaxstepmultiple
text🗸🗸🗸🗸
email🗸🗸🗸🗸🗸
password🗸🗸🗸🗸
url🗸🗸🗸🗸🗸
tel🗸🗸🗸🗸
search🗸🗸🗸🗸
datetime-local🗸🗸🗸🗸
date🗸🗸🗸🗸
time🗸🗸🗸🗸
select🗸🗸
textarea🗸🗸🗸
radio🗸
color🗸
checkbox🗸
number🗸🗸🗸🗸
range🗸🗸🗸🗸
file🗸🗸

Keywords

FAQs

Package last updated on 09 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc