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)
- π 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.).
If you want to inspect or extend the internals, here is where things live:
src/fields/
Each file defines a reusable field schema (e.g. email, password, age, phone, etc.).
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.
npm install smartformvalidateor
yarn add smartformvalidateimport { 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);smartformvalidate never mutates your data. It only reads from it.
const formData = {
emailField: 'test@gmail.com',
age: 18,
};Each field can define how it should be validated.
const config = {
fields: {
emailField: {
extends: 'email',
level: 'strict',
overrideRules: { minLength: 5 }
},
},
};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'
}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
Rules are merged in the following order (lowest β highest priority):
- Schema base rules
- Schema level rules
fieldConfig.requiredfieldConfig.overrideRules
This guarantees predictable behavior.
The package ships with a large, production-ready set of validators covering most real-world use cases.
requiredrequiredIfminLengthmaxLengthregexmustIncludemustExcludeisSameAs
isNumberisIntegerminNumbermaxNumber
minItemsmaxItemsuniqueItems
isDateminDatemaxDateisValidBirthDate
isStringisNumericStringisStrongPassword
isEnumisBoolean
isBloodTypeisNationalIdisSocialisPhoneNumberisUuidisNationalCompanyId
isUrlisFileTypeisObjectmaxPropertiesmustHaveKeys
isCreditCardisIbancardExpireisCryptoAddress
isCoordinateisTimezone
Example:
overrideRules: {
minLength: 6,
maxLength: 20,
}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;
}
]
}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
}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
{
valid: boolean,
errors: {
[fieldName]: {
valid: boolean,
errors: FieldError[]
}
}
}const formData = {
email: 'wrong-email',
age: 15,
};
const config = {
defaultLevel: 'strict',
fields: {
email: { extends: 'email' },
age: { extends: 'age' }
}
};
const result = smartValidate(formData, config);This library is built around predictability, explicitness, and extensibility.
- No magic strings
- No hidden mutations
- No hard-coded messages
You control everything.
- Async validators
- Cross-field dependency helpers
- Built-in i18n packs
- React / Vue adapters
MIT