Validators
Built-in validation rules for use with useForm and useField. All accept an optional custom error message.
import {
required, minLength, maxLength,
pattern, min, max, email, url, custom
} from "@allem-sdk/forms";Available validators
| Validator | Signature | Description |
|---|---|---|
| required | required(msg?) | Field must not be empty |
| minLength | minLength(n, msg?) | Minimum string length |
| maxLength | maxLength(n, msg?) | Maximum string length |
| min | min(n, msg?) | Minimum numeric value |
| max | max(n, msg?) | Maximum numeric value |
| pattern | pattern(regex, msg?) | Must match regex |
| email(msg?) | Valid email format | |
| url | url(msg?) | Valid URL (http/https) |
| custom | custom(fn) | Custom validation function |
Custom validator
const passwordStrength = custom<string>((value) => {
if (value.length < 8) return "Must be at least 8 characters";
if (!/[A-Z]/.test(value)) return "Must contain an uppercase letter";
if (!/[0-9]/.test(value)) return "Must contain a number";
return undefined; // valid
});
const form = useForm({
password: {
initialValue: "",
rules: [required(), passwordStrength],
},
});