@txmo-dev/midas-validation
v1.0.3
Published
TypeScript mirror of Midas.Validation — reusable validation rules and helpers for the Midas HMS ecosystem (phone, NHIS ID, email, dates).
Maintainers
Readme
@txmo-dev/midas-validation
TypeScript mirror of Midas.Validation — reusable validation rules and helpers for the Midas HMS ecosystem.
Includes built-in rules for Ghana phone numbers, NHIS IDs, email addresses, PHI field masking, and data subject rights types.
Installation
npm install @txmo-dev/midas-validationUsage
Functional style (lightweight)
import { validate, Rules } from '@txmo-dev/midas-validation';
const result = validate(formData, {
phone: [Rules.notEmpty, Rules.ghanaPhone],
email: [Rules.email],
nhisId: [Rules.nhisId],
dateOfBirth: [Rules.notFutureDate],
});
if (!result.isValid) {
console.log(result.errors);
// [{ field: 'phone', messages: ['phone must be a valid Ghana phone number'] }]
}Throw on failure
import { validateOrThrow } from '@txmo-dev/midas-validation';
validateOrThrow(formData, {
phone: [Rules.notEmpty, Rules.ghanaPhone],
});
// Throws MidasValidationError if invalidClass-based validator (mirrors C# MidasValidator<T>)
import { MidasValidator } from '@txmo-dev/midas-validation';
class RegisterPatientValidator extends MidasValidator<RegisterPatientCommand> {
constructor() {
super();
this.ruleFor('phone', (cmd) => cmd.phone, [this.ghanaPhone()]);
this.ruleFor('nhisId', (cmd) => cmd.nhisId, [this.nhisId()]);
this.ruleFor('email', (cmd) => cmd.email, [this.emailAddress()]);
this.ruleFor('firstName', (cmd) => cmd.firstName, [this.notEmpty(), this.maxLength(100)]);
}
}
const validator = new RegisterPatientValidator();
validator.validateOrThrow(command);PHI masking
import { maskPhi, isPhiField, PHI_FIELDS } from '@txmo-dev/midas-validation';
maskPhi('John Mensah', 'firstName'); // 'J*** M***'
maskPhi('0241234567', 'phone'); // '024***567'
maskPhi('[email protected]', 'email'); // 'j***@***.com'
maskPhi('1990-03-15', 'dateOfBirth'); // '****-**-15'
isPhiField('nhisId'); // true
isPhiField('clinicId'); // falseAPI
Standalone rules
| Rule | Description |
|------|-------------|
| Rules.notEmpty(field, value) | Field must not be empty |
| Rules.ghanaPhone(field, value) | Valid Ghana mobile number (0XXXXXXXXX) |
| Rules.nhisId(field, value) | Valid NHIS ID (6–20 alphanumeric) |
| Rules.email(field, value) | Valid email address |
| Rules.notFutureDate(field, value) | Date must not be in the future |
Functions
| Function | Description |
|----------|-------------|
| validate(value, rules) | Validate an object against field rules |
| validateOrThrow(value, rules) | Validate and throw MidasValidationError on failure |
PHI
| Export | Description |
|--------|-------------|
| PHI_FIELDS | Canonical list of PHI field names |
| isPhiField(name) | Returns true if the field is PHI |
| maskPhi(value, fieldType) | Mask a PHI value for safe display |
Classes
| Export | Description |
|--------|-------------|
| MidasValidator<T> | Abstract base validator with built-in rule helpers |
| MidasValidationError | Error thrown by validateOrThrow |
Compliance
- HIPAA 45 CFR §164.514(b) — PHI field definitions
- Ghana NDPA 2023 — Data subject rights
- GDPR Art. 5 — Data minimisation (PHI masking)
