superform-validator
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

📦 SuperForm Validator

A powerful, flexible, and universal JavaScript form validation library that supports Native HTML Forms, Express.js API validation, and is planned to support React integration in the future.

npm npm bundle size (version) GitHub release (by tag) jsDelivr hits (npm) npm GitHub issues GitHub closed issues


🚀 Features

Feature Supported
Native HTML Form Validation ✅ Yes
Express.js Integration ✅ Yes
File Input Validation ✅ Yes
Type Casting & Pre/Post Processors ✅ Yes
Live Validation Support ✅ Yes
Custom Rule Registration ✅ Yes
Flexible Error Formatting Engine ✅ Yes
TypeScript Ready ✅ Yes
React Support ⏳ Coming Soon

📦 Installation

npm install superform-validator

CDN Usage

<!-- jsDeliver -->
<!-- Latest version -->
<script src="https://cdn.jsdelivr.net/npm/superform-validator@latest/dist/form-validator.js"></script>

<!-- OR -->

<!-- Recommended with specific version -->
<script src="https://cdn.jsdelivr.net/npm/superform-validator@1.0.4/dist/form-validator.js"></script>

<!-- OR -->

<!-- unpkg -->
<script src="https://unpkg.com/superform-validator@1.0.4/dist/form-validator.js"></script>

🔄 Quick Examples

HTML Form

const validator = FormValidator.init('#myForm', schema, onValid, onError, options);
validator.enableLiveValidation();

Express Middleware

app.get('/users', expressValidator.validateQuery({
  page: 'require|integer|gt(0)|cast::integer'
}), (req, res) => {
  res.json(req.validated);
});

Error Formatter

const msg = ErrorFormatter.format('Welcome @{user.name || "Guest"}', { user: {} });

📓 Full Documentation


🔄 Live Validation

validator.enableLiveValidation(['blur', 'change', 'input']);

Default events: blur, change.


🔧 Custom Rule Registration

FormValidator.registerRule({
  name: 'isEven',
  paramType: 'none',
  argumentType: 'integer',
  aliases: [],
  validators: [
    {
      callback: (value) => parseInt(value) % 2 === 0,
      message: '@{field} must be an even number'
    }
  ]
}, 'integer');

🔎 Validation Schemas

String Syntax

{
  email: 'require|email|case::lower',
  age: 'require|integer|length(2)|intBetween(10, 15)|cast::integer',
  gender: 'optional|inList(male,female,other,none)|default(none)'
}

Object Syntax with Custom Messages

{
  name: {
    require: true,
    minLength: 2,
    maxLength: 20,
    messages: {
      require: 'Name is required.',
      minLength: 'Name must be at least 2 characters.',
      maxLength: 'Name must be less than 20 characters.'
    }
  }
}

🔒 Validation Rules

Supported rules include:

  • require, minLength, maxLength, length, email, integer, float, mobile
  • file::maxFiles, file::maxSize, file::accepts
  • Pre/Post processors like trim, case::camel, case::upper
  • Casting: cast::integer, cast::boolean, cast::float
  • Full list available in detailed rule reference (coming soon)

Full list of available rules and processors


🔐 Error Placeholder Reference

Supported placeholders:

@{field}
@{param.field}
@{param.value}
@{param}
@{param.min}
@{param.max}
@{param.raw}

Supports advanced formatting with nested access, fallbacks, and string modifiers like upper, lower, capitalize.

Error Formatter Guide


📂 Validation Result Structure

{
  valid: boolean;
  validated: Record<string, any>;
  errors: Record<string, string>;
}

req.validated is available in Express.


🌟 Planned Features

  • 🔊 React integration via custom hooks
  • 🌐 Multi-language support
  • 👥 Field grouping and nested schema support

💻 Examples


📜 License

Released under the MIT License


👨‍💻 Author

Made with ❤️ by Harshal Khairnar
Founder, Hitraa Technologies
📧 harshal@hitraa.com

Package Sidebar

Install

npm i superform-validator

Weekly Downloads

9

Version

2.0.1

License

MIT

Unpacked Size

276 kB

Total Files

65

Last publish

Collaborators

  • khairnar2960