|
| 1 | +declare const validSym: unique symbol |
| 2 | +declare const namedSym: unique symbol |
| 3 | + |
| 4 | +/** |
| 5 | + * @tsplus derive nominal |
| 6 | + */ |
| 7 | +export interface Brand<in out K extends string> { |
| 8 | + [namedSym]: { |
| 9 | + [k in K]: K |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +export declare namespace Brand { |
| 14 | + export type valid = typeof validSym |
| 15 | + export type name = typeof namedSym |
| 16 | + |
| 17 | + export type Validated<A, K extends string> = A & Brand.Valid<A, K> |
| 18 | + |
| 19 | + /** |
| 20 | + * @tsplus derive nominal |
| 21 | + */ |
| 22 | + export interface Valid<in out A, in out K extends string> { |
| 23 | + [validSym]: { |
| 24 | + [k in K]: A |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + export type IsValidated<P extends Valid<any, any>> = { |
| 29 | + [k in keyof P[Brand.valid]]: P extends P[Brand.valid][k] ? 0 : 1 |
| 30 | + }[keyof P[Brand.valid]] extends 0 ? unknown : never |
| 31 | + |
| 32 | + export type Unbranded<P> = P extends infer Q & Brands<P> ? Q : P |
| 33 | + export type Unnamed<P> = P extends infer Q & Names<P> ? Q : P |
| 34 | + |
| 35 | + export type Brands<P> = P extends Valid<any, any> ? TypeLevel.UnionToIntersection< |
| 36 | + { |
| 37 | + [k in keyof P[Brand.valid]]: P extends P[Brand.valid][k] ? k extends string ? Valid<P[Brand.valid][k], k> : never |
| 38 | + : never |
| 39 | + }[keyof P[Brand.valid]] |
| 40 | + > |
| 41 | + : unknown |
| 42 | + |
| 43 | + export type Names<P> = P extends Brand<any> ? TypeLevel.UnionToIntersection< |
| 44 | + { |
| 45 | + [k in keyof P[Brand.name]]: k extends string ? Brand<k> |
| 46 | + : never |
| 47 | + }[keyof P[Brand.name]] |
| 48 | + > |
| 49 | + : unknown |
| 50 | + |
| 51 | + export interface FailedValidation { |
| 52 | + readonly _tag: "FailedValidation" |
| 53 | + readonly brands: string[] |
| 54 | + readonly message: string |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @tsplus type Brand.MakeValidated |
| 59 | + * @tsplus derive nominal |
| 60 | + */ |
| 61 | + export interface MakeValidated<in out A> { |
| 62 | + make: (value: Unnamed<Unbranded<A>>) => Either<FailedValidation, A> |
| 63 | + unsafeMake: (value: Unnamed<Unbranded<A>>) => A |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @tsplus type Brand.Make |
| 68 | + * @tsplus derive nominal |
| 69 | + */ |
| 70 | + export interface Make<in out A> { |
| 71 | + make: (value: Unnamed<Unbranded<A>>) => A |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @tsplus type Brand.Validation |
| 76 | + * @tsplus derive nominal |
| 77 | + */ |
| 78 | + export interface Validation<in out A, in out K extends string> { |
| 79 | + readonly validate: (a: A) => a is A & Brand.Valid<A, K> |
| 80 | + } |
| 81 | + |
| 82 | + export type ValidatedWith<X extends Validation<any, any>> = X extends Validation<infer A, infer K> ? Validated<A, K> |
| 83 | + : never |
| 84 | + |
| 85 | + /** |
| 86 | + * @tsplus type Brand/Ops |
| 87 | + */ |
| 88 | + export interface Ops { |
| 89 | + readonly validation: <A, K extends string>(predicate: (a: A) => boolean) => Brand.Validation<A, K> |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export function validation<A, K extends string>(predicate: (a: A) => boolean): Brand.Validation<A, K> { |
| 94 | + return { |
| 95 | + validate: (value): value is Brand.Validated<A, K> => predicate(value) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export const Brand: Brand.Ops = { |
| 100 | + validation |
| 101 | +} |
| 102 | + |
| 103 | +export class FailedValidationException extends Error implements Brand.FailedValidation { |
| 104 | + readonly _tag = "FailedValidation" |
| 105 | + constructor(readonly brands: string[]) { |
| 106 | + super(`Failed Validation of brands: ${brands.join(", ")}`) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export class FailedValidationError implements Brand.FailedValidation { |
| 111 | + readonly _tag = "FailedValidation" |
| 112 | + constructor(readonly brands: string[]) {} |
| 113 | + get message() { |
| 114 | + return `Failed Validation of brands: ${this.brands.join(", ")}` |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export type Regex<R extends string> = Brand.Validated<string, `Regex(${R})`> |
| 119 | + |
| 120 | +/** |
| 121 | + * @tsplus derive Brand.Validation<_, _> 10 |
| 122 | + */ |
| 123 | +export function deriveRegexValidation<B extends string, V extends `Regex(${string})`>( |
| 124 | + ...[regexStr]: V extends `Regex(${infer R extends string})` ? [R] : never |
| 125 | +): Brand.Validation<B, V> { |
| 126 | + const r = new RegExp(regexStr) |
| 127 | + return validation((b) => r.test(b)) |
| 128 | +} |
| 129 | + |
| 130 | +export type Min<N extends number> = Brand.Validated<number, `Min(${N})`> |
| 131 | +export type Max<N extends number> = Brand.Validated<number, `Max(${N})`> |
| 132 | +export type Range<X extends number, Y extends number> = Min<X> & Max<Y> |
| 133 | + |
| 134 | +/** |
| 135 | + * @tsplus derive Brand.Validation<_, _> 10 |
| 136 | + */ |
| 137 | +export function deriveMinValidation<B extends number, V extends `Min(${number})`>( |
| 138 | + ...[min]: V extends `Min(${infer N extends number})` ? [N] : never |
| 139 | +): Brand.Validation<B, V> { |
| 140 | + return validation((b) => b >= min) |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @tsplus derive Brand.Validation<_, _> 10 |
| 145 | + */ |
| 146 | +export function deriveMaxValidation<B extends number, V extends `Max(${number})`>( |
| 147 | + ...[max]: V extends `Max(${infer N extends number})` ? [N] : never |
| 148 | +): Brand.Validation<B, V> { |
| 149 | + return validation((b) => b <= max) |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * @tsplus derive Brand.MakeValidated<_> 10 |
| 154 | + */ |
| 155 | +export function deriveMakeMakeValidated<A>( |
| 156 | + ...[brands]: [ |
| 157 | + brands: A extends Brand.Valid<any, any> ? { |
| 158 | + [k in (keyof A[Brand.valid]) & string]: Brand.Validation<A[Brand.valid][k], k> |
| 159 | + } |
| 160 | + : {} |
| 161 | + ] |
| 162 | +): Brand.MakeValidated<A> { |
| 163 | + const make = (value: Brand.Unnamed<Brand.Unbranded<A>>): Either<Brand.FailedValidation, A> => { |
| 164 | + const failures: string[] = [] |
| 165 | + for (const brand of Object.keys(brands)) { |
| 166 | + if (!brands[brand]!.validate(value as any)) { |
| 167 | + failures.push(brand) |
| 168 | + } |
| 169 | + } |
| 170 | + if (failures.length > 0) { |
| 171 | + return Either.left(new FailedValidationError(failures)) |
| 172 | + } |
| 173 | + return Either.right(value) |
| 174 | + } |
| 175 | + const unsafeMake = (value: Brand.Unnamed<Brand.Unbranded<A>>) => { |
| 176 | + const errorOrValue = make(value) |
| 177 | + if (errorOrValue.isLeft()) { |
| 178 | + throw new FailedValidationException(errorOrValue.left.brands) |
| 179 | + } |
| 180 | + return errorOrValue.right |
| 181 | + } |
| 182 | + return { |
| 183 | + make, |
| 184 | + unsafeMake |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * @tsplus derive Brand.Make<_> 10 |
| 190 | + */ |
| 191 | +export function deriveMake<A>( |
| 192 | + ..._: [] |
| 193 | +): Brand.Make<A> { |
| 194 | + return { |
| 195 | + make: (a) => a |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +/** @tsplus implicit */ |
| 200 | +export const Positive = Brand.validation<number, "Positive">((n: number) => n > 0) |
| 201 | +export type Positive = Brand.ValidatedWith<typeof Positive> |
| 202 | + |
| 203 | +/** @tsplus implicit */ |
| 204 | +export const Int = Brand.validation<number, "Int">((n: number) => Number.isInteger(n)) |
| 205 | +export type Int = Brand.ValidatedWith<typeof Int> |
| 206 | + |
| 207 | +/** @tsplus implicit */ |
| 208 | +export const Finite = Brand.validation<number, "Finite">((n: number) => Number.isFinite(n)) |
| 209 | +export type Finite = Brand.ValidatedWith<typeof Finite> |
0 commit comments