Create a validated brand with runtime validation
const Email = ValidatedBrand("Email", (s: string) => /^[^@]+@[^@]+\.[^@]+$/.test(s))const email = Email.of("user@example.com") // Some(Brand<"Email", string>) Copy
const Email = ValidatedBrand("Email", (s: string) => /^[^@]+@[^@]+\.[^@]+$/.test(s))const email = Email.of("user@example.com") // Some(Brand<"Email", string>)
// With Either for error messagesconst Port = ValidatedBrand("Port", (n: number) => n >= 1 && n <= 65535)const result = Port.from(8080) // Right(Brand<"Port", number>)const error = Port.from(70000) // Left("Invalid Port: validation failed") Copy
// With Either for error messagesconst Port = ValidatedBrand("Port", (n: number) => n >= 1 && n <= 65535)const result = Port.from(8080) // Right(Brand<"Port", number>)const error = Port.from(70000) // Left("Invalid Port: validation failed")
// Type guard usageconst value: unknown = "test@example.com"if (Email.is(value)) { // value is Brand<"Email", string>} Copy
// Type guard usageconst value: unknown = "test@example.com"if (Email.is(value)) { // value is Brand<"Email", string>}
Create a validated brand with runtime validation