| 868 | * @since 4.0.0 |
| 869 | */ |
| 870 | export function schema<T>(codec: Schema.ConstraintCodec<T, unknown>, path?: string | ConfigProvider.Path): Config<T> { |
| 871 | const codecStringTree = Schema.toCodecStringTree(codec) |
| 872 | const encodedAst = SchemaAST.toEncoded(codecStringTree.ast) |
| 873 | const decodeCursor = SchemaParser.decodeUnknownEffect( |
| 874 | Schema.make<Schema.Codec<T, ConfigCursor>>(toConfigCursorAST(codecStringTree.ast)) |
| 875 | ) |
| 876 | const localPath = typeof path === "string" ? [path] : path ?? [] |
| 877 | return make((provider, pathPrefix) => { |
| 878 | const fullPath = [...pathPrefix, ...localPath] |
| 879 | return loadCursor(provider, fullPath).pipe( |
| 880 | Effect.flatMapEager((cursor) => |
| 881 | decodeCursor(cursor).pipe( |
| 882 | Effect.mapEager(Result.succeed), |
| 883 | Effect.catchEager((issue) => { |
| 884 | const error = new ConfigError( |
| 885 | new Schema.SchemaError(fullPath.length > 0 ? new SchemaIssue.Pointer(fullPath, issue) : issue) |
| 886 | ) |
| 887 | return hasProviderInput(encodedAst, cursor.node) |
| 888 | ? Effect.fail(error) |
| 889 | : Effect.succeed(Result.fail(error)) |
| 890 | }) |
| 891 | ) |
| 892 | ), |
| 893 | mapReasons((reason) => |
| 894 | Cause.isDieReason(reason) && isSourceError(reason.defect) |
| 895 | ? Cause.makeFailReason(new ConfigError(reason.defect)).annotate(Cause.reasonAnnotations(reason)) |
| 896 | : reason |
| 897 | ) |
| 898 | ) |
| 899 | }) |
| 900 | } |
| 901 | |
| 902 | const PortSchema = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 })) |
| 903 | |