Skip to content

Built-in Rules

@pliant/core includes 11 production-ready rules that cover common validation scenarios. All rules are tree-shakeable—only the rules you import are included in your bundle.

Rule List

RuleDescriptionCommon Use
requiredValue must be presentRequired fields
emailValid email formatEmail inputs
numericNumeric characters onlyID numbers
lengthLength within rangeUsernames, passwords
minLengthMinimum lengthPasswords
maxLengthMaximum lengthComments, bios
rangeNumber within rangeAge, quantity
minMinimum valuePrices
maxMaximum valueLimits
regexPattern matchingPhone, postal code
equalsValue equalityPassword confirm

Quick Reference

ts
import {
  requiredRule,    // required()
  emailRule,       // email({ allowEmpty? })
  numericRule,     // numeric()
  lengthRule,      // length({ min?, max? })
  minLengthRule,   // minLength({ min })
  maxLengthRule,   // maxLength({ max })
  rangeRule,       // range({ min, max })
  minRule,         // min({ min })
  maxRule,         // max({ max })
  regexRule,       // regex({ pattern, invert? })
  equalsRule       // equals({ field?, value? })
} from '@pliant/core';

Usage Pattern

All rules follow the same pattern:

ts
import { createRegistry, addRules, ruleNameRule } from '@pliant/core';

const registry = createRegistry();

addRules(registry, {
  ruleName: ruleNameRule(options)
});

Complete Example

ts
import {
  createRegistry,
  addRules,
  evaluateRules,
  requiredRule,
  emailRule,
  lengthRule,
  minLengthRule,
  rangeRule,
  regexRule,
  equalsRule
} from '@pliant/core';

const registry = createRegistry();

addRules(registry, {
  required: requiredRule(),
  email: emailRule(),
  username: lengthRule({ min: 3, max: 20 }),
  password: minLengthRule({ min: 8 }),
  age: rangeRule({ min: 18, max: 120 }),
  phone: regexRule({ pattern: /^\d{10}$/ }),
  confirmPassword: equalsRule({ field: 'password' })
});

Inheritance

All rules can be extended with inheritRule():

ts
import { inheritRule, minLengthRule } from '@pliant/core';

const strongPassword = inheritRule(minLengthRule({ min: 12 }), {
  code: 'strongPassword'
});

See Rule Inheritance for details.

Custom Rules

Need something specific? See Custom Rules.

Async Rules

For server-side validation, see Async Rules.

Released under the MIT License.