Skip to content

a-terohid/smartformvalidate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

smartformvalidate

A schema-driven, extensible, and level-based form validation library for JavaScript and TypeScript.

Designed for developers who want:

  • Full control over validation logic
  • Clean separation between form data and validation rules
  • Extensible validators and schemas
  • Customizable error output (messages, suggestions, localization)

✨ Key Features

  • πŸ”Œ Schema-based fields (email, password, age, etc.)
  • 🎚 Validation levels (soft, normal, strict)
  • 🧩 Pluggable validators (register your own rules)
  • 🎨 Custom error formatting (i18n-ready)
  • 🧠 Smart merging logic (base rules β†’ level rules β†’ field overrides)
  • πŸ§ͺ Fully typed with TypeScript

βœ… This package supports a comprehensive set of commonly-used fields across real-world websites (auth forms, profiles, payments, admin panels, etc.).


πŸ“ Project Structure (Important)

If you want to inspect or extend the internals, here is where things live:

Built-in Field Schemas

src/fields/

Each file defines a reusable field schema (e.g. email, password, age, phone, etc.).

Built-in Validator Functions

src/validators/

Each validator is an isolated function responsible for a single rule (minLength, required, isEmail, ...).

πŸ“Œ If you want to understand how a rule works internally, start here.


πŸ“¦ Installation

npm install smartformvalidate

or

yarn add smartformvalidate

πŸš€ Quick Start

Basic Usage

import { smartValidate } from 'smartformvalidate';

const formData = {
  email: 'user@example.com',
  age: 25,
};

const result = smartValidate(formData);

console.log(result.valid); // true | false
console.log(result.errors);

🧱 Core Concepts

Form Data

smartformvalidate never mutates your data. It only reads from it.

const formData = {
  emailField: 'test@gmail.com',
  age: 18,
};

Field Configuration

Each field can define how it should be validated.

const config = {
  fields: {
    emailField: {
      extends: 'email',
      level: 'strict',
      overrideRules: { minLength: 5 }
    },
  },
};

🧬 Schemas & extends

Schemas define reusable validation logic.

Example built-in schema:

email: {
  base: { required: true },
  soft: { email: true },
  strict: { email: true, minLength: 8 },
}

Usage:

emailField: {
  extends: 'email'
}

🎚 Validation Levels

Levels allow you to control strictness dynamically.

const config = {
  defaultLevel: 'strict',
};

Field-level override:

age: {
  extends: 'age',
  level: 'soft'
}

Priority:

Field Level β†’ Form Default Level β†’ Schema Default Level

βš–οΈ Rule Priority Order

Rules are merged in the following order (lowest β†’ highest priority):

  1. Schema base rules
  2. Schema level rules
  3. fieldConfig.required
  4. fieldConfig.overrideRules

This guarantees predictable behavior.


🧩 Built-in Validators

The package ships with a large, production-ready set of validators covering most real-world use cases.

Basic Validators

  • required
  • requiredIf
  • minLength
  • maxLength
  • regex
  • mustInclude
  • mustExclude
  • isSameAs

Number Validators

  • isNumber
  • isInteger
  • minNumber
  • maxNumber

Array Validators

  • minItems
  • maxItems
  • uniqueItems

Date Validators

  • isDate
  • minDate
  • maxDate
  • isValidBirthDate

String Validators

  • isString
  • isNumericString
  • isStrongPassword

Enum / Boolean Validators

  • isEnum
  • isBoolean

Identification Validators

  • isBloodType
  • isNationalId
  • isSocial
  • isPhoneNumber
  • isUuid
  • isNationalCompanyId

URL / File / Object Validators

  • isUrl
  • isFileType
  • isObject
  • maxProperties
  • mustHaveKeys

Financial / Payment Validators

  • isCreditCard
  • isIban
  • cardExpire
  • isCryptoAddress

Misc Validators

  • isCoordinate
  • isTimezone

Example:

overrideRules: {
  minLength: 6,
  maxLength: 20,
}

🧠 Custom Validators (Field-Level)

You can attach validators directly to a field.

age: {
  customValidators: [
    (value) => {
      if (value % 2 !== 0) {
        return {
          rule: 'evenNumber',
          code: 'NOT_EVEN',
          message: 'Age must be an even number',
        };
      }
      return null;
    }
  ]
}

πŸ”Œ Registering Global Validators (Plugin System)

You can add your own validators globally.

import { registerValidator } from 'smartformvalidate';

registerValidator('startsWithA', (value) => {
  if (typeof value === 'string' && !value.startsWith('A')) {
    return {
      rule: 'startsWithA',
      code: 'INVALID_START',
      message: 'Value must start with letter A',
    };
  }
  return null;
});

Usage in config:

overrideRules: {
  startsWithA: true
}

🎨 Error Formatter (Localization & Custom Output)

You can fully control error messages and suggestions.

const config = {
  errorFormatter: (error, ctx) => {
    if (error.rule === 'required') {
      return {
        ...error,
        message: 'This field is mandatory',
        suggestion: 'Please provide a value',
      };
    }

    if (error.rule === 'minLength') {
      return {
        ...error,
        message: `Minimum length is ${ctx.ruleConfig}`,
      };
    }

    return error;
  }
};

Perfect for:

  • i18n
  • UX customization
  • Product-specific wording

πŸ“Š Validation Result Structure

{
  valid: boolean,
  errors: {
    [fieldName]: {
      valid: boolean,
      errors: FieldError[]
    }
  }
}

πŸ§ͺ Full Example

const formData = {
  email: 'wrong-email',
  age: 15,
};

const config = {
  defaultLevel: 'strict',
  fields: {
    email: { extends: 'email' },
    age: { extends: 'age' }
  }
};

const result = smartValidate(formData, config);

🧠 Philosophy

This library is built around predictability, explicitness, and extensibility.

  • No magic strings
  • No hidden mutations
  • No hard-coded messages

You control everything.


πŸ›£ Roadmap

  • Async validators
  • Cross-field dependency helpers
  • Built-in i18n packs
  • React / Vue adapters

πŸ“„ License

MIT


🟦 Github

https://github.com/a-terohid/smartformvalidate

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors