1.0.1 • Published 10 months ago

valid-utils v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Welcome to Valid Utils

A simple and flexible validation package for common data types and formats, including email, URLs, dates, numbers, and more.

Installation

npm install valid-utils

Usage

import React, { useState } from "react";

// Assuming `isItValidPass` is imported from your utility file
import { isItValidPass } from "valid-utils";

const PasswordValidator: React.FC = () => {
  const [password, setPassword] = useState<string>("");
  const [validationResult, setValidationResult] = useState<string | null>(null);

  const validatePassword = () => {
    const options = {
      minLength: 8,
    };

    const { result, isValid } = isItValidPass(password, options);

    if (isValid) {
      setValidationResult("Password is valid");
    } else {
      setValidationResult(`Password is invalid: ${result}`);
    }
  };

  return (
    <div>
      <input
        type="password"
        value={password}
        onChange={(e) => setPassword(e.target.value)}
        placeholder="Enter your password"
      />
      <button onClick={validatePassword}>Validate Password</button>
      {validationResult && <p>{validationResult}</p>}
    </div>
  );
};

export default PasswordValidator;

Available Validation Functions

isItValidEmail

Validates an email address based on various customizable options.

  • Parameters:
    • email (string): The email address to validate.
    • options (optional EmailOptions): Optional configuration for the validation process.
  • Returns: { isValid: boolean, result: string }

isItValidPass

Validates a password based on various customizable options.

  • Parameters:
    • password (string): The password to validate.
    • options (optional PasswordOptions): Optional configuration for the validation process.
  • Returns: { isValid: boolean, result: string }

isItValidDate

Validates a date based on a specified format.

  • Parameters:
    • date (string): The date to validate.
    • format (string): The date format to validate (e.g., "YYYY-MM-DD", "MM/DD/YYYY", "DD/MM/YYYY").
  • Returns: { isValid: boolean, result: string }

isItValidNumber

Validates a numeric input based on various customizable options.

  • Parameters:
    • value (number | string): The number to validate.
    • options (optional NumericOptions): Optional configuration for the validation process.
  • Returns: { isValid: boolean, result: string }

isItValidUrl

Validates a URL based on various customizable options.

  • Parameters:
    • url (string): The URL to validate.
    • options (optional UrlOptions): Optional configuration for the validation process.
  • Returns: { isValid: boolean, result: string }

Type Definitions

ResponseShape

The shape of the response returned by validation functions.

PropertyTypeDescription
isValidbooleanIndicates whether the input is valid or not.
resultstring | numberThe result of the validation. Contains validation errors or the valid input.

GlobalOptions

Global options that apply to various validation functions.

PropertyTypeDescription
minLengthnumberMinimum length for the input.
maxLengthnumberMaximum length for the input.
patternRegExpRegular expression pattern for validation.

EmailOptions

Options specific to email validation.

PropertyTypeDescription
allowSpecialCharactersbooleanAllow special characters in the local part.
allowedDomainsstring[]List of allowed domains.
disallowedDomainsstring[]List of disallowed domains.
caseSensitivebooleanCase sensitivity for email validation.
allowSubdomainsbooleanAllow subdomains in the domain part.
requiredTLDbooleanRequire a top-level domain.
isRequiredbooleanWhether the email is required.

PasswordOptions

Options specific to password validation.

PropertyTypeDescription
requireUppercasenumberMinimum number of uppercase characters required.
requireLowercasenumberMinimum number of lowercase characters required.
requireNumbersnumberMinimum number of numeric characters required.
requireSpecialCharsnumberMinimum number of special characters required.
specialCharsstringAllowed special characters.
minUniqueCharsnumberMinimum number of unique characters required.

NumericOptions

Options specific to numeric validation.

PropertyTypeDescription
minnumberMinimum value allowed.
maxnumberMaximum value allowed.
decimalPlacesnumberNumber of decimal places allowed.

UrlOptions

Options specific to URL validation.

PropertyTypeDescription
protocolsAllowedProtocols[]Allowed URL protocols.
formatRegExpRegular expression format for validation.

AllowedProtocols

This type defines the allowed protocols for URLs.

ProtocolDescription
httpHypertext Transfer Protocol
httpsHypertext Transfer Protocol Secure
ftpFile Transfer Protocol
ftpsFile Transfer Protocol Secure
mailtoEmail address
fileLocal file
dataData URL scheme
telTelephone number
smsShort Message Service
wsWebSocket protocol
wssWebSocket Secure protocol

Contributing

There are many different ways to contribute to React Router's development. If you're interested, check out our contributing guidelines to learn how you can get involved.

1.0.1

10 months ago

1.0.0

10 months ago