0.0.19 • Published 4 months ago

onsubmit v0.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

onSubmit

Description

onsubmit is the simplest, least set-up needing way to validate an input or a form. On the client or on the server.

Features

  • Light-weight. (~1KB gzipped)
  • Simple API.
  • Zero dependencies.

Install

  npm i onsubmit

Quickstart

import { validateField } from 'onsubmit';

const firstNameRules = {
  required: { criterion: true, message: 'First Name is required' },
  minLength: { criterion: 3, message: 'Minimum length is 3' },
  maxLength: { criterion: 10, message: 'Maximum length is 10' },
};

// recieve an array of errors
const errors = validateField('Ammar', 'firstName', firstNameRules);

API

Methods

FunctionDescriptionParametersReturns
validateFieldValidates a single form field against specified rules.value: The string value of the field. name: Name of the field. rules: Object containing validation rules.Array of FieldError objects, each containing the name of the field and the error message.
validateFormValidates an entire form.values: A key-value pair object of field names and values. rules: A key-value object which maps field names to their rules .Array of FieldError objects for the entire form.

Validation Rules

RuleDescriptionExpected Value
minLengthMinimum length of the field's value.Number (length)
maxLengthMaximum length of the field's value.Number (length)
patternRegex pattern the field's value should match.RegExp
customCustom validation logic.Function
requiredWhether the field is required.Boolean

Examples

Validate a single field

import { validateField } from 'onsubmit';

const emailRules = {
  required: { criterion: true, message: 'Email is required' },
  pattern: {
    criterion: /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/,
    mesaage: 'Invalid email',
  },
};

const errors = validateField('ammar@winenergy.co' , 'email', emailRules);

Custom validation logic

import { validateForm } from 'onsubmit';

const rulesObject = {
  custom: {
    criterion: (value) => value === password,
    message: 'Passwords do not match',
  }
};

const errors = validateField(passwordRepeat, 'passwordRepeat', rulesObject);

Validate some data

import { validateForm } from 'onsubmit';

const rulesObject = {
  minLength: { criterion: 3, message: 'Minimum length is 3' },
  maxLength: { criterion: 10, message: 'Maximum length is 10' },
  pattern: { criterion: /^[a-z]+$/, message: 'Only lowercase letters allowed' },
  custom: { criterion: (value) => value !== 'example', message: 'Value cannot be "example"' },
  required: { criterion: true, message: 'Field is required' },
};

const data = {
  firstName: 'Ammar',
  lastName: 'Halees',
  email: 'ammar@company.co',
};

const errors = validateForm(data, rulesObject);

Validate a form

import { validateForm } from 'onsubmit';

const handleOnsubmit = (e) => {
  e.preventDefault();

  const formData = new FormData(e.currentTarget);
  const data = Object.fromEntries(formData);
  const errors = validateForm(data, RulesMap);
};

Utils and patterns

utils

onsubmit provides a set of utility functions and patterns to help you build your forms.

Function NameType SignatureDescription
isDateObject(value: unknown) => value is DateChecks if the given value is a valid Date object.
isString(value: unknown) => value is stringDetermines if the provided value is a string. This includes both string literals and String objects.
isNumber(value: unknown) => value is numberVerifies if the value is a number and is finite.
isNullOrUndefined(value: unknown) => value is null \| undefinedChecks if the value is either null or undefined.
isObject<T extends object>(value: unknown) => value is TDetermines if a value is an object. This excludes null, arrays, and Date objects.
isFile(element: HTMLInputElement) => element is HTMLInputElementChecks if an HTML input element is of type file.

_patterns

  1. email
  2. uri
  3. alphanumeric
  4. cuid
  5. kebabCase
  6. arabic

FAQ

Which rule object has precedence?

The required rule has the highest precedence. The remaining rules are evaluated in the order they are specified in the rulesObject.

Types

type Criterion = string | number | CustomFunction | boolean | RegExp;

interface Rule {
criterion: Criterion;
message: string;
}

type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue };

interface RulesObject {
required?: Rule;
minLength?: Rule;
maxLength?: Rule;
pattern?: Rule;
custom?: Rule;
}

TODO

  • onlySecure opt-out rule.
  • minDate rule.
  • maxDate rule.
  • minTime rule.rule
  • maxTime rule
  • file rule: { minSize, maxSize, type, name }
  • cardNumber pattern.
  • cardCompany utility.
  • Benchmarking
  • Allow for multiple patterns
0.0.19

4 months ago

0.0.18

5 months ago

0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.13

5 months ago

0.0.14

5 months ago

0.0.15

5 months ago

0.0.9

5 months ago

0.0.16

5 months ago

0.0.8

5 months ago

0.0.17

5 months ago

0.0.7

5 months ago

0.0.5

5 months ago

0.0.6

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago