Validation
Validate JavaScript objects against YounndAI Domain schemas.
Quick Example
import { validateRecord } from "@younndai/domains";
const result = await validateRecord("yon.fintech", "TXN", {
id: "txn-001",
amount: 1500.5,
currency: "USD",
});
5 Constraint Types
| Constraint | Description | Example |
|---|---|---|
required | Field must be present | { required: true } |
type | Value type must match | { type: 'int' } |
range | Numeric value within bounds | { range: [0, 999999] } |
enum | Value in allowed set | { enum: ['USD', 'EUR'] } |
pattern | String matches regex | { pattern: '^[A-Z]{3}$' } |
Sync Validation
When you already have the schema, skip network resolution:
import { validateRecordSync, getBundledDomain } from "@younndai/domains";
const domain = getBundledDomain("yon.fintech")!;
const result = validateRecordSync("TXN", { id: "txn-001" }, domain);
Batch Validation
Each domain is resolved once — efficient for hundreds of records:
import { validateRecords } from "@younndai/domains";
const results = await validateRecords([
{ domainId: "yon.fintech", tag: "TXN", data: { id: "txn-001", amount: 100 } },
{ domainId: "yon.health", tag: "VITALS", data: { bp: "120/80" } },
]);
Error Behavior
- Domain not found: Returns
{ valid: false, errors: [{ field: '_domain' }] }— never throws - Tag not found: Returns
{ valid: false, errors: [{ field: '_tag' }] }— never throws
ValidationResult Type
interface ValidationResult {
valid: boolean;
errors: ValidationError[];
warnings: ValidationError[];
}
interface ValidationError {
field: string;
message: string;
constraint: "required" | "type" | "range" | "enum" | "pattern";
expected?: string;
received?: string;
}