Small TypeScript validation helpers extracted from
hn_email_template outerTemplate component validation.
npm install @llazyemail/validatorRequires a read:packages token for GitHub Packages. See .npmrc.example.
import { validateInput, ValidationError, minLength } from '@llazyemail/validator';
try {
validateInput(
{ title: 'Newsletter', email: 'user@example.com' },
[
{ field: 'title', errorMessage: 'no title was passed' },
{ field: 'email', errorMessage: 'invalid email', rules: ['required', 'email'] },
{ field: 'title', errorMessage: 'title too short', rules: [minLength(3)] },
],
);
} catch (err) {
if (err instanceof ValidationError) {
console.error(err.field, err.message, err.ruleMessage);
}
}Built-in rules: required, nonEmptyString, string, email, url.
Length helpers: minLength(n), maxLength(n).
A check defaults to ['required', 'nonEmptyString'] when rules is omitted.
Custom Rule functions are also accepted in the rules array.
npm install
npm test
npm run typecheck
npm run buildtsup emits ESM + CJS + .d.ts into dist/.