| 272 | * @since 4.0.0 |
| 273 | */ |
| 274 | export function check<A extends Brand<any>>( |
| 275 | ...checks: readonly [ |
| 276 | SchemaAST.Check<Brand.Unbranded<A>>, |
| 277 | ...Array<SchemaAST.Check<Brand.Unbranded<A>>> |
| 278 | ] |
| 279 | ): Constructor<A> { |
| 280 | const result = (input: Brand.Unbranded<A>): Result.Result<A, BrandError> => { |
| 281 | return Result.mapError(SchemaAST.runChecks(checks, input), (issue) => new BrandError(issue)) as any |
| 282 | } |
| 283 | return Object.assign((input: Brand.Unbranded<A>) => Result.getOrThrow(result(input)), { |
| 284 | option: (input: Brand.Unbranded<A>) => Option.getSuccess(result(input)), |
| 285 | result, |
| 286 | is: (input: Brand.Unbranded<A>): input is Brand.Unbranded<A> & A => Result.isSuccess(result(input)), |
| 287 | checks |
| 288 | }) |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Combines one or more brand constructors to form a single branded type. |