Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adria-forms

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adria-forms

A super simple form validation library

  • 0.2.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Adria

A super simple form validation library, with autocomplete and value/type checking using the power of TypeScript.

npm i adria-forms

Demo: https://adria-demo.vercel.app

Overview

import { Form } from "adria-forms";

const form = new Form().field("username", (value) => {
  if (!value) return "Please enter your username";
  if (typeof value !== "string") return "Invalid input";
  if (value.length < 4) return "Username must be at least 4 characters long";
  return; // success
});

const formData = new FormData(); // can be a regular object as well

const errors = await form.validate(formData);
/*
usernameError will be typed as "Please enter..." or "Username must be..."
*/
const usernameError = errors?.username; // autocomplete

Reference

.field()

Creates a new field. fieldName cannot be an existing field name, and validate can be a synchronous or asynchronous function. A void return from validate will tell Adria the value has passed the validation.

const field: (
  fieldName: string,
  validate: (
    value: null | FormDataEntryValue,
    formData: Map<string, FormDataEntryValue | null>
  ) => MaybePromise<void | any>
) => this;
Example
new Form()
  .field("username", (value) => {
    if (!value)
      return {
        code: 0,
        message: "empty input",
      };
    return; // success
  })
  .field("password", (_, formData) => {
    const usernameField = formData.get("username"); // autocompletes username, password
    const passwordField = formData.get("randomFieldName"); // TS will yell at you since the field doesn't exist yet
  })
  .field(
    "username" // TS will yell at you since this field already exists
    // ...
  );

.validate()

Validates the form data. Will only check fields defined with .field(). Will return null if the form data is valid or a fieldName:errorMessage record if not.

const validate: (
  formData: FormData | Record<any, any>
) => Promise<Record<string, any> | null>;
Example
const form = new Form()
  .field("username", () => {
    return "error";
  })
  .field("password", () => {
    return {
      code: 0,
    };
  });

const errors = await form.validate(formData);

const userNameError: "fail" = errors.username; // autocomplete username, password
const randomError = errors.random; // TS will yell at you since field random does not exist
const passwordErrorCode: number = errors.password.code; // since password can return an object, code will be typed as number and not 0

TypeScript

In the previous example (validate()), errors will only be typed with a value when the validate function returns a string/number. We can fix this by typing the return value of the validate function as const.

const form = new Form().field("password", () => {
  return {
    code: 0,
  } as const;
});

const errors = await form.validate(formData as FormData);

const passwordErrorCode: number = errors.password.code; // typed as 0, and not number as before

Keywords

FAQs

Package last updated on 04 Dec 2022

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